rustc_attr_parsing/attributes/
allow_unstable.rs

1use std::iter;
2
3use rustc_feature::{AttributeTemplate, template};
4use rustc_hir::attrs::AttributeKind;
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 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 TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
36
37    fn extend<'c>(
38        cx: &'c mut AcceptContext<'_, '_, S>,
39        args: &'c ArgParser<'_>,
40    ) -> impl IntoIterator<Item = Self::Item> {
41        if !cx.features().staged_api() {
42            cx.emit_err(session_diagnostics::StabilityOutsideStd { span: cx.attr_span });
43        }
44        parse_unstable(cx, args, <Self as CombineAttributeParser<S>>::PATH[0])
45            .into_iter()
46            .zip(iter::repeat(cx.attr_span))
47    }
48}
49
50pub(crate) struct AllowConstFnUnstableParser;
51impl<S: Stage> CombineAttributeParser<S> for AllowConstFnUnstableParser {
52    const PATH: &[Symbol] = &[sym::rustc_allow_const_fn_unstable];
53    type Item = Symbol;
54    const CONVERT: ConvertFn<Self::Item> =
55        |items, first_span| AttributeKind::AllowConstFnUnstable(items, first_span);
56    const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
57
58    fn extend<'c>(
59        cx: &'c mut AcceptContext<'_, '_, S>,
60        args: &'c ArgParser<'_>,
61    ) -> impl IntoIterator<Item = Self::Item> + 'c {
62        parse_unstable(cx, args, <Self as CombineAttributeParser<S>>::PATH[0])
63    }
64}
65
66fn parse_unstable<S: Stage>(
67    cx: &AcceptContext<'_, '_, S>,
68    args: &ArgParser<'_>,
69    symbol: Symbol,
70) -> impl IntoIterator<Item = Symbol> {
71    let mut res = Vec::new();
72
73    let Some(list) = args.list() else {
74        cx.emit_err(session_diagnostics::ExpectsFeatureList {
75            span: cx.attr_span,
76            name: symbol.to_ident_string(),
77        });
78        return res;
79    };
80
81    for param in list.mixed() {
82        let param_span = param.span();
83        if let Some(ident) = param.meta_item().and_then(|i| i.path().word()) {
84            res.push(ident.name);
85        } else {
86            cx.emit_err(session_diagnostics::ExpectsFeatures {
87                span: param_span,
88                name: symbol.to_ident_string(),
89            });
90        }
91    }
92
93    res
94}