rustc_attr_parsing/attributes/
allow_unstable.rs1use std::iter;
2
3use rustc_feature::{AttributeTemplate, template};
4use rustc_hir::attrs::AttributeKind;
5use rustc_hir::{MethodKind, Target};
6use rustc_span::{Span, Symbol, sym};
7
8use super::{CombineAttributeParser, ConvertFn};
9use crate::context::MaybeWarn::{Allow, Warn};
10use crate::context::{AcceptContext, AllowedTargets, Stage};
11use crate::parser::ArgParser;
12use crate::session_diagnostics;
13
14pub(crate) struct AllowInternalUnstableParser;
15impl<S: Stage> CombineAttributeParser<S> for AllowInternalUnstableParser {
16 const PATH: &[Symbol] = &[sym::allow_internal_unstable];
17 type Item = (Symbol, Span);
18 const CONVERT: ConvertFn<Self::Item> =
19 |items, span| AttributeKind::AllowInternalUnstable(items, span);
20 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
21 Allow(Target::MacroDef),
22 Allow(Target::Fn),
23 Warn(Target::Field),
24 Warn(Target::Arm),
25 ]);
26 const TEMPLATE: AttributeTemplate = template!(Word, List: &["feat1, feat2, ..."]);
27
28 fn extend<'c>(
29 cx: &'c mut AcceptContext<'_, '_, S>,
30 args: &'c ArgParser<'_>,
31 ) -> impl IntoIterator<Item = Self::Item> {
32 parse_unstable(cx, args, <Self as CombineAttributeParser<S>>::PATH[0])
33 .into_iter()
34 .zip(iter::repeat(cx.attr_span))
35 }
36}
37
38pub(crate) struct UnstableFeatureBoundParser;
39impl<S: Stage> CombineAttributeParser<S> for UnstableFeatureBoundParser {
40 const PATH: &'static [rustc_span::Symbol] = &[sym::unstable_feature_bound];
41 type Item = (Symbol, Span);
42 const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::UnstableFeatureBound(items);
43 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
44 Allow(Target::Fn),
45 Allow(Target::Impl { of_trait: true }),
46 Allow(Target::Trait),
47 ]);
48 const TEMPLATE: AttributeTemplate = template!(Word, List: &["feat1, feat2, ..."]);
49
50 fn extend<'c>(
51 cx: &'c mut AcceptContext<'_, '_, S>,
52 args: &'c ArgParser<'_>,
53 ) -> impl IntoIterator<Item = Self::Item> {
54 if !cx.features().staged_api() {
55 cx.emit_err(session_diagnostics::StabilityOutsideStd { span: cx.attr_span });
56 }
57 parse_unstable(cx, args, <Self as CombineAttributeParser<S>>::PATH[0])
58 .into_iter()
59 .zip(iter::repeat(cx.attr_span))
60 }
61}
62
63pub(crate) struct AllowConstFnUnstableParser;
64impl<S: Stage> CombineAttributeParser<S> for AllowConstFnUnstableParser {
65 const PATH: &[Symbol] = &[sym::rustc_allow_const_fn_unstable];
66 type Item = Symbol;
67 const CONVERT: ConvertFn<Self::Item> =
68 |items, first_span| AttributeKind::AllowConstFnUnstable(items, first_span);
69 const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
70 Allow(Target::Fn),
71 Allow(Target::Method(MethodKind::Inherent)),
72 Allow(Target::Method(MethodKind::Trait { body: false })),
73 Allow(Target::Method(MethodKind::Trait { body: true })),
74 Allow(Target::Method(MethodKind::TraitImpl)),
75 ]);
76 const TEMPLATE: AttributeTemplate = template!(Word, List: &["feat1, feat2, ..."]);
77
78 fn extend<'c>(
79 cx: &'c mut AcceptContext<'_, '_, S>,
80 args: &'c ArgParser<'_>,
81 ) -> impl IntoIterator<Item = Self::Item> + 'c {
82 parse_unstable(cx, args, <Self as CombineAttributeParser<S>>::PATH[0])
83 }
84}
85
86fn parse_unstable<S: Stage>(
87 cx: &AcceptContext<'_, '_, S>,
88 args: &ArgParser<'_>,
89 symbol: Symbol,
90) -> impl IntoIterator<Item = Symbol> {
91 let mut res = Vec::new();
92
93 let Some(list) = args.list() else {
94 cx.emit_err(session_diagnostics::ExpectsFeatureList {
95 span: cx.attr_span,
96 name: symbol.to_ident_string(),
97 });
98 return res;
99 };
100
101 for param in list.mixed() {
102 let param_span = param.span();
103 if let Some(ident) = param.meta_item().and_then(|i| i.path().word()) {
104 res.push(ident.name);
105 } else {
106 cx.emit_err(session_diagnostics::ExpectsFeatures {
107 span: param_span,
108 name: symbol.to_ident_string(),
109 });
110 }
111 }
112
113 res
114}