|
1 |
| -use rustc_ast::attr::{AttributeExt, filter_by_name}; |
2 |
| -use rustc_session::Session; |
3 |
| -use rustc_span::{Symbol, sym}; |
| 1 | +use std::iter; |
4 | 2 |
|
| 3 | +use rustc_attr_data_structures::AttributeKind; |
| 4 | +use rustc_span::{Span, Symbol, sym}; |
| 5 | + |
| 6 | +use super::{CombineAttributeParser, ConvertFn}; |
| 7 | +use crate::context::AcceptContext; |
| 8 | +use crate::parser::ArgParser; |
5 | 9 | use crate::session_diagnostics;
|
6 | 10 |
|
7 |
| -pub fn allow_internal_unstable( |
8 |
| - sess: &Session, |
9 |
| - attrs: &[impl AttributeExt], |
10 |
| -) -> impl Iterator<Item = Symbol> { |
11 |
| - allow_unstable(sess, attrs, sym::allow_internal_unstable) |
| 11 | +pub(crate) struct AllowInternalUnstableParser; |
| 12 | +impl CombineAttributeParser for AllowInternalUnstableParser { |
| 13 | + const PATH: &'static [rustc_span::Symbol] = &[sym::allow_internal_unstable]; |
| 14 | + type Item = (Symbol, Span); |
| 15 | + const CONVERT: ConvertFn<Self::Item> = AttributeKind::AllowInternalUnstable; |
| 16 | + |
| 17 | + fn extend<'a>( |
| 18 | + cx: &'a AcceptContext<'a>, |
| 19 | + args: &'a ArgParser<'a>, |
| 20 | + ) -> impl IntoIterator<Item = Self::Item> + 'a { |
| 21 | + parse_unstable(cx, args, Self::PATH[0]).into_iter().zip(iter::repeat(cx.attr_span)) |
| 22 | + } |
12 | 23 | }
|
13 | 24 |
|
14 |
| -pub fn rustc_allow_const_fn_unstable( |
15 |
| - sess: &Session, |
16 |
| - attrs: &[impl AttributeExt], |
17 |
| -) -> impl Iterator<Item = Symbol> { |
18 |
| - allow_unstable(sess, attrs, sym::rustc_allow_const_fn_unstable) |
| 25 | +pub(crate) struct AllowConstFnUnstableParser; |
| 26 | +impl CombineAttributeParser for AllowConstFnUnstableParser { |
| 27 | + const PATH: &'static [rustc_span::Symbol] = &[sym::rustc_allow_const_fn_unstable]; |
| 28 | + type Item = Symbol; |
| 29 | + const CONVERT: ConvertFn<Self::Item> = AttributeKind::AllowConstFnUnstable; |
| 30 | + |
| 31 | + fn extend<'a>( |
| 32 | + cx: &'a AcceptContext<'a>, |
| 33 | + args: &'a ArgParser<'a>, |
| 34 | + ) -> impl IntoIterator<Item = Self::Item> + 'a { |
| 35 | + parse_unstable(cx, args, Self::PATH[0]) |
| 36 | + } |
19 | 37 | }
|
20 | 38 |
|
21 |
| -fn allow_unstable( |
22 |
| - sess: &Session, |
23 |
| - attrs: &[impl AttributeExt], |
| 39 | +fn parse_unstable<'a>( |
| 40 | + cx: &AcceptContext<'_>, |
| 41 | + args: &'a ArgParser<'a>, |
24 | 42 | symbol: Symbol,
|
25 |
| -) -> impl Iterator<Item = Symbol> { |
26 |
| - let attrs = filter_by_name(attrs, symbol); |
27 |
| - let list = attrs |
28 |
| - .filter_map(move |attr| { |
29 |
| - attr.meta_item_list().or_else(|| { |
30 |
| - sess.dcx().emit_err(session_diagnostics::ExpectsFeatureList { |
31 |
| - span: attr.span(), |
32 |
| - name: symbol.to_ident_string(), |
33 |
| - }); |
34 |
| - None |
35 |
| - }) |
36 |
| - }) |
37 |
| - .flatten(); |
38 |
| - |
39 |
| - list.into_iter().filter_map(move |it| { |
40 |
| - let name = it.ident().map(|ident| ident.name); |
41 |
| - if name.is_none() { |
42 |
| - sess.dcx().emit_err(session_diagnostics::ExpectsFeatures { |
43 |
| - span: it.span(), |
| 43 | +) -> impl IntoIterator<Item = Symbol> { |
| 44 | + let mut res = Vec::new(); |
| 45 | + |
| 46 | + let Some(list) = args.list() else { |
| 47 | + cx.emit_err(session_diagnostics::ExpectsFeatureList { |
| 48 | + span: cx.attr_span, |
| 49 | + name: symbol.to_ident_string(), |
| 50 | + }); |
| 51 | + return res; |
| 52 | + }; |
| 53 | + |
| 54 | + for param in list.mixed() { |
| 55 | + let param_span = param.span(); |
| 56 | + if let Some(ident) = param.meta_item().and_then(|i| i.word_without_args()) { |
| 57 | + res.push(ident.name); |
| 58 | + } else { |
| 59 | + cx.emit_err(session_diagnostics::ExpectsFeatures { |
| 60 | + span: param_span, |
44 | 61 | name: symbol.to_ident_string(),
|
45 | 62 | });
|
46 | 63 | }
|
47 |
| - name |
48 |
| - }) |
| 64 | + } |
| 65 | + |
| 66 | + res |
49 | 67 | }
|
0 commit comments