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> = AttributeKind::AllowInternalUnstable;
17    const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
18
19    fn extend<'c>(
20        cx: &'c mut AcceptContext<'_, '_, S>,
21        args: &'c ArgParser<'_>,
22    ) -> impl IntoIterator<Item = Self::Item> {
23        parse_unstable(cx, args, <Self as CombineAttributeParser<S>>::PATH[0])
24            .into_iter()
25            .zip(iter::repeat(cx.attr_span))
26    }
27}
28
29pub(crate) struct AllowConstFnUnstableParser;
30impl<S: Stage> CombineAttributeParser<S> for AllowConstFnUnstableParser {
31    const PATH: &[Symbol] = &[sym::rustc_allow_const_fn_unstable];
32    type Item = Symbol;
33    const CONVERT: ConvertFn<Self::Item> = AttributeKind::AllowConstFnUnstable;
34    const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
35
36    fn extend<'c>(
37        cx: &'c mut AcceptContext<'_, '_, S>,
38        args: &'c ArgParser<'_>,
39    ) -> impl IntoIterator<Item = Self::Item> + 'c {
40        parse_unstable(cx, args, <Self as CombineAttributeParser<S>>::PATH[0])
41    }
42}
43
44fn parse_unstable<S: Stage>(
45    cx: &AcceptContext<'_, '_, S>,
46    args: &ArgParser<'_>,
47    symbol: Symbol,
48) -> impl IntoIterator<Item = Symbol> {
49    let mut res = Vec::new();
50
51    let Some(list) = args.list() else {
52        cx.emit_err(session_diagnostics::ExpectsFeatureList {
53            span: cx.attr_span,
54            name: symbol.to_ident_string(),
55        });
56        return res;
57    };
58
59    for param in list.mixed() {
60        let param_span = param.span();
61        if let Some(ident) = param.meta_item().and_then(|i| i.path().word()) {
62            res.push(ident.name);
63        } else {
64            cx.emit_err(session_diagnostics::ExpectsFeatures {
65                span: param_span,
66                name: symbol.to_ident_string(),
67            });
68        }
69    }
70
71    res
72}