Skip to main content

rustc_attr_parsing/
parser.rs

1//! Parsing of attribute arguments.
2//!
3//! Depending on the attribute parser, an [`ArgParser`] can be used to parse the arguments given to
4//! an attribute. See its documentation for more information.
5//!
6//! This is in essence an (improved) duplicate of `rustc_ast/attr/mod.rs`.
7//! That module is intended to be deleted in its entirety.
8//!
9//! FIXME(jdonszelmann): delete `rustc_ast/attr/mod.rs`
10
11use std::borrow::Borrow;
12use std::fmt::{Debug, Display};
13#[cfg(debug_assertions)]
14use std::sync::atomic::{AtomicBool, Ordering};
15
16use rustc_ast::token::{self, Delimiter, MetaVarKind};
17use rustc_ast::tokenstream::TokenStream;
18use rustc_ast::{
19    AttrArgs, Expr, ExprKind, LitKind, MetaItemLit, Path, PathSegment, StmtKind, UnOp,
20};
21use rustc_ast_pretty::pprust;
22use rustc_errors::{Applicability, Diag, PResult};
23use rustc_hir::{self as hir, AttrPath};
24use rustc_parse::exp;
25use rustc_parse::parser::{ForceCollect, Parser, PathStyle, Recovery, token_descr};
26use rustc_session::errors::create_lit_error;
27use rustc_session::parse::ParseSess;
28use rustc_span::{Ident, Span, Symbol, sym};
29use thin_vec::ThinVec;
30
31use crate::ShouldEmit;
32use crate::session_diagnostics::{
33    AdditionalCommaSuggestion, ExpectedComma, InvalidMetaItem, InvalidMetaItemQuoteIdentSugg,
34    InvalidMetaItemRemoveNegSugg, MetaBadDelim, MetaBadDelimSugg, SuffixedLiteralInAttribute,
35};
36
37#[derive(#[automatically_derived]
impl<P: ::core::clone::Clone + Borrow<Path>> ::core::clone::Clone for
    PathParser<P> {
    #[inline]
    fn clone(&self) -> PathParser<P> {
        PathParser(::core::clone::Clone::clone(&self.0))
    }
}Clone, #[automatically_derived]
impl<P: ::core::fmt::Debug + Borrow<Path>> ::core::fmt::Debug for
    PathParser<P> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "PathParser",
            &&self.0)
    }
}Debug)]
38pub struct PathParser<P: Borrow<Path>>(pub P);
39
40pub type OwnedPathParser = PathParser<Path>;
41pub type RefPathParser<'p> = PathParser<&'p Path>;
42
43impl<P: Borrow<Path>> PathParser<P> {
44    pub fn get_attribute_path(&self) -> hir::AttrPath {
45        AttrPath {
46            segments: self.segments().map(|s| s.name).collect::<Vec<_>>().into_boxed_slice(),
47            span: self.span(),
48        }
49    }
50
51    pub fn segments(&self) -> impl Iterator<Item = &Ident> {
52        self.0.borrow().segments.iter().map(|seg| &seg.ident)
53    }
54
55    pub fn span(&self) -> Span {
56        self.0.borrow().span
57    }
58
59    pub fn len(&self) -> usize {
60        self.0.borrow().segments.len()
61    }
62
63    pub fn segments_is(&self, segments: &[Symbol]) -> bool {
64        self.segments().map(|segment| &segment.name).eq(segments)
65    }
66
67    pub fn word(&self) -> Option<Ident> {
68        (self.len() == 1).then(|| **self.segments().next().as_ref().unwrap())
69    }
70
71    pub fn word_sym(&self) -> Option<Symbol> {
72        self.word().map(|ident| ident.name)
73    }
74
75    /// Asserts that this MetaItem is some specific word.
76    ///
77    /// See [`word`](Self::word) for examples of what a word is.
78    pub fn word_is(&self, sym: Symbol) -> bool {
79        self.word().map(|i| i.name == sym).unwrap_or(false)
80    }
81
82    /// Checks whether the first segments match the givens.
83    ///
84    /// Unlike [`segments_is`](Self::segments_is),
85    /// `self` may contain more segments than the number matched  against.
86    pub fn starts_with(&self, segments: &[Symbol]) -> bool {
87        segments.len() < self.len() && self.segments().zip(segments).all(|(a, b)| a.name == *b)
88    }
89}
90
91impl<P: Borrow<Path>> Display for PathParser<P> {
92    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93        f.write_fmt(format_args!("{0}", pprust::path_to_string(self.0.borrow())))write!(f, "{}", pprust::path_to_string(self.0.borrow()))
94    }
95}
96
97/// Used for parsing attribute arguments.
98///
99/// See also [`AttributeDiagnosticContext`], which is the preferred interface for issuing argument
100/// parsing related diagnostics.
101///
102/// [`AttributeDiagnosticContext`]: crate::context::AttributeDiagnosticContext
103#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ArgParser {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            ArgParser::NoArgs =>
                ::core::fmt::Formatter::write_str(f, "NoArgs"),
            ArgParser::List(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "List",
                    &__self_0),
            ArgParser::NameValue(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "NameValue", &__self_0),
        }
    }
}Debug)]
104#[must_use]
105pub enum ArgParser {
106    NoArgs,
107    List(MetaItemListParser),
108    NameValue(NameValueParser),
109}
110
111impl ArgParser {
112    pub fn span(&self) -> Option<Span> {
113        match self {
114            Self::NoArgs => None,
115            Self::List(l) => Some(l.span),
116            Self::NameValue(n) => Some(n.value_span.with_lo(n.eq_span.lo())),
117        }
118    }
119
120    pub fn from_attr_args<'sess>(
121        value: &AttrArgs,
122        parts: &[Symbol],
123        psess: &'sess ParseSess,
124        should_emit: ShouldEmit,
125        allow_expr_metavar: AllowExprMetavar,
126    ) -> Option<Self> {
127        Some(match value {
128            AttrArgs::Empty => Self::NoArgs,
129            AttrArgs::Delimited(args) => {
130                // Diagnostic attributes can't error if they encounter non meta item syntax.
131                // However, the current syntax for diagnostic attributes is meta item syntax.
132                // Therefore we can substitute with a dummy value on invalid syntax.
133                if #[allow(non_exhaustive_omitted_patterns)] match parts {
    [sym::rustc_dummy] | [sym::diagnostic, ..] => true,
    _ => false,
}matches!(parts, [sym::rustc_dummy] | [sym::diagnostic, ..]) {
134                    match MetaItemListParser::new(
135                        &args.tokens,
136                        args.dspan.entire(),
137                        psess,
138                        ShouldEmit::ErrorsAndLints { recovery: Recovery::Forbidden },
139                        allow_expr_metavar,
140                    ) {
141                        Ok(p) => return Some(ArgParser::List(p)),
142                        Err(e) => {
143                            // We can just dispose of the diagnostic and not bother with a lint,
144                            // because this will look like `#[diagnostic::attr()]` was used. This
145                            // is invalid for all diagnostic attrs, so a lint explaining the proper
146                            // form will be issued later.
147                            e.cancel();
148                            return Some(ArgParser::List(MetaItemListParser {
149                                sub_parsers: ThinVec::new(),
150                                span: args.dspan.entire(),
151                            }));
152                        }
153                    }
154                }
155
156                if args.delim != Delimiter::Parenthesis {
157                    should_emit.emit_err(psess.dcx().create_err(MetaBadDelim {
158                        span: args.dspan.entire(),
159                        sugg: MetaBadDelimSugg { open: args.dspan.open, close: args.dspan.close },
160                    }));
161                    return None;
162                }
163
164                Self::List(
165                    MetaItemListParser::new(
166                        &args.tokens,
167                        args.dspan.entire(),
168                        psess,
169                        should_emit,
170                        allow_expr_metavar,
171                    )
172                    .map_err(|e| should_emit.emit_err(e))
173                    .ok()?,
174                )
175            }
176            AttrArgs::Eq { eq_span, expr } => Self::NameValue(NameValueParser {
177                eq_span: *eq_span,
178                value: expr_to_lit(psess, &expr, expr.span, should_emit)
179                    .map_err(|e| should_emit.emit_err(e))
180                    .ok()??,
181                value_span: expr.span,
182            }),
183        })
184    }
185
186    /// Asserts that this MetaItem is a list
187    ///
188    /// Some examples:
189    ///
190    /// - `#[allow(clippy::complexity)]`: `(clippy::complexity)` is a list
191    /// - `#[rustfmt::skip::macros(target_macro_name)]`: `(target_macro_name)` is a list
192    pub fn as_list(&self) -> Option<&MetaItemListParser> {
193        match self {
194            Self::List(l) => Some(l),
195            Self::NameValue(_) | Self::NoArgs => None,
196        }
197    }
198
199    /// Asserts that this MetaItem is a name-value pair.
200    ///
201    /// Some examples:
202    ///
203    /// - `#[clippy::cyclomatic_complexity = "100"]`: `clippy::cyclomatic_complexity = "100"` is a name value pair,
204    ///   where the name is a path (`clippy::cyclomatic_complexity`). You already checked the path
205    ///   to get an `ArgParser`, so this method will effectively only assert that the `= "100"` is
206    ///   there
207    /// - `#[doc = "hello"]`: `doc = "hello`  is also a name value pair
208    pub fn as_name_value(&self) -> Option<&NameValueParser> {
209        match self {
210            Self::NameValue(n) => Some(n),
211            Self::List(_) | Self::NoArgs => None,
212        }
213    }
214
215    /// Assert that there were no args.
216    /// If there were, get a span to the arguments
217    /// (to pass to [`AttributeDiagnosticContext::expected_no_args`](crate::context::AttributeDiagnosticContext::expected_no_args)).
218    pub fn as_no_args(&self) -> Result<(), Span> {
219        match self {
220            Self::NoArgs => Ok(()),
221            Self::List(args) => Err(args.span),
222            Self::NameValue(args) => Err(args.args_span()),
223        }
224    }
225
226    /// Explicitly ignore the arguments, disarming the arguments-used check
227    pub fn ignore_args(&self) {
228        #[cfg(debug_assertions)]
229        match self {
230            ArgParser::List(list) => {
231                for item in list.mixed() {
232                    item.ignore_args();
233                }
234            }
235            _ => {}
236        }
237    }
238}
239
240/// Inside lists, values could be either literals, or more deeply nested meta items.
241/// This enum represents that.
242///
243/// Choose which one you want using the provided methods.
244#[derive(#[automatically_derived]
impl ::core::fmt::Debug for MetaItemOrLitParser {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            MetaItemOrLitParser::MetaItemParser(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "MetaItemParser", &__self_0),
            MetaItemOrLitParser::Lit(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Lit",
                    &__self_0),
        }
    }
}Debug)]
245pub enum MetaItemOrLitParser {
246    MetaItemParser(MetaItemParser),
247    Lit(MetaItemLit),
248}
249
250impl MetaItemOrLitParser {
251    pub fn parse_single<'sess>(
252        parser: &mut Parser<'sess>,
253        should_emit: ShouldEmit,
254        allow_expr_metavar: AllowExprMetavar,
255    ) -> PResult<'sess, MetaItemOrLitParser> {
256        let mut this = MetaItemListParserContext { parser, should_emit, allow_expr_metavar };
257        this.parse_meta_item_inner()
258    }
259
260    pub fn span(&self) -> Span {
261        match self {
262            MetaItemOrLitParser::MetaItemParser(generic_meta_item_parser) => {
263                generic_meta_item_parser.span()
264            }
265            MetaItemOrLitParser::Lit(meta_item_lit) => meta_item_lit.span,
266        }
267    }
268
269    pub fn as_lit(&self) -> Option<&MetaItemLit> {
270        match self {
271            MetaItemOrLitParser::Lit(meta_item_lit) => Some(meta_item_lit),
272            MetaItemOrLitParser::MetaItemParser(_) => None,
273        }
274    }
275
276    pub fn meta_item(&self) -> Option<&MetaItemParser> {
277        match self {
278            MetaItemOrLitParser::MetaItemParser(parser) => Some(parser),
279            MetaItemOrLitParser::Lit(_) => None,
280        }
281    }
282
283    /// Returns some if this `MetaItemOrLitParser` is a `MetaItem` with no arguments
284    pub fn meta_item_no_args(&self) -> Option<&MetaItemParser> {
285        let meta_item = self.meta_item()?;
286        match meta_item.args().as_no_args() {
287            Ok(_) => Some(meta_item),
288            Err(_) => None,
289        }
290    }
291
292    /// Explicitly ignore the arguments, disarming the arguments-used check
293    pub fn ignore_args(&self) {
294        #[cfg(debug_assertions)]
295        match self {
296            MetaItemOrLitParser::MetaItemParser(meta_item) => {
297                meta_item.ignore_args();
298            }
299            MetaItemOrLitParser::Lit(_) => {}
300        }
301    }
302}
303
304// FIXME(scrabsha): once #155696 is merged, update this and mention the higher-level APIs.
305/// Utility that deconstructs a MetaItem into usable parts.
306///
307/// MetaItems are syntactically extremely flexible, but specific attributes want to parse
308/// them in custom, more restricted ways. This can be done using this struct.
309///
310/// MetaItems consist of some path, and some args. The args could be empty. In other words:
311///
312/// - `name` -> args are empty
313/// - `name(...)` -> args are a [`list`](ArgParser::as_list), which is the bit between the
314///   parentheses
315/// - `name = value`-> arg is [`name_value`](ArgParser::as_name_value), where the argument is the
316///   `= value` part
317///
318/// The syntax of MetaItems can be found at <https://doc.rust-lang.org/reference/attributes.html>
319pub struct MetaItemParser {
320    path: OwnedPathParser,
321    args: ArgParser,
322
323    /// Whether the `args` of this meta item have been looked at.
324    /// This is tracked because if the arguments of a `MetaItemParser` are ignored, this is probably a mistake
325    #[cfg(debug_assertions)]
326    args_checked: AtomicBool,
327}
328
329impl Debug for MetaItemParser {
330    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
331        f.debug_struct("MetaItemParser")
332            .field("path", &self.path)
333            .field("args", &self.args)
334            .finish()
335    }
336}
337
338impl MetaItemParser {
339    /// For a single-segment meta item, returns its name; otherwise, returns `None`.
340    pub fn ident(&self) -> Option<Ident> {
341        if let [PathSegment { ident, .. }] = self.path.0.segments[..] { Some(ident) } else { None }
342    }
343
344    pub fn span(&self) -> Span {
345        if let Some(other) = self.args.span() {
346            self.path.borrow().span().with_hi(other.hi())
347        } else {
348            self.path.borrow().span()
349        }
350    }
351
352    /// Gets just the path, without the args. Some examples:
353    ///
354    /// - `#[rustfmt::skip]`: `rustfmt::skip` is a path
355    /// - `#[allow(clippy::complexity)]`: `clippy::complexity` is a path
356    /// - `#[inline]`: `inline` is a single segment path
357    pub fn path(&self) -> &OwnedPathParser {
358        &self.path
359    }
360
361    /// Gets just the args parser, without caring about the path.
362    pub fn args(&self) -> &ArgParser {
363        #[cfg(debug_assertions)]
364        self.args_checked.store(true, Ordering::Relaxed);
365        &self.args
366    }
367
368    /// Asserts that this MetaItem starts with a word, or single segment path.
369    ///
370    /// Some examples:
371    /// - `#[inline]`: `inline` is a word
372    /// - `#[rustfmt::skip]`: `rustfmt::skip` is a path,
373    ///   and not a word and should instead be parsed using [`path`](Self::path)
374    pub fn word_is(&self, sym: Symbol) -> Option<&ArgParser> {
375        self.path().word_is(sym).then(|| self.args())
376    }
377
378    /// Explicitly ignore the arguments, disarming the arguments-used check
379    pub fn ignore_args(&self) {
380        self.args().ignore_args();
381    }
382
383    #[cfg(debug_assertions)]
384    pub fn are_args_checked(&self) -> bool {
385        self.args_checked.load(Ordering::Relaxed)
386    }
387}
388
389#[derive(#[automatically_derived]
impl ::core::clone::Clone for NameValueParser {
    #[inline]
    fn clone(&self) -> NameValueParser {
        NameValueParser {
            eq_span: ::core::clone::Clone::clone(&self.eq_span),
            value: ::core::clone::Clone::clone(&self.value),
            value_span: ::core::clone::Clone::clone(&self.value_span),
        }
    }
}Clone)]
390pub struct NameValueParser {
391    pub eq_span: Span,
392    value: MetaItemLit,
393    pub value_span: Span,
394}
395
396impl Debug for NameValueParser {
397    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
398        f.debug_struct("NameValueParser")
399            .field("eq_span", &self.eq_span)
400            .field("value", &self.value)
401            .field("value_span", &self.value_span)
402            .finish()
403    }
404}
405
406impl NameValueParser {
407    pub fn value_as_lit(&self) -> &MetaItemLit {
408        &self.value
409    }
410
411    pub fn value_as_str(&self) -> Option<Symbol> {
412        self.value_as_lit().kind.str()
413    }
414
415    /// If the value is a string literal, it will return its value associated with its span (an
416    /// `Ident` in short).
417    pub fn value_as_ident(&self) -> Option<Ident> {
418        let meta_item = self.value_as_lit();
419        meta_item.kind.str().map(|name| Ident { name, span: meta_item.span })
420    }
421
422    pub fn args_span(&self) -> Span {
423        self.eq_span.to(self.value_span)
424    }
425}
426
427fn expr_to_lit<'sess>(
428    psess: &'sess ParseSess,
429    expr: &Expr,
430    span: Span,
431    should_emit: ShouldEmit,
432) -> PResult<'sess, Option<MetaItemLit>> {
433    if let ExprKind::Lit(token_lit) = expr.kind {
434        let res = MetaItemLit::from_token_lit(token_lit, expr.span);
435        match res {
436            Ok(lit) => {
437                if token_lit.suffix.is_some() {
438                    Err(psess.dcx().create_err(SuffixedLiteralInAttribute { span: lit.span }))
439                } else {
440                    if lit.kind.is_unsuffixed() {
441                        Ok(Some(lit))
442                    } else {
443                        Err(psess.dcx().create_err(SuffixedLiteralInAttribute { span: lit.span }))
444                    }
445                }
446            }
447            Err(err) => {
448                let err = create_lit_error(psess, err, token_lit, expr.span);
449                if #[allow(non_exhaustive_omitted_patterns)] match should_emit {
    ShouldEmit::ErrorsAndLints { recovery: Recovery::Forbidden } => true,
    _ => false,
}matches!(
450                    should_emit,
451                    ShouldEmit::ErrorsAndLints { recovery: Recovery::Forbidden }
452                ) {
453                    Err(err)
454                } else {
455                    let lit = MetaItemLit {
456                        symbol: token_lit.symbol,
457                        suffix: token_lit.suffix,
458                        kind: LitKind::Err(err.emit()),
459                        span: expr.span,
460                    };
461                    Ok(Some(lit))
462                }
463            }
464        }
465    } else {
466        if #[allow(non_exhaustive_omitted_patterns)] match should_emit {
    ShouldEmit::Nothing => true,
    _ => false,
}matches!(should_emit, ShouldEmit::Nothing) || #[allow(non_exhaustive_omitted_patterns)] match expr.kind {
    ExprKind::Err(_) => true,
    _ => false,
}matches!(expr.kind, ExprKind::Err(_)) {
467            return Ok(None);
468        }
469
470        // Example cases:
471        // - `#[foo = 1+1]`: results in `ast::ExprKind::BinOp`.
472        // - `#[foo = include_str!("nonexistent-file.rs")]`:
473        //   results in `ast::ExprKind::Err`.
474        let msg = "attribute value must be a literal";
475        let mut err = psess.dcx().struct_span_err(span, msg);
476
477        // Suggest adding quotation marks to turn an identifier into a string literal
478        if let ExprKind::Path(None, ref path) = expr.kind
479            && let [segment] = path.segments.as_slice()
480        {
481            err.span_suggestion(
482                expr.span,
483                "try adding quotation marks",
484                &::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("\"{0}\"", segment.ident))
    })format!("\"{}\"", segment.ident),
485                Applicability::MaybeIncorrect,
486            );
487        }
488
489        Err(err)
490    }
491}
492
493/// Whether expansions of `expr` metavariables from decrarative macros
494/// are permitted. Used when parsing meta items; currently, only `cfg` predicates
495/// enable this option
496#[derive(#[automatically_derived]
impl ::core::clone::Clone for AllowExprMetavar {
    #[inline]
    fn clone(&self) -> AllowExprMetavar { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for AllowExprMetavar { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for AllowExprMetavar {
    #[inline]
    fn eq(&self, other: &AllowExprMetavar) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for AllowExprMetavar {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq)]
497pub enum AllowExprMetavar {
498    No,
499    Yes,
500}
501
502struct MetaItemListParserContext<'a, 'sess> {
503    parser: &'a mut Parser<'sess>,
504    should_emit: ShouldEmit,
505    allow_expr_metavar: AllowExprMetavar,
506}
507
508impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
509    fn parse_unsuffixed_meta_item_lit(&mut self) -> PResult<'sess, MetaItemLit> {
510        let Some(token_lit) = self.parser.eat_token_lit() else { return Err(self.expected_lit()) };
511        self.unsuffixed_meta_item_from_lit(token_lit)
512    }
513
514    fn unsuffixed_meta_item_from_lit(
515        &mut self,
516        token_lit: token::Lit,
517    ) -> PResult<'sess, MetaItemLit> {
518        let lit = match MetaItemLit::from_token_lit(token_lit, self.parser.prev_token.span) {
519            Ok(lit) => lit,
520            Err(err) => {
521                return Err(create_lit_error(
522                    &self.parser.psess,
523                    err,
524                    token_lit,
525                    self.parser.prev_token_uninterpolated_span(),
526                ));
527            }
528        };
529
530        if !lit.kind.is_unsuffixed() {
531            // Emit error and continue, we can still parse the attribute as if the suffix isn't there
532            let err = self.parser.dcx().create_err(SuffixedLiteralInAttribute { span: lit.span });
533            if #[allow(non_exhaustive_omitted_patterns)] match self.should_emit {
    ShouldEmit::ErrorsAndLints { recovery: Recovery::Forbidden } => true,
    _ => false,
}matches!(
534                self.should_emit,
535                ShouldEmit::ErrorsAndLints { recovery: Recovery::Forbidden }
536            ) {
537                return Err(err);
538            } else {
539                self.should_emit.emit_err(err)
540            };
541        }
542
543        Ok(lit)
544    }
545
546    fn parse_meta_item(&mut self) -> PResult<'sess, MetaItemParser> {
547        if let Some(metavar) = self.parser.token.is_metavar_seq() {
548            match (metavar, self.allow_expr_metavar) {
549                (kind @ MetaVarKind::Expr { .. }, AllowExprMetavar::Yes) => {
550                    return self
551                        .parser
552                        .eat_metavar_seq(kind, |this| {
553                            MetaItemListParserContext {
554                                parser: this,
555                                should_emit: self.should_emit,
556                                allow_expr_metavar: AllowExprMetavar::Yes,
557                            }
558                            .parse_meta_item()
559                        })
560                        .ok_or_else(|| {
561                            self.parser.unexpected_any::<core::convert::Infallible>().unwrap_err()
562                        });
563                }
564                (MetaVarKind::Meta { has_meta_form }, _) => {
565                    return if has_meta_form {
566                        let attr_item = self
567                            .parser
568                            .eat_metavar_seq(MetaVarKind::Meta { has_meta_form: true }, |this| {
569                                MetaItemListParserContext {
570                                    parser: this,
571                                    should_emit: self.should_emit,
572                                    allow_expr_metavar: self.allow_expr_metavar,
573                                }
574                                .parse_meta_item()
575                            })
576                            .unwrap();
577                        Ok(attr_item)
578                    } else {
579                        self.parser.unexpected_any()
580                    };
581                }
582                _ => {}
583            }
584        }
585
586        let path = self.parser.parse_path(PathStyle::Mod)?;
587
588        // Check style of arguments that this meta item has
589        let args = if self.parser.check(::rustc_parse::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: ::rustc_parse::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
590            let start = self.parser.token.span;
591            let (sub_parsers, _) = self.parser.parse_paren_comma_seq(|parser| {
592                MetaItemListParserContext {
593                    parser,
594                    should_emit: self.should_emit,
595                    allow_expr_metavar: self.allow_expr_metavar,
596                }
597                .parse_meta_item_inner()
598            })?;
599            let end = self.parser.prev_token.span;
600            ArgParser::List(MetaItemListParser { sub_parsers, span: start.with_hi(end.hi()) })
601        } else if self.parser.eat(::rustc_parse::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Eq,
    token_type: ::rustc_parse::parser::token_type::TokenType::Eq,
}exp!(Eq)) {
602            let eq_span = self.parser.prev_token.span;
603            let value = self.parse_unsuffixed_meta_item_lit()?;
604
605            ArgParser::NameValue(NameValueParser { eq_span, value, value_span: value.span })
606        } else {
607            ArgParser::NoArgs
608        };
609
610        Ok(MetaItemParser {
611            path: PathParser(path),
612            args,
613            #[cfg(debug_assertions)]
614            args_checked: AtomicBool::new(false),
615        })
616    }
617
618    fn parse_meta_item_inner(&mut self) -> PResult<'sess, MetaItemOrLitParser> {
619        if let Some(token_lit) = self.parser.eat_token_lit() {
620            // If a literal token is parsed, we commit to parsing a MetaItemLit for better errors
621            Ok(MetaItemOrLitParser::Lit(self.unsuffixed_meta_item_from_lit(token_lit)?))
622        } else {
623            let prev_pros = self.parser.approx_token_stream_pos();
624            match self.parse_meta_item() {
625                Ok(item) => Ok(MetaItemOrLitParser::MetaItemParser(item)),
626                Err(err) => {
627                    // If `parse_attr_item` made any progress, it likely has a more precise error we should prefer
628                    // If it didn't make progress we use the `expected_lit` from below
629                    if self.parser.approx_token_stream_pos() != prev_pros {
630                        Err(err)
631                    } else {
632                        err.cancel();
633                        Err(self.expected_lit())
634                    }
635                }
636            }
637        }
638    }
639
640    fn expected_lit(&mut self) -> Diag<'sess> {
641        let mut err = InvalidMetaItem {
642            span: self.parser.token.span,
643            descr: token_descr(&self.parser.token),
644            quote_ident_sugg: None,
645            remove_neg_sugg: None,
646            label: None,
647        };
648
649        if let token::OpenInvisible(_) = self.parser.token.kind {
650            // Do not attempt to suggest anything when encountered as part of a macro expansion.
651            return self.parser.dcx().create_err(err);
652        }
653
654        if let ShouldEmit::ErrorsAndLints { recovery: Recovery::Forbidden } = self.should_emit {
655            // Do not attempt to suggest anything in `Recovery::Forbidden` mode.
656            // Malformed diagnostic-attr arguments that start with an `if` expression can lead to
657            // an ICE (https://github.com/rust-lang/rust/issues/152744), because callers may cancel the `InvalidMetaItem` error.
658            return self.parser.dcx().create_err(err);
659        }
660
661        // Suggest quoting idents, e.g. in `#[cfg(key = value)]`. We don't use `Token::ident` and
662        // don't `uninterpolate` the token to avoid suggesting anything butchered or questionable
663        // when macro metavariables are involved.
664        let snapshot = self.parser.create_snapshot_for_diagnostic();
665        let stmt = self.parser.parse_stmt_without_recovery(false, ForceCollect::No, false);
666        match stmt {
667            Ok(Some(stmt)) => {
668                // The user tried to write something like
669                // `#[deprecated(note = concat!("a", "b"))]`.
670                err.descr = stmt.kind.descr().to_string();
671                err.label = Some(stmt.span);
672                err.span = stmt.span;
673                if let StmtKind::Expr(expr) = &stmt.kind
674                    && let ExprKind::Unary(UnOp::Neg, val) = &expr.kind
675                    && let ExprKind::Lit(_) = val.kind
676                {
677                    err.remove_neg_sugg = Some(InvalidMetaItemRemoveNegSugg {
678                        negative_sign: expr.span.until(val.span),
679                    });
680                } else if let StmtKind::Expr(expr) = &stmt.kind
681                    && let ExprKind::Path(None, Path { segments, .. }) = &expr.kind
682                    && segments.len() == 1
683                {
684                    while let token::Ident(..) | token::Literal(_) | token::Dot =
685                        self.parser.token.kind
686                    {
687                        // We've got a word, so we try to consume the rest of a potential sentence.
688                        // We include `.` to correctly handle things like `A sentence here.`.
689                        self.parser.bump();
690                    }
691                    err.quote_ident_sugg = Some(InvalidMetaItemQuoteIdentSugg {
692                        before: expr.span.shrink_to_lo(),
693                        after: self.parser.prev_token.span.shrink_to_hi(),
694                    });
695                }
696            }
697            Ok(None) => {}
698            Err(e) => {
699                e.cancel();
700                self.parser.restore_snapshot(snapshot);
701            }
702        }
703
704        self.parser.dcx().create_err(err)
705    }
706
707    fn should_continue_parsing_meta_items(&mut self) -> Result<bool, Diag<'sess>> {
708        if self.parser.eat(::rustc_parse::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: ::rustc_parse::parser::token_type::TokenType::Comma,
}exp!(Comma)) {
709            return Ok(true);
710        } else if self.parser.token == token::Eof {
711            return Ok(false);
712        }
713
714        let mut snapshot = self.parser.create_snapshot_for_diagnostic();
715        if #[allow(non_exhaustive_omitted_patterns)] match self.should_emit {
    ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed } => true,
    _ => false,
}matches!(self.should_emit, ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed }) {
716            let mut missing_commas = ThinVec::new();
717            let mut found_comma = false;
718            while self.parser.token != token::Eof {
719                let span = self.parser.prev_token.span.shrink_to_hi();
720                self.should_emit = ShouldEmit::Nothing;
721                match self.parse_meta_item_inner() {
722                    Ok(_) => {
723                        if !found_comma {
724                            missing_commas.push(span);
725                        }
726                    }
727                    Err(e) => {
728                        e.cancel();
729                        break;
730                    }
731                }
732                found_comma = self.parser.eat(::rustc_parse::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: ::rustc_parse::parser::token_type::TokenType::Comma,
}exp!(Comma));
733            }
734
735            let mut missing_commas = missing_commas.into_iter();
736            if let Some(span) = missing_commas.next() {
737                let additional =
738                    missing_commas.map(|span| AdditionalCommaSuggestion { span }).collect();
739                return Err(self.parser.dcx().create_err(ExpectedComma { span, additional }));
740            }
741        }
742        snapshot.unexpected_any()
743    }
744
745    fn parse(
746        tokens: TokenStream,
747        psess: &'sess ParseSess,
748        span: Span,
749        should_emit: ShouldEmit,
750        allow_expr_metavar: AllowExprMetavar,
751    ) -> PResult<'sess, MetaItemListParser> {
752        let mut parser = Parser::new(psess, tokens, None);
753        if let ShouldEmit::ErrorsAndLints { recovery } = should_emit {
754            parser = parser.recovery(recovery);
755        }
756
757        let mut this =
758            MetaItemListParserContext { parser: &mut parser, should_emit, allow_expr_metavar };
759
760        // Presumably, the majority of the time there will only be one attr.
761        let mut sub_parsers = ThinVec::with_capacity(1);
762        while this.parser.token != token::Eof {
763            sub_parsers.push(this.parse_meta_item_inner()?);
764
765            if !this.should_continue_parsing_meta_items()? {
766                break;
767            }
768        }
769
770        Ok(MetaItemListParser { sub_parsers, span })
771    }
772}
773
774#[derive(#[automatically_derived]
impl ::core::fmt::Debug for MetaItemListParser {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "MetaItemListParser", "sub_parsers", &self.sub_parsers, "span",
            &&self.span)
    }
}Debug)]
775pub struct MetaItemListParser {
776    sub_parsers: ThinVec<MetaItemOrLitParser>,
777    pub span: Span,
778}
779
780impl MetaItemListParser {
781    pub(crate) fn new<'sess>(
782        tokens: &TokenStream,
783        span: Span,
784        psess: &'sess ParseSess,
785        should_emit: ShouldEmit,
786        allow_expr_metavar: AllowExprMetavar,
787    ) -> Result<Self, Diag<'sess>> {
788        MetaItemListParserContext::parse(
789            tokens.clone(),
790            psess,
791            span,
792            should_emit,
793            allow_expr_metavar,
794        )
795    }
796
797    /// Lets you pick and choose as what you want to parse each element in the list
798    pub fn mixed(&self) -> impl Iterator<Item = &MetaItemOrLitParser> {
799        self.sub_parsers.iter()
800    }
801
802    pub fn len(&self) -> usize {
803        self.sub_parsers.len()
804    }
805
806    pub fn is_empty(&self) -> bool {
807        self.len() == 0
808    }
809
810    /// Returns Some if the list contains only a single element.
811    ///
812    /// Inside the Some is the parser to parse this single element.
813    pub fn as_single(&self) -> Option<&MetaItemOrLitParser> {
814        let mut iter = self.mixed();
815        iter.next().filter(|_| iter.next().is_none())
816    }
817}