rustc_attr_parsing/attributes/
allow_unstable.rs

1use std::iter;
2
3use rustc_attr_data_structures::AttributeKind;
4use rustc_feature::{AttributeTemplate, template};
5use rustc_span::{Span, Symbol, sym};
6
7use super::{CombineAttributeParser, ConvertFn};
8use crate::context::{AcceptContext, Stage};
9use crate::parser::ArgParser;
10use crate::session_diagnostics;
11
12pub(crate) struct AllowInternalUnstableParser;
13impl<S: Stage> CombineAttributeParser<S> for AllowInternalUnstableParser {
14    const PATH: &[Symbol] = &[sym::allow_internal_unstable];
15    type Item = (Symbol, Span);
16    const CONVERT: ConvertFn<Self::Item> =
17        |items, span| AttributeKind::AllowInternalUnstable(items, span);
18    const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
19
20    fn extend<'c>(
21        cx: &'c mut AcceptContext<'_, '_, S>,
22        args: &'c 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 AllowConstFnUnstableParser;
31impl<S: Stage> CombineAttributeParser<S> for AllowConstFnUnstableParser {
32    const PATH: &[Symbol] = &[sym::rustc_allow_const_fn_unstable];
33    type Item = Symbol;
34    const CONVERT: ConvertFn<Self::Item> =
35        |items, first_span| AttributeKind::AllowConstFnUnstable(items, first_span);
36    const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
37
38    fn extend<'c>(
39        cx: &'c mut AcceptContext<'_, '_, S>,
40        args: &'c ArgParser<'_>,
41    ) -> impl IntoIterator<Item = Self::Item> + 'c {
42        parse_unstable(cx, args, <Self as CombineAttributeParser<S>>::PATH[0])
43    }
44}
45
46fn parse_unstable<S: Stage>(
47    cx: &AcceptContext<'_, '_, S>,
48    args: &ArgParser<'_>,
49    symbol: Symbol,
50) -> impl IntoIterator<Item = Symbol> {
51    let mut res = Vec::new();
52
53    let Some(list) = args.list() else {
54        cx.emit_err(session_diagnostics::ExpectsFeatureList {
55            span: cx.attr_span,
56            name: symbol.to_ident_string(),
57        });
58        return res;
59    };
60
61    for param in list.mixed() {
62        let param_span = param.span();
63        if let Some(ident) = param.meta_item().and_then(|i| i.path().word()) {
64            res.push(ident.name);
65        } else {
66            cx.emit_err(session_diagnostics::ExpectsFeatures {
67                span: param_span,
68                name: symbol.to_ident_string(),
69            });
70        }
71    }
72
73    res
74}