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