Skip to main content

rustc_parse/parser/
mod.rs

1pub mod attr;
2mod attr_wrapper;
3mod diagnostics;
4mod expr;
5mod generics;
6mod item;
7mod nonterminal;
8mod pat;
9mod path;
10mod stmt;
11pub mod token_type;
12mod ty;
13
14// Parsers for non-functionlike builtin macros are defined in rustc_parse so they can be used by
15// both rustc_builtin_macros and rustfmt.
16pub mod asm;
17pub mod cfg_select;
18
19use std::{fmt, mem, slice};
20
21use attr_wrapper::{AttrWrapper, UsePreAttrPos};
22pub use diagnostics::AttemptLocalParseRecovery;
23// Public to use it for custom `if` expressions in rustfmt forks like https://github.com/tucant/rustfmt
24pub use expr::LetChainsPolicy;
25pub(crate) use item::{FnContext, FnParseMode};
26pub use pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
27pub use path::PathStyle;
28use rustc_ast::token::{
29    self, IdentIsRaw, InvisibleOrigin, MetaVarKind, NtExprKind, NtPatKind, Token, TokenKind,
30};
31use rustc_ast::tokenstream::{
32    ParserRange, ParserReplacement, Spacing, TokenCursor, TokenStream, TokenTree, TokenTreeCursor,
33};
34use rustc_ast::util::case::Case;
35use rustc_ast::util::classify;
36use rustc_ast::{
37    self as ast, AnonConst, AttrArgs, AttrId, BinOpKind, ByRef, Const, CoroutineKind,
38    DUMMY_NODE_ID, DelimArgs, Expr, ExprKind, Extern, HasAttrs, HasTokens, ImplRestriction,
39    MgcaDisambiguation, Mutability, Recovered, RestrictionKind, Safety, StrLit, Visibility,
40    VisibilityKind,
41};
42use rustc_ast_pretty::pprust;
43use rustc_data_structures::debug_assert_matches;
44use rustc_data_structures::fx::FxHashMap;
45use rustc_errors::{Applicability, Diag, FatalError, MultiSpan, PResult};
46use rustc_index::interval::IntervalSet;
47use rustc_session::parse::ParseSess;
48use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
49use thin_vec::ThinVec;
50use token_type::TokenTypeSet;
51pub use token_type::{ExpKeywordPair, ExpTokenPair, TokenType};
52use tracing::debug;
53
54use crate::errors::{
55    self, IncorrectImplRestriction, IncorrectVisibilityRestriction, NonStringAbiLiteral,
56    TokenDescription,
57};
58use crate::exp;
59
60#[cfg(test)]
61mod tests;
62
63// Ideally, these tests would be in `rustc_ast`. But they depend on having a
64// parser, so they are here.
65#[cfg(test)]
66mod tokenstream {
67    mod tests;
68}
69
70bitflags::bitflags! {
71    /// Restrictions applied while parsing.
72    ///
73    /// The parser maintains a bitset of restrictions it will honor while
74    /// parsing. This is essentially used as a way of tracking state of what
75    /// is being parsed and to change behavior based on that.
76    #[derive(#[automatically_derived]
impl ::core::clone::Clone for Restrictions {
    #[inline]
    fn clone(&self) -> Restrictions {
        let _:
                ::core::clone::AssertParamIsClone<<Restrictions as
                ::bitflags::__private::PublicFlags>::Internal>;
        *self
    }
}
impl Restrictions {
    #[doc = r" Restricts expressions for use in statement position."]
    #[doc = r""]
    #[doc =
    r" When expressions are used in various places, like statements or"]
    #[doc =
    r" match arms, this is used to stop parsing once certain tokens are"]
    #[doc = r" reached."]
    #[doc = r""]
    #[doc =
    r" For example, `if true {} & 1` with `STMT_EXPR` in effect is parsed"]
    #[doc =
    r" as two separate expression statements (`if` and a reference to 1)."]
    #[doc =
    r" Otherwise it is parsed as a bitwise AND where `if` is on the left"]
    #[doc = r" and 1 is on the right."]
    #[allow(deprecated, non_upper_case_globals,)]
    pub const STMT_EXPR: Self = Self::from_bits_retain(1 << 0);
    #[doc = r" Do not allow struct literals."]
    #[doc = r""]
    #[doc =
    r" There are several places in the grammar where we don't want to"]
    #[doc = r" allow struct literals because they can require lookahead, or"]
    #[doc = r" otherwise could be ambiguous or cause confusion. For example,"]
    #[doc =
    r" `if Foo {} {}` isn't clear if it is `Foo{}` struct literal, or"]
    #[doc = r" just `Foo` is the condition, followed by a consequent block,"]
    #[doc = r" followed by an empty block."]
    #[doc = r""]
    #[doc =
    r" See [RFC 92](https://rust-lang.github.io/rfcs/0092-struct-grammar.html)."]
    #[allow(deprecated, non_upper_case_globals,)]
    pub const NO_STRUCT_LITERAL: Self = Self::from_bits_retain(1 << 1);
    #[doc =
    r" Used to provide better error messages for const generic arguments."]
    #[doc = r""]
    #[doc =
    r" An un-braced const generic argument is limited to a very small"]
    #[doc =
    r" subset of expressions. This is used to detect the situation where"]
    #[doc =
    r" an expression outside of that subset is used, and to suggest to"]
    #[doc = r" wrap the expression in braces."]
    #[allow(deprecated, non_upper_case_globals,)]
    pub const CONST_EXPR: Self = Self::from_bits_retain(1 << 2);
    #[doc = r" Allows `let` expressions."]
    #[doc = r""]
    #[doc =
    r" `let pattern = scrutinee` is parsed as an expression, but it is"]
    #[doc = r" only allowed in let chains (`if` and `while` conditions)."]
    #[doc =
    r" Otherwise it is not an expression (note that `let` in statement"]
    #[doc =
    r" positions is treated as a `StmtKind::Let` statement, which has a"]
    #[doc = r" slightly different grammar)."]
    #[allow(deprecated, non_upper_case_globals,)]
    pub const ALLOW_LET: Self = Self::from_bits_retain(1 << 3);
    #[doc = r" Used to detect a missing `=>` in a match guard."]
    #[doc = r""]
    #[doc =
    r" This is used for error handling in a match guard to give a better"]
    #[doc =
    r" error message if the `=>` is missing. It is set when parsing the"]
    #[doc = r" guard expression."]
    #[allow(deprecated, non_upper_case_globals,)]
    pub const IN_IF_GUARD: Self = Self::from_bits_retain(1 << 4);
    #[doc = r" Used to detect the incorrect use of expressions in patterns."]
    #[doc = r""]
    #[doc =
    r" This is used for error handling while parsing a pattern. During"]
    #[doc =
    r" error recovery, this will be set to try to parse the pattern as an"]
    #[doc =
    r" expression, but halts parsing the expression when reaching certain"]
    #[doc = r" tokens like `=`."]
    #[allow(deprecated, non_upper_case_globals,)]
    pub const IS_PAT: Self = Self::from_bits_retain(1 << 5);
}
impl ::bitflags::Flags for Restrictions {
    const FLAGS: &'static [::bitflags::Flag<Restrictions>] =
        &[{

                        #[allow(deprecated, non_upper_case_globals,)]
                        ::bitflags::Flag::new("STMT_EXPR", Restrictions::STMT_EXPR)
                    },
                    {

                        #[allow(deprecated, non_upper_case_globals,)]
                        ::bitflags::Flag::new("NO_STRUCT_LITERAL",
                            Restrictions::NO_STRUCT_LITERAL)
                    },
                    {

                        #[allow(deprecated, non_upper_case_globals,)]
                        ::bitflags::Flag::new("CONST_EXPR",
                            Restrictions::CONST_EXPR)
                    },
                    {

                        #[allow(deprecated, non_upper_case_globals,)]
                        ::bitflags::Flag::new("ALLOW_LET", Restrictions::ALLOW_LET)
                    },
                    {

                        #[allow(deprecated, non_upper_case_globals,)]
                        ::bitflags::Flag::new("IN_IF_GUARD",
                            Restrictions::IN_IF_GUARD)
                    },
                    {

                        #[allow(deprecated, non_upper_case_globals,)]
                        ::bitflags::Flag::new("IS_PAT", Restrictions::IS_PAT)
                    }];
    type Bits = u8;
    fn bits(&self) -> u8 { Restrictions::bits(self) }
    fn from_bits_retain(bits: u8) -> Restrictions {
        Restrictions::from_bits_retain(bits)
    }
}
#[allow(dead_code, deprecated, unused_doc_comments, unused_attributes,
unused_mut, unused_imports, non_upper_case_globals, clippy ::
assign_op_pattern, clippy :: indexing_slicing, clippy :: same_name_method,
clippy :: iter_without_into_iter,)]
const _: () =
    {
        #[repr(transparent)]
        struct InternalBitFlags(u8);
        #[automatically_derived]
        #[doc(hidden)]
        unsafe impl ::core::clone::TrivialClone for InternalBitFlags { }
        #[automatically_derived]
        impl ::core::clone::Clone for InternalBitFlags {
            #[inline]
            fn clone(&self) -> InternalBitFlags {
                let _: ::core::clone::AssertParamIsClone<u8>;
                *self
            }
        }
        #[automatically_derived]
        impl ::core::marker::Copy for InternalBitFlags { }
        #[automatically_derived]
        impl ::core::marker::StructuralPartialEq for InternalBitFlags { }
        #[automatically_derived]
        impl ::core::cmp::PartialEq for InternalBitFlags {
            #[inline]
            fn eq(&self, other: &InternalBitFlags) -> bool {
                self.0 == other.0
            }
        }
        #[automatically_derived]
        impl ::core::cmp::Eq for InternalBitFlags {
            #[inline]
            #[doc(hidden)]
            #[coverage(off)]
            fn assert_fields_are_eq(&self) {
                let _: ::core::cmp::AssertParamIsEq<u8>;
            }
        }
        #[automatically_derived]
        impl ::core::cmp::PartialOrd for InternalBitFlags {
            #[inline]
            fn partial_cmp(&self, other: &InternalBitFlags)
                -> ::core::option::Option<::core::cmp::Ordering> {
                ::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
            }
        }
        #[automatically_derived]
        impl ::core::cmp::Ord for InternalBitFlags {
            #[inline]
            fn cmp(&self, other: &InternalBitFlags) -> ::core::cmp::Ordering {
                ::core::cmp::Ord::cmp(&self.0, &other.0)
            }
        }
        #[automatically_derived]
        impl ::core::hash::Hash for InternalBitFlags {
            #[inline]
            fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
                ::core::hash::Hash::hash(&self.0, state)
            }
        }
        impl ::bitflags::__private::PublicFlags for Restrictions {
            type Primitive = u8;
            type Internal = InternalBitFlags;
        }
        impl ::bitflags::__private::core::default::Default for
            InternalBitFlags {
            #[inline]
            fn default() -> Self { InternalBitFlags::empty() }
        }
        impl ::bitflags::__private::core::fmt::Debug for InternalBitFlags {
            fn fmt(&self,
                f: &mut ::bitflags::__private::core::fmt::Formatter<'_>)
                -> ::bitflags::__private::core::fmt::Result {
                if self.is_empty() {
                    f.write_fmt(format_args!("{0:#x}",
                            <u8 as ::bitflags::Bits>::EMPTY))
                } else {
                    ::bitflags::__private::core::fmt::Display::fmt(self, f)
                }
            }
        }
        impl ::bitflags::__private::core::fmt::Display for InternalBitFlags {
            fn fmt(&self,
                f: &mut ::bitflags::__private::core::fmt::Formatter<'_>)
                -> ::bitflags::__private::core::fmt::Result {
                ::bitflags::parser::to_writer(&Restrictions(*self), f)
            }
        }
        impl ::bitflags::__private::core::str::FromStr for InternalBitFlags {
            type Err = ::bitflags::parser::ParseError;
            fn from_str(s: &str)
                ->
                    ::bitflags::__private::core::result::Result<Self,
                    Self::Err> {
                ::bitflags::parser::from_str::<Restrictions>(s).map(|flags|
                        flags.0)
            }
        }
        impl ::bitflags::__private::core::convert::AsRef<u8> for
            InternalBitFlags {
            fn as_ref(&self) -> &u8 { &self.0 }
        }
        impl ::bitflags::__private::core::convert::From<u8> for
            InternalBitFlags {
            fn from(bits: u8) -> Self { Self::from_bits_retain(bits) }
        }
        #[allow(dead_code, deprecated, unused_attributes)]
        impl InternalBitFlags {
            /// Get a flags value with all bits unset.
            #[inline]
            pub const fn empty() -> Self {
                Self(<u8 as ::bitflags::Bits>::EMPTY)
            }
            /// Get a flags value with all known bits set.
            #[inline]
            pub const fn all() -> Self {
                let mut truncated = <u8 as ::bitflags::Bits>::EMPTY;
                let mut i = 0;
                {
                    {
                        let flag =
                            <Restrictions as
                                            ::bitflags::Flags>::FLAGS[i].value().bits();
                        truncated = truncated | flag;
                        i += 1;
                    }
                };
                {
                    {
                        let flag =
                            <Restrictions as
                                            ::bitflags::Flags>::FLAGS[i].value().bits();
                        truncated = truncated | flag;
                        i += 1;
                    }
                };
                {
                    {
                        let flag =
                            <Restrictions as
                                            ::bitflags::Flags>::FLAGS[i].value().bits();
                        truncated = truncated | flag;
                        i += 1;
                    }
                };
                {
                    {
                        let flag =
                            <Restrictions as
                                            ::bitflags::Flags>::FLAGS[i].value().bits();
                        truncated = truncated | flag;
                        i += 1;
                    }
                };
                {
                    {
                        let flag =
                            <Restrictions as
                                            ::bitflags::Flags>::FLAGS[i].value().bits();
                        truncated = truncated | flag;
                        i += 1;
                    }
                };
                {
                    {
                        let flag =
                            <Restrictions as
                                            ::bitflags::Flags>::FLAGS[i].value().bits();
                        truncated = truncated | flag;
                        i += 1;
                    }
                };
                let _ = i;
                Self(truncated)
            }
            /// Get the underlying bits value.
            ///
            /// The returned value is exactly the bits set in this flags value.
            #[inline]
            pub const fn bits(&self) -> u8 { self.0 }
            /// Convert from a bits value.
            ///
            /// This method will return `None` if any unknown bits are set.
            #[inline]
            pub const fn from_bits(bits: u8)
                -> ::bitflags::__private::core::option::Option<Self> {
                let truncated = Self::from_bits_truncate(bits).0;
                if truncated == bits {
                    ::bitflags::__private::core::option::Option::Some(Self(bits))
                } else { ::bitflags::__private::core::option::Option::None }
            }
            /// Convert from a bits value, unsetting any unknown bits.
            #[inline]
            pub const fn from_bits_truncate(bits: u8) -> Self {
                Self(bits & Self::all().0)
            }
            /// Convert from a bits value exactly.
            #[inline]
            pub const fn from_bits_retain(bits: u8) -> Self { Self(bits) }
            /// Get a flags value with the bits of a flag with the given name set.
            ///
            /// This method will return `None` if `name` is empty or doesn't
            /// correspond to any named flag.
            #[inline]
            pub fn from_name(name: &str)
                -> ::bitflags::__private::core::option::Option<Self> {
                {
                    if name == "STMT_EXPR" {
                        return ::bitflags::__private::core::option::Option::Some(Self(Restrictions::STMT_EXPR.bits()));
                    }
                };
                ;
                {
                    if name == "NO_STRUCT_LITERAL" {
                        return ::bitflags::__private::core::option::Option::Some(Self(Restrictions::NO_STRUCT_LITERAL.bits()));
                    }
                };
                ;
                {
                    if name == "CONST_EXPR" {
                        return ::bitflags::__private::core::option::Option::Some(Self(Restrictions::CONST_EXPR.bits()));
                    }
                };
                ;
                {
                    if name == "ALLOW_LET" {
                        return ::bitflags::__private::core::option::Option::Some(Self(Restrictions::ALLOW_LET.bits()));
                    }
                };
                ;
                {
                    if name == "IN_IF_GUARD" {
                        return ::bitflags::__private::core::option::Option::Some(Self(Restrictions::IN_IF_GUARD.bits()));
                    }
                };
                ;
                {
                    if name == "IS_PAT" {
                        return ::bitflags::__private::core::option::Option::Some(Self(Restrictions::IS_PAT.bits()));
                    }
                };
                ;
                let _ = name;
                ::bitflags::__private::core::option::Option::None
            }
            /// Whether all bits in this flags value are unset.
            #[inline]
            pub const fn is_empty(&self) -> bool {
                self.0 == <u8 as ::bitflags::Bits>::EMPTY
            }
            /// Whether all known bits in this flags value are set.
            #[inline]
            pub const fn is_all(&self) -> bool {
                Self::all().0 | self.0 == self.0
            }
            /// Whether any set bits in a source flags value are also set in a target flags value.
            #[inline]
            pub const fn intersects(&self, other: Self) -> bool {
                self.0 & other.0 != <u8 as ::bitflags::Bits>::EMPTY
            }
            /// Whether all set bits in a source flags value are also set in a target flags value.
            #[inline]
            pub const fn contains(&self, other: Self) -> bool {
                self.0 & other.0 == other.0
            }
            /// The bitwise or (`|`) of the bits in two flags values.
            #[inline]
            pub fn insert(&mut self, other: Self) {
                *self = Self(self.0).union(other);
            }
            /// The intersection of a source flags value with the complement of a target flags
            /// value (`&!`).
            ///
            /// This method is not equivalent to `self & !other` when `other` has unknown bits set.
            /// `remove` won't truncate `other`, but the `!` operator will.
            #[inline]
            pub fn remove(&mut self, other: Self) {
                *self = Self(self.0).difference(other);
            }
            /// The bitwise exclusive-or (`^`) of the bits in two flags values.
            #[inline]
            pub fn toggle(&mut self, other: Self) {
                *self = Self(self.0).symmetric_difference(other);
            }
            /// Call `insert` when `value` is `true` or `remove` when `value` is `false`.
            #[inline]
            pub fn set(&mut self, other: Self, value: bool) {
                if value { self.insert(other); } else { self.remove(other); }
            }
            /// The bitwise and (`&`) of the bits in two flags values.
            #[inline]
            #[must_use]
            pub const fn intersection(self, other: Self) -> Self {
                Self(self.0 & other.0)
            }
            /// The bitwise or (`|`) of the bits in two flags values.
            #[inline]
            #[must_use]
            pub const fn union(self, other: Self) -> Self {
                Self(self.0 | other.0)
            }
            /// The intersection of a source flags value with the complement of a target flags
            /// value (`&!`).
            ///
            /// This method is not equivalent to `self & !other` when `other` has unknown bits set.
            /// `difference` won't truncate `other`, but the `!` operator will.
            #[inline]
            #[must_use]
            pub const fn difference(self, other: Self) -> Self {
                Self(self.0 & !other.0)
            }
            /// The bitwise exclusive-or (`^`) of the bits in two flags values.
            #[inline]
            #[must_use]
            pub const fn symmetric_difference(self, other: Self) -> Self {
                Self(self.0 ^ other.0)
            }
            /// The bitwise negation (`!`) of the bits in a flags value, truncating the result.
            #[inline]
            #[must_use]
            pub const fn complement(self) -> Self {
                Self::from_bits_truncate(!self.0)
            }
        }
        impl ::bitflags::__private::core::fmt::Binary for InternalBitFlags {
            fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
                -> ::bitflags::__private::core::fmt::Result {
                let inner = self.0;
                ::bitflags::__private::core::fmt::Binary::fmt(&inner, f)
            }
        }
        impl ::bitflags::__private::core::fmt::Octal for InternalBitFlags {
            fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
                -> ::bitflags::__private::core::fmt::Result {
                let inner = self.0;
                ::bitflags::__private::core::fmt::Octal::fmt(&inner, f)
            }
        }
        impl ::bitflags::__private::core::fmt::LowerHex for InternalBitFlags {
            fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
                -> ::bitflags::__private::core::fmt::Result {
                let inner = self.0;
                ::bitflags::__private::core::fmt::LowerHex::fmt(&inner, f)
            }
        }
        impl ::bitflags::__private::core::fmt::UpperHex for InternalBitFlags {
            fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
                -> ::bitflags::__private::core::fmt::Result {
                let inner = self.0;
                ::bitflags::__private::core::fmt::UpperHex::fmt(&inner, f)
            }
        }
        impl ::bitflags::__private::core::ops::BitOr for InternalBitFlags {
            type Output = Self;
            /// The bitwise or (`|`) of the bits in two flags values.
            #[inline]
            fn bitor(self, other: InternalBitFlags) -> Self {
                self.union(other)
            }
        }
        impl ::bitflags::__private::core::ops::BitOrAssign for
            InternalBitFlags {
            /// The bitwise or (`|`) of the bits in two flags values.
            #[inline]
            fn bitor_assign(&mut self, other: Self) { self.insert(other); }
        }
        impl ::bitflags::__private::core::ops::BitXor for InternalBitFlags {
            type Output = Self;
            /// The bitwise exclusive-or (`^`) of the bits in two flags values.
            #[inline]
            fn bitxor(self, other: Self) -> Self {
                self.symmetric_difference(other)
            }
        }
        impl ::bitflags::__private::core::ops::BitXorAssign for
            InternalBitFlags {
            /// The bitwise exclusive-or (`^`) of the bits in two flags values.
            #[inline]
            fn bitxor_assign(&mut self, other: Self) { self.toggle(other); }
        }
        impl ::bitflags::__private::core::ops::BitAnd for InternalBitFlags {
            type Output = Self;
            /// The bitwise and (`&`) of the bits in two flags values.
            #[inline]
            fn bitand(self, other: Self) -> Self { self.intersection(other) }
        }
        impl ::bitflags::__private::core::ops::BitAndAssign for
            InternalBitFlags {
            /// The bitwise and (`&`) of the bits in two flags values.
            #[inline]
            fn bitand_assign(&mut self, other: Self) {
                *self =
                    Self::from_bits_retain(self.bits()).intersection(other);
            }
        }
        impl ::bitflags::__private::core::ops::Sub for InternalBitFlags {
            type Output = Self;
            /// The intersection of a source flags value with the complement of a target flags value (`&!`).
            ///
            /// This method is not equivalent to `self & !other` when `other` has unknown bits set.
            /// `difference` won't truncate `other`, but the `!` operator will.
            #[inline]
            fn sub(self, other: Self) -> Self { self.difference(other) }
        }
        impl ::bitflags::__private::core::ops::SubAssign for InternalBitFlags
            {
            /// The intersection of a source flags value with the complement of a target flags value (`&!`).
            ///
            /// This method is not equivalent to `self & !other` when `other` has unknown bits set.
            /// `difference` won't truncate `other`, but the `!` operator will.
            #[inline]
            fn sub_assign(&mut self, other: Self) { self.remove(other); }
        }
        impl ::bitflags::__private::core::ops::Not for InternalBitFlags {
            type Output = Self;
            /// The bitwise negation (`!`) of the bits in a flags value, truncating the result.
            #[inline]
            fn not(self) -> Self { self.complement() }
        }
        impl ::bitflags::__private::core::iter::Extend<InternalBitFlags> for
            InternalBitFlags {
            /// The bitwise or (`|`) of the bits in each flags value.
            fn extend<T: ::bitflags::__private::core::iter::IntoIterator<Item
                = Self>>(&mut self, iterator: T) {
                for item in iterator { self.insert(item) }
            }
        }
        impl ::bitflags::__private::core::iter::FromIterator<InternalBitFlags>
            for InternalBitFlags {
            /// The bitwise or (`|`) of the bits in each flags value.
            fn from_iter<T: ::bitflags::__private::core::iter::IntoIterator<Item
                = Self>>(iterator: T) -> Self {
                use ::bitflags::__private::core::iter::Extend;
                let mut result = Self::empty();
                result.extend(iterator);
                result
            }
        }
        impl InternalBitFlags {
            /// Yield a set of contained flags values.
            ///
            /// Each yielded flags value will correspond to a defined named flag. Any unknown bits
            /// will be yielded together as a final flags value.
            #[inline]
            pub const fn iter(&self) -> ::bitflags::iter::Iter<Restrictions> {
                ::bitflags::iter::Iter::__private_const_new(<Restrictions as
                        ::bitflags::Flags>::FLAGS,
                    Restrictions::from_bits_retain(self.bits()),
                    Restrictions::from_bits_retain(self.bits()))
            }
            /// Yield a set of contained named flags values.
            ///
            /// This method is like [`iter`](#method.iter), except only yields bits in contained named flags.
            /// Any unknown bits, or bits not corresponding to a contained flag will not be yielded.
            #[inline]
            pub const fn iter_names(&self)
                -> ::bitflags::iter::IterNames<Restrictions> {
                ::bitflags::iter::IterNames::__private_const_new(<Restrictions
                        as ::bitflags::Flags>::FLAGS,
                    Restrictions::from_bits_retain(self.bits()),
                    Restrictions::from_bits_retain(self.bits()))
            }
        }
        impl ::bitflags::__private::core::iter::IntoIterator for
            InternalBitFlags {
            type Item = Restrictions;
            type IntoIter = ::bitflags::iter::Iter<Restrictions>;
            fn into_iter(self) -> Self::IntoIter { self.iter() }
        }
        impl InternalBitFlags {
            /// Returns a mutable reference to the raw value of the flags currently stored.
            #[inline]
            pub fn bits_mut(&mut self) -> &mut u8 { &mut self.0 }
        }
        #[allow(dead_code, deprecated, unused_attributes)]
        impl Restrictions {
            /// Get a flags value with all bits unset.
            #[inline]
            pub const fn empty() -> Self { Self(InternalBitFlags::empty()) }
            /// Get a flags value with all known bits set.
            #[inline]
            pub const fn all() -> Self { Self(InternalBitFlags::all()) }
            /// Get the underlying bits value.
            ///
            /// The returned value is exactly the bits set in this flags value.
            #[inline]
            pub const fn bits(&self) -> u8 { self.0.bits() }
            /// Convert from a bits value.
            ///
            /// This method will return `None` if any unknown bits are set.
            #[inline]
            pub const fn from_bits(bits: u8)
                -> ::bitflags::__private::core::option::Option<Self> {
                match InternalBitFlags::from_bits(bits) {
                    ::bitflags::__private::core::option::Option::Some(bits) =>
                        ::bitflags::__private::core::option::Option::Some(Self(bits)),
                    ::bitflags::__private::core::option::Option::None =>
                        ::bitflags::__private::core::option::Option::None,
                }
            }
            /// Convert from a bits value, unsetting any unknown bits.
            #[inline]
            pub const fn from_bits_truncate(bits: u8) -> Self {
                Self(InternalBitFlags::from_bits_truncate(bits))
            }
            /// Convert from a bits value exactly.
            #[inline]
            pub const fn from_bits_retain(bits: u8) -> Self {
                Self(InternalBitFlags::from_bits_retain(bits))
            }
            /// Get a flags value with the bits of a flag with the given name set.
            ///
            /// This method will return `None` if `name` is empty or doesn't
            /// correspond to any named flag.
            #[inline]
            pub fn from_name(name: &str)
                -> ::bitflags::__private::core::option::Option<Self> {
                match InternalBitFlags::from_name(name) {
                    ::bitflags::__private::core::option::Option::Some(bits) =>
                        ::bitflags::__private::core::option::Option::Some(Self(bits)),
                    ::bitflags::__private::core::option::Option::None =>
                        ::bitflags::__private::core::option::Option::None,
                }
            }
            /// Whether all bits in this flags value are unset.
            #[inline]
            pub const fn is_empty(&self) -> bool { self.0.is_empty() }
            /// Whether all known bits in this flags value are set.
            #[inline]
            pub const fn is_all(&self) -> bool { self.0.is_all() }
            /// Whether any set bits in a source flags value are also set in a target flags value.
            #[inline]
            pub const fn intersects(&self, other: Self) -> bool {
                self.0.intersects(other.0)
            }
            /// Whether all set bits in a source flags value are also set in a target flags value.
            #[inline]
            pub const fn contains(&self, other: Self) -> bool {
                self.0.contains(other.0)
            }
            /// The bitwise or (`|`) of the bits in two flags values.
            #[inline]
            pub fn insert(&mut self, other: Self) { self.0.insert(other.0) }
            /// The intersection of a source flags value with the complement of a target flags
            /// value (`&!`).
            ///
            /// This method is not equivalent to `self & !other` when `other` has unknown bits set.
            /// `remove` won't truncate `other`, but the `!` operator will.
            #[inline]
            pub fn remove(&mut self, other: Self) { self.0.remove(other.0) }
            /// The bitwise exclusive-or (`^`) of the bits in two flags values.
            #[inline]
            pub fn toggle(&mut self, other: Self) { self.0.toggle(other.0) }
            /// Call `insert` when `value` is `true` or `remove` when `value` is `false`.
            #[inline]
            pub fn set(&mut self, other: Self, value: bool) {
                self.0.set(other.0, value)
            }
            /// The bitwise and (`&`) of the bits in two flags values.
            #[inline]
            #[must_use]
            pub const fn intersection(self, other: Self) -> Self {
                Self(self.0.intersection(other.0))
            }
            /// The bitwise or (`|`) of the bits in two flags values.
            #[inline]
            #[must_use]
            pub const fn union(self, other: Self) -> Self {
                Self(self.0.union(other.0))
            }
            /// The intersection of a source flags value with the complement of a target flags
            /// value (`&!`).
            ///
            /// This method is not equivalent to `self & !other` when `other` has unknown bits set.
            /// `difference` won't truncate `other`, but the `!` operator will.
            #[inline]
            #[must_use]
            pub const fn difference(self, other: Self) -> Self {
                Self(self.0.difference(other.0))
            }
            /// The bitwise exclusive-or (`^`) of the bits in two flags values.
            #[inline]
            #[must_use]
            pub const fn symmetric_difference(self, other: Self) -> Self {
                Self(self.0.symmetric_difference(other.0))
            }
            /// The bitwise negation (`!`) of the bits in a flags value, truncating the result.
            #[inline]
            #[must_use]
            pub const fn complement(self) -> Self {
                Self(self.0.complement())
            }
        }
        impl ::bitflags::__private::core::fmt::Binary for Restrictions {
            fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
                -> ::bitflags::__private::core::fmt::Result {
                let inner = self.0;
                ::bitflags::__private::core::fmt::Binary::fmt(&inner, f)
            }
        }
        impl ::bitflags::__private::core::fmt::Octal for Restrictions {
            fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
                -> ::bitflags::__private::core::fmt::Result {
                let inner = self.0;
                ::bitflags::__private::core::fmt::Octal::fmt(&inner, f)
            }
        }
        impl ::bitflags::__private::core::fmt::LowerHex for Restrictions {
            fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
                -> ::bitflags::__private::core::fmt::Result {
                let inner = self.0;
                ::bitflags::__private::core::fmt::LowerHex::fmt(&inner, f)
            }
        }
        impl ::bitflags::__private::core::fmt::UpperHex for Restrictions {
            fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
                -> ::bitflags::__private::core::fmt::Result {
                let inner = self.0;
                ::bitflags::__private::core::fmt::UpperHex::fmt(&inner, f)
            }
        }
        impl ::bitflags::__private::core::ops::BitOr for Restrictions {
            type Output = Self;
            /// The bitwise or (`|`) of the bits in two flags values.
            #[inline]
            fn bitor(self, other: Restrictions) -> Self { self.union(other) }
        }
        impl ::bitflags::__private::core::ops::BitOrAssign for Restrictions {
            /// The bitwise or (`|`) of the bits in two flags values.
            #[inline]
            fn bitor_assign(&mut self, other: Self) { self.insert(other); }
        }
        impl ::bitflags::__private::core::ops::BitXor for Restrictions {
            type Output = Self;
            /// The bitwise exclusive-or (`^`) of the bits in two flags values.
            #[inline]
            fn bitxor(self, other: Self) -> Self {
                self.symmetric_difference(other)
            }
        }
        impl ::bitflags::__private::core::ops::BitXorAssign for Restrictions {
            /// The bitwise exclusive-or (`^`) of the bits in two flags values.
            #[inline]
            fn bitxor_assign(&mut self, other: Self) { self.toggle(other); }
        }
        impl ::bitflags::__private::core::ops::BitAnd for Restrictions {
            type Output = Self;
            /// The bitwise and (`&`) of the bits in two flags values.
            #[inline]
            fn bitand(self, other: Self) -> Self { self.intersection(other) }
        }
        impl ::bitflags::__private::core::ops::BitAndAssign for Restrictions {
            /// The bitwise and (`&`) of the bits in two flags values.
            #[inline]
            fn bitand_assign(&mut self, other: Self) {
                *self =
                    Self::from_bits_retain(self.bits()).intersection(other);
            }
        }
        impl ::bitflags::__private::core::ops::Sub for Restrictions {
            type Output = Self;
            /// The intersection of a source flags value with the complement of a target flags value (`&!`).
            ///
            /// This method is not equivalent to `self & !other` when `other` has unknown bits set.
            /// `difference` won't truncate `other`, but the `!` operator will.
            #[inline]
            fn sub(self, other: Self) -> Self { self.difference(other) }
        }
        impl ::bitflags::__private::core::ops::SubAssign for Restrictions {
            /// The intersection of a source flags value with the complement of a target flags value (`&!`).
            ///
            /// This method is not equivalent to `self & !other` when `other` has unknown bits set.
            /// `difference` won't truncate `other`, but the `!` operator will.
            #[inline]
            fn sub_assign(&mut self, other: Self) { self.remove(other); }
        }
        impl ::bitflags::__private::core::ops::Not for Restrictions {
            type Output = Self;
            /// The bitwise negation (`!`) of the bits in a flags value, truncating the result.
            #[inline]
            fn not(self) -> Self { self.complement() }
        }
        impl ::bitflags::__private::core::iter::Extend<Restrictions> for
            Restrictions {
            /// The bitwise or (`|`) of the bits in each flags value.
            fn extend<T: ::bitflags::__private::core::iter::IntoIterator<Item
                = Self>>(&mut self, iterator: T) {
                for item in iterator { self.insert(item) }
            }
        }
        impl ::bitflags::__private::core::iter::FromIterator<Restrictions> for
            Restrictions {
            /// The bitwise or (`|`) of the bits in each flags value.
            fn from_iter<T: ::bitflags::__private::core::iter::IntoIterator<Item
                = Self>>(iterator: T) -> Self {
                use ::bitflags::__private::core::iter::Extend;
                let mut result = Self::empty();
                result.extend(iterator);
                result
            }
        }
        impl Restrictions {
            /// Yield a set of contained flags values.
            ///
            /// Each yielded flags value will correspond to a defined named flag. Any unknown bits
            /// will be yielded together as a final flags value.
            #[inline]
            pub const fn iter(&self) -> ::bitflags::iter::Iter<Restrictions> {
                ::bitflags::iter::Iter::__private_const_new(<Restrictions as
                        ::bitflags::Flags>::FLAGS,
                    Restrictions::from_bits_retain(self.bits()),
                    Restrictions::from_bits_retain(self.bits()))
            }
            /// Yield a set of contained named flags values.
            ///
            /// This method is like [`iter`](#method.iter), except only yields bits in contained named flags.
            /// Any unknown bits, or bits not corresponding to a contained flag will not be yielded.
            #[inline]
            pub const fn iter_names(&self)
                -> ::bitflags::iter::IterNames<Restrictions> {
                ::bitflags::iter::IterNames::__private_const_new(<Restrictions
                        as ::bitflags::Flags>::FLAGS,
                    Restrictions::from_bits_retain(self.bits()),
                    Restrictions::from_bits_retain(self.bits()))
            }
        }
        impl ::bitflags::__private::core::iter::IntoIterator for Restrictions
            {
            type Item = Restrictions;
            type IntoIter = ::bitflags::iter::Iter<Restrictions>;
            fn into_iter(self) -> Self::IntoIter { self.iter() }
        }
    };Clone, #[automatically_derived]
impl ::core::marker::Copy for Restrictions { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for Restrictions {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Restrictions",
            &&self.0)
    }
}Debug)]
77    struct Restrictions: u8 {
78        /// Restricts expressions for use in statement position.
79        ///
80        /// When expressions are used in various places, like statements or
81        /// match arms, this is used to stop parsing once certain tokens are
82        /// reached.
83        ///
84        /// For example, `if true {} & 1` with `STMT_EXPR` in effect is parsed
85        /// as two separate expression statements (`if` and a reference to 1).
86        /// Otherwise it is parsed as a bitwise AND where `if` is on the left
87        /// and 1 is on the right.
88        const STMT_EXPR         = 1 << 0;
89        /// Do not allow struct literals.
90        ///
91        /// There are several places in the grammar where we don't want to
92        /// allow struct literals because they can require lookahead, or
93        /// otherwise could be ambiguous or cause confusion. For example,
94        /// `if Foo {} {}` isn't clear if it is `Foo{}` struct literal, or
95        /// just `Foo` is the condition, followed by a consequent block,
96        /// followed by an empty block.
97        ///
98        /// See [RFC 92](https://rust-lang.github.io/rfcs/0092-struct-grammar.html).
99        const NO_STRUCT_LITERAL = 1 << 1;
100        /// Used to provide better error messages for const generic arguments.
101        ///
102        /// An un-braced const generic argument is limited to a very small
103        /// subset of expressions. This is used to detect the situation where
104        /// an expression outside of that subset is used, and to suggest to
105        /// wrap the expression in braces.
106        const CONST_EXPR        = 1 << 2;
107        /// Allows `let` expressions.
108        ///
109        /// `let pattern = scrutinee` is parsed as an expression, but it is
110        /// only allowed in let chains (`if` and `while` conditions).
111        /// Otherwise it is not an expression (note that `let` in statement
112        /// positions is treated as a `StmtKind::Let` statement, which has a
113        /// slightly different grammar).
114        const ALLOW_LET         = 1 << 3;
115        /// Used to detect a missing `=>` in a match guard.
116        ///
117        /// This is used for error handling in a match guard to give a better
118        /// error message if the `=>` is missing. It is set when parsing the
119        /// guard expression.
120        const IN_IF_GUARD       = 1 << 4;
121        /// Used to detect the incorrect use of expressions in patterns.
122        ///
123        /// This is used for error handling while parsing a pattern. During
124        /// error recovery, this will be set to try to parse the pattern as an
125        /// expression, but halts parsing the expression when reaching certain
126        /// tokens like `=`.
127        const IS_PAT            = 1 << 5;
128    }
129}
130
131#[derive(#[automatically_derived]
impl ::core::clone::Clone for SemiColonMode {
    #[inline]
    fn clone(&self) -> SemiColonMode { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for SemiColonMode { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for SemiColonMode {
    #[inline]
    fn eq(&self, other: &SemiColonMode) -> 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::fmt::Debug for SemiColonMode {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                SemiColonMode::Break => "Break",
                SemiColonMode::Ignore => "Ignore",
                SemiColonMode::Comma => "Comma",
            })
    }
}Debug)]
132enum SemiColonMode {
133    Break,
134    Ignore,
135    Comma,
136}
137
138#[derive(#[automatically_derived]
impl ::core::clone::Clone for BlockMode {
    #[inline]
    fn clone(&self) -> BlockMode { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for BlockMode { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for BlockMode {
    #[inline]
    fn eq(&self, other: &BlockMode) -> 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::fmt::Debug for BlockMode {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                BlockMode::Break => "Break",
                BlockMode::Ignore => "Ignore",
            })
    }
}Debug)]
139enum BlockMode {
140    Break,
141    Ignore,
142}
143
144/// Whether or not we should force collection of tokens for an AST node,
145/// regardless of whether or not it has attributes
146#[derive(#[automatically_derived]
impl ::core::clone::Clone for ForceCollect {
    #[inline]
    fn clone(&self) -> ForceCollect { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for ForceCollect { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for ForceCollect {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                ForceCollect::Yes => "Yes",
                ForceCollect::No => "No",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for ForceCollect {
    #[inline]
    fn eq(&self, other: &ForceCollect) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq)]
147pub enum ForceCollect {
148    Yes,
149    No,
150}
151
152/// Whether to accept `const { ... }` as a shorthand for `const _: () = const { ... }`.
153#[derive(#[automatically_derived]
impl ::core::clone::Clone for AllowConstBlockItems {
    #[inline]
    fn clone(&self) -> AllowConstBlockItems { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for AllowConstBlockItems { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for AllowConstBlockItems {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                AllowConstBlockItems::Yes => "Yes",
                AllowConstBlockItems::No => "No",
                AllowConstBlockItems::DoesNotMatter => "DoesNotMatter",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for AllowConstBlockItems {
    #[inline]
    fn eq(&self, other: &AllowConstBlockItems) -> 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 AllowConstBlockItems {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq)]
154pub enum AllowConstBlockItems {
155    Yes,
156    No,
157    DoesNotMatter,
158}
159
160/// If the next tokens are ill-formed `$ty::` recover them as `<$ty>::`.
161#[macro_export]
162macro_rules! maybe_recover_from_interpolated_ty_qpath {
163    ($self: expr, $allow_qpath_recovery: expr) => {
164        if $allow_qpath_recovery
165            && $self.may_recover()
166            && let Some(mv_kind) = $self.token.is_metavar_seq()
167            && let token::MetaVarKind::Ty { .. } = mv_kind
168            && $self.check_noexpect_past_close_delim(&token::PathSep)
169        {
170            // Reparse the type, then move to recovery.
171            let ty = $self
172                .eat_metavar_seq(mv_kind, |this| this.parse_ty_no_question_mark_recover())
173                .expect("metavar seq ty");
174
175            return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
176        }
177    };
178}
179
180#[derive(#[automatically_derived]
impl ::core::clone::Clone for Recovery {
    #[inline]
    fn clone(&self) -> Recovery { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Recovery { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for Recovery {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                Recovery::Allowed => "Allowed",
                Recovery::Forbidden => "Forbidden",
            })
    }
}Debug)]
181pub enum Recovery {
182    Allowed,
183    Forbidden,
184}
185
186#[derive(#[automatically_derived]
impl<'a> ::core::clone::Clone for Parser<'a> {
    #[inline]
    fn clone(&self) -> Parser<'a> {
        Parser {
            psess: ::core::clone::Clone::clone(&self.psess),
            token: ::core::clone::Clone::clone(&self.token),
            token_spacing: ::core::clone::Clone::clone(&self.token_spacing),
            prev_token: ::core::clone::Clone::clone(&self.prev_token),
            capture_cfg: ::core::clone::Clone::clone(&self.capture_cfg),
            restrictions: ::core::clone::Clone::clone(&self.restrictions),
            expected_token_types: ::core::clone::Clone::clone(&self.expected_token_types),
            token_cursor: ::core::clone::Clone::clone(&self.token_cursor),
            num_bump_calls: ::core::clone::Clone::clone(&self.num_bump_calls),
            break_last_token: ::core::clone::Clone::clone(&self.break_last_token),
            unmatched_angle_bracket_count: ::core::clone::Clone::clone(&self.unmatched_angle_bracket_count),
            angle_bracket_nesting: ::core::clone::Clone::clone(&self.angle_bracket_nesting),
            parsing_generics: ::core::clone::Clone::clone(&self.parsing_generics),
            last_unexpected_token_span: ::core::clone::Clone::clone(&self.last_unexpected_token_span),
            subparser_name: ::core::clone::Clone::clone(&self.subparser_name),
            capture_state: ::core::clone::Clone::clone(&self.capture_state),
            current_closure: ::core::clone::Clone::clone(&self.current_closure),
            recovery: ::core::clone::Clone::clone(&self.recovery),
            in_fn_body: ::core::clone::Clone::clone(&self.in_fn_body),
            fn_body_missing_semi_guar: ::core::clone::Clone::clone(&self.fn_body_missing_semi_guar),
        }
    }
}Clone)]
187pub struct Parser<'a> {
188    pub psess: &'a ParseSess,
189    /// The current token.
190    pub token: Token = Token::dummy(),
191    /// The spacing for the current token.
192    token_spacing: Spacing = Spacing::Alone,
193    /// The previous token.
194    pub prev_token: Token = Token::dummy(),
195    pub capture_cfg: bool = false,
196    restrictions: Restrictions = Restrictions::empty(),
197    expected_token_types: TokenTypeSet = TokenTypeSet::new(),
198    token_cursor: TokenCursor,
199    // The number of calls to `bump`, i.e. the position in the token stream.
200    num_bump_calls: u32 = 0,
201    // During parsing we may sometimes need to "unglue" a glued token into two
202    // or three component tokens (e.g. `>>` into `>` and `>`, or `>>=` into `>`
203    // and `>` and `=`), so the parser can consume them one at a time. This
204    // process bypasses the normal capturing mechanism (e.g. `num_bump_calls`
205    // will not be incremented), since the "unglued" tokens due not exist in
206    // the original `TokenStream`.
207    //
208    // If we end up consuming all the component tokens, this is not an issue,
209    // because we'll end up capturing the single "glued" token.
210    //
211    // However, sometimes we may want to capture not all of the original
212    // token. For example, capturing the `Vec<u8>` in `Option<Vec<u8>>`
213    // requires us to unglue the trailing `>>` token. The `break_last_token`
214    // field is used to track these tokens. They get appended to the captured
215    // stream when we evaluate a `LazyAttrTokenStream`.
216    //
217    // This value is always 0, 1, or 2. It can only reach 2 when splitting
218    // `>>=` or `<<=`.
219    break_last_token: u32 = 0,
220    /// This field is used to keep track of how many left angle brackets we have seen. This is
221    /// required in order to detect extra leading left angle brackets (`<` characters) and error
222    /// appropriately.
223    ///
224    /// See the comments in the `parse_path_segment` function for more details.
225    unmatched_angle_bracket_count: u16 = 0,
226    angle_bracket_nesting: u16 = 0,
227    /// Keep track of when we're within `<...>` for proper error recovery.
228    parsing_generics: bool = false,
229
230    last_unexpected_token_span: Option<Span> = None,
231    /// If present, this `Parser` is not parsing Rust code but rather a macro call.
232    subparser_name: Option<&'static str>,
233    capture_state: CaptureState,
234    /// This allows us to recover when the user forget to add braces around
235    /// multiple statements in the closure body.
236    current_closure: Option<ClosureSpans> = None,
237    /// Whether the parser is allowed to do recovery.
238    /// This is disabled when parsing macro arguments, see #103534
239    recovery: Recovery = Recovery::Allowed,
240    /// Whether we're parsing a function body.
241    in_fn_body: bool = false,
242    /// Whether we have detected a missing semicolon in function body.
243    pub fn_body_missing_semi_guar: Option<ErrorGuaranteed> = None,
244}
245
246// This type is used a lot, e.g. it's cloned when matching many declarative macro rules with
247// nonterminals. Make sure it doesn't unintentionally get bigger. We only check a few arches
248// though, because `TokenTypeSet(u128)` alignment varies on others, changing the total size.
249#[cfg(all(target_pointer_width = "64", any(target_arch = "aarch64", target_arch = "x86_64")))]
250const _: [(); 288] = [(); ::std::mem::size_of::<Parser<'_>>()];rustc_data_structures::static_assert_size!(Parser<'_>, 288);
251
252/// Stores span information about a closure.
253#[derive(#[automatically_derived]
impl ::core::clone::Clone for ClosureSpans {
    #[inline]
    fn clone(&self) -> ClosureSpans {
        ClosureSpans {
            whole_closure: ::core::clone::Clone::clone(&self.whole_closure),
            closing_pipe: ::core::clone::Clone::clone(&self.closing_pipe),
            body: ::core::clone::Clone::clone(&self.body),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ClosureSpans {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "ClosureSpans",
            "whole_closure", &self.whole_closure, "closing_pipe",
            &self.closing_pipe, "body", &&self.body)
    }
}Debug)]
254struct ClosureSpans {
255    whole_closure: Span,
256    closing_pipe: Span,
257    body: Span,
258}
259
260/// Controls how we capture tokens. Capturing can be expensive,
261/// so we try to avoid performing capturing in cases where
262/// we will never need an `AttrTokenStream`.
263#[derive(#[automatically_derived]
impl ::core::marker::Copy for Capturing { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Capturing {
    #[inline]
    fn clone(&self) -> Capturing { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Capturing {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self { Capturing::No => "No", Capturing::Yes => "Yes", })
    }
}Debug)]
264enum Capturing {
265    /// We aren't performing any capturing - this is the default mode.
266    No,
267    /// We are capturing tokens
268    Yes,
269}
270
271// This state is used by `Parser::collect_tokens`.
272#[derive(#[automatically_derived]
impl ::core::clone::Clone for CaptureState {
    #[inline]
    fn clone(&self) -> CaptureState {
        CaptureState {
            capturing: ::core::clone::Clone::clone(&self.capturing),
            parser_replacements: ::core::clone::Clone::clone(&self.parser_replacements),
            inner_attr_parser_ranges: ::core::clone::Clone::clone(&self.inner_attr_parser_ranges),
            seen_attrs: ::core::clone::Clone::clone(&self.seen_attrs),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for CaptureState {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field4_finish(f, "CaptureState",
            "capturing", &self.capturing, "parser_replacements",
            &self.parser_replacements, "inner_attr_parser_ranges",
            &self.inner_attr_parser_ranges, "seen_attrs", &&self.seen_attrs)
    }
}Debug)]
273struct CaptureState {
274    capturing: Capturing,
275    parser_replacements: Vec<ParserReplacement>,
276    inner_attr_parser_ranges: FxHashMap<AttrId, ParserRange>,
277    // `IntervalSet` is good for perf because attrs are mostly added to this
278    // set in contiguous ranges.
279    seen_attrs: IntervalSet<AttrId>,
280}
281
282/// A sequence separator.
283#[derive(#[automatically_derived]
impl ::core::fmt::Debug for SeqSep {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "SeqSep", "sep",
            &self.sep, "trailing_sep_allowed", &&self.trailing_sep_allowed)
    }
}Debug)]
284struct SeqSep {
285    /// The separator token.
286    sep: Option<ExpTokenPair>,
287    /// `true` if a trailing separator is allowed.
288    trailing_sep_allowed: bool,
289}
290
291impl SeqSep {
292    fn trailing_allowed(sep: ExpTokenPair) -> SeqSep {
293        SeqSep { sep: Some(sep), trailing_sep_allowed: true }
294    }
295
296    fn none() -> SeqSep {
297        SeqSep { sep: None, trailing_sep_allowed: false }
298    }
299}
300
301#[derive(#[automatically_derived]
impl ::core::fmt::Debug for FollowedByType {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                FollowedByType::Yes => "Yes",
                FollowedByType::No => "No",
            })
    }
}Debug)]
302pub enum FollowedByType {
303    Yes,
304    No,
305}
306
307#[derive(#[automatically_derived]
impl ::core::marker::Copy for Trailing { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Trailing {
    #[inline]
    fn clone(&self) -> Trailing { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Trailing {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self { Trailing::No => "No", Trailing::Yes => "Yes", })
    }
}Debug)]
308pub enum Trailing {
309    No,
310    Yes,
311}
312
313impl From<bool> for Trailing {
314    fn from(b: bool) -> Trailing {
315        if b { Trailing::Yes } else { Trailing::No }
316    }
317}
318
319pub fn token_descr(token: &Token) -> String {
320    let s = pprust::token_to_string(token).to_string();
321
322    match (TokenDescription::from_token(token), &token.kind) {
323        (Some(TokenDescription::ReservedIdentifier), _) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("reserved identifier `{0}`", s))
    })format!("reserved identifier `{s}`"),
324        (Some(TokenDescription::Keyword), _) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("keyword `{0}`", s))
    })format!("keyword `{s}`"),
325        (Some(TokenDescription::ReservedKeyword), _) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("reserved keyword `{0}`", s))
    })format!("reserved keyword `{s}`"),
326        (Some(TokenDescription::DocComment), _) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("doc comment `{0}`", s))
    })format!("doc comment `{s}`"),
327        // Deliberately doesn't print `s`, which is empty.
328        (Some(TokenDescription::MetaVar(kind)), _) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` metavariable", kind))
    })format!("`{kind}` metavariable"),
329        (None, TokenKind::NtIdent(..)) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("identifier `{0}`", s))
    })format!("identifier `{s}`"),
330        (None, TokenKind::NtLifetime(..)) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("lifetime `{0}`", s))
    })format!("lifetime `{s}`"),
331        (None, _) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}`", s))
    })format!("`{s}`"),
332    }
333}
334
335impl<'a> Parser<'a> {
336    pub fn new(
337        psess: &'a ParseSess,
338        stream: TokenStream,
339        subparser_name: Option<&'static str>,
340    ) -> Self {
341        let mut parser = Parser {
342            psess,
343            token_cursor: TokenCursor { curr: TokenTreeCursor::new(stream), stack: Vec::new() },
344            subparser_name,
345            capture_state: CaptureState {
346                capturing: Capturing::No,
347                parser_replacements: Vec::new(),
348                inner_attr_parser_ranges: Default::default(),
349                seen_attrs: IntervalSet::new(u32::MAX as usize),
350            },
351            ..
352        };
353
354        // Make parser point to the first token.
355        parser.bump();
356
357        // Change this from 1 back to 0 after the bump. This eases debugging of
358        // `Parser::collect_tokens` because 0-indexed token positions are nicer
359        // than 1-indexed token positions.
360        parser.num_bump_calls = 0;
361
362        parser
363    }
364
365    #[inline]
366    pub fn recovery(mut self, recovery: Recovery) -> Self {
367        self.recovery = recovery;
368        self
369    }
370
371    #[inline]
372    fn with_recovery<T>(&mut self, recovery: Recovery, f: impl FnOnce(&mut Self) -> T) -> T {
373        let old = mem::replace(&mut self.recovery, recovery);
374        let res = f(self);
375        self.recovery = old;
376        res
377    }
378
379    /// Whether the parser is allowed to recover from broken code.
380    ///
381    /// If this returns false, recovering broken code into valid code (especially if this recovery does lookahead)
382    /// is not allowed. All recovery done by the parser must be gated behind this check.
383    ///
384    /// Technically, this only needs to restrict eager recovery by doing lookahead at more tokens.
385    /// But making the distinction is very subtle, and simply forbidding all recovery is a lot simpler to uphold.
386    #[inline]
387    fn may_recover(&self) -> bool {
388        #[allow(non_exhaustive_omitted_patterns)] match self.recovery {
    Recovery::Allowed => true,
    _ => false,
}matches!(self.recovery, Recovery::Allowed)
389    }
390
391    /// Version of [`unexpected`](Parser::unexpected) that "returns" any type in the `Ok`
392    /// (both those functions never return "Ok", and so can lie like that in the type).
393    pub fn unexpected_any<T>(&mut self) -> PResult<'a, T> {
394        match self.expect_one_of(&[], &[]) {
395            Err(e) => Err(e),
396            // We can get `Ok(true)` from `recover_closing_delimiter`
397            // which is called in `expected_one_of_not_found`.
398            Ok(_) => FatalError.raise(),
399        }
400    }
401
402    pub fn unexpected(&mut self) -> PResult<'a, ()> {
403        self.unexpected_any()
404    }
405
406    /// Expects and consumes the token `t`. Signals an error if the next token is not `t`.
407    pub fn expect(&mut self, exp: ExpTokenPair) -> PResult<'a, Recovered> {
408        if self.expected_token_types.is_empty() {
409            if self.token == exp.tok {
410                self.bump();
411                Ok(Recovered::No)
412            } else {
413                self.unexpected_try_recover(&exp.tok)
414            }
415        } else {
416            self.expect_one_of(slice::from_ref(&exp), &[])
417        }
418    }
419
420    /// Expect next token to be edible or inedible token. If edible,
421    /// then consume it; if inedible, then return without consuming
422    /// anything. Signal a fatal error if next token is unexpected.
423    fn expect_one_of(
424        &mut self,
425        edible: &[ExpTokenPair],
426        inedible: &[ExpTokenPair],
427    ) -> PResult<'a, Recovered> {
428        if edible.iter().any(|exp| exp.tok == self.token.kind) {
429            self.bump();
430            Ok(Recovered::No)
431        } else if inedible.iter().any(|exp| exp.tok == self.token.kind) {
432            // leave it in the input
433            Ok(Recovered::No)
434        } else if self.token != token::Eof
435            && self.last_unexpected_token_span == Some(self.token.span)
436        {
437            FatalError.raise();
438        } else {
439            self.expected_one_of_not_found(edible, inedible)
440                .map(|error_guaranteed| Recovered::Yes(error_guaranteed))
441        }
442    }
443
444    // Public for rustfmt usage.
445    pub fn parse_ident(&mut self) -> PResult<'a, Ident> {
446        self.parse_ident_common(self.may_recover())
447    }
448
449    pub(crate) fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
450        let (ident, is_raw) = self.ident_or_err(recover)?;
451
452        if is_raw == IdentIsRaw::No && ident.is_reserved() {
453            let err = self.expected_ident_found_err();
454            if recover {
455                err.emit();
456            } else {
457                return Err(err);
458            }
459        }
460        self.bump();
461        Ok(ident)
462    }
463
464    fn ident_or_err(&mut self, recover: bool) -> PResult<'a, (Ident, IdentIsRaw)> {
465        match self.token.ident() {
466            Some(ident) => Ok(ident),
467            None => self.expected_ident_found(recover),
468        }
469    }
470
471    /// Checks if the next token is `tok`, and returns `true` if so.
472    ///
473    /// This method will automatically add `tok` to `expected_token_types` if `tok` is not
474    /// encountered.
475    #[inline]
476    pub fn check(&mut self, exp: ExpTokenPair) -> bool {
477        let is_present = self.token == exp.tok;
478        if !is_present {
479            self.expected_token_types.insert(exp.token_type);
480        }
481        is_present
482    }
483
484    #[inline]
485    #[must_use]
486    fn check_noexpect(&self, tok: &TokenKind) -> bool {
487        self.token == *tok
488    }
489
490    // Check the first token after the delimiter that closes the current
491    // delimited sequence. (Panics if used in the outermost token stream, which
492    // has no delimiters.) It uses a clone of the relevant tree cursor to skip
493    // past the entire `TokenTree::Delimited` in a single step, avoiding the
494    // need for unbounded token lookahead.
495    //
496    // Primarily used when `self.token` matches `OpenInvisible(_))`, to look
497    // ahead through the current metavar expansion.
498    fn check_noexpect_past_close_delim(&self, tok: &TokenKind) -> bool {
499        let mut tree_cursor = self.token_cursor.stack.last().unwrap().clone();
500        tree_cursor.bump();
501        #[allow(non_exhaustive_omitted_patterns)] match tree_cursor.curr() {
    Some(TokenTree::Token(token::Token { kind, .. }, _)) if kind == tok =>
        true,
    _ => false,
}matches!(
502            tree_cursor.curr(),
503            Some(TokenTree::Token(token::Token { kind, .. }, _)) if kind == tok
504        )
505    }
506
507    /// Consumes a token 'tok' if it exists. Returns whether the given token was present.
508    ///
509    /// the main purpose of this function is to reduce the cluttering of the suggestions list
510    /// which using the normal eat method could introduce in some cases.
511    #[inline]
512    #[must_use]
513    fn eat_noexpect(&mut self, tok: &TokenKind) -> bool {
514        let is_present = self.check_noexpect(tok);
515        if is_present {
516            self.bump()
517        }
518        is_present
519    }
520
521    /// Consumes a token 'tok' if it exists. Returns whether the given token was present.
522    #[inline]
523    #[must_use]
524    pub fn eat(&mut self, exp: ExpTokenPair) -> bool {
525        let is_present = self.check(exp);
526        if is_present {
527            self.bump()
528        }
529        is_present
530    }
531
532    /// If the next token is the given keyword, returns `true` without eating it.
533    /// An expectation is also added for diagnostics purposes.
534    #[inline]
535    #[must_use]
536    fn check_keyword(&mut self, exp: ExpKeywordPair) -> bool {
537        let is_keyword = self.token.is_keyword(exp.kw);
538        if !is_keyword {
539            self.expected_token_types.insert(exp.token_type);
540        }
541        is_keyword
542    }
543
544    #[inline]
545    #[must_use]
546    fn check_keyword_case(&mut self, exp: ExpKeywordPair, case: Case) -> bool {
547        if self.check_keyword(exp) {
548            true
549        } else if case == Case::Insensitive
550            && let Some((ident, IdentIsRaw::No)) = self.token.ident()
551            // Do an ASCII case-insensitive match, because all keywords are ASCII.
552            && ident.as_str().eq_ignore_ascii_case(exp.kw.as_str())
553        {
554            true
555        } else {
556            false
557        }
558    }
559
560    /// If the next token is the given keyword, eats it and returns `true`.
561    /// Otherwise, returns `false`. An expectation is also added for diagnostics purposes.
562    // Public for rustc_builtin_macros and rustfmt usage.
563    #[inline]
564    #[must_use]
565    pub fn eat_keyword(&mut self, exp: ExpKeywordPair) -> bool {
566        let is_keyword = self.check_keyword(exp);
567        if is_keyword {
568            self.bump();
569        }
570        is_keyword
571    }
572
573    /// Eats a keyword, optionally ignoring the case.
574    /// If the case differs (and is ignored) an error is issued.
575    /// This is useful for recovery.
576    #[inline]
577    #[must_use]
578    fn eat_keyword_case(&mut self, exp: ExpKeywordPair, case: Case) -> bool {
579        if self.eat_keyword(exp) {
580            true
581        } else if case == Case::Insensitive
582            && let Some((ident, IdentIsRaw::No)) = self.token.ident()
583            // Do an ASCII case-insensitive match, because all keywords are ASCII.
584            && ident.as_str().eq_ignore_ascii_case(exp.kw.as_str())
585        {
586            let kw = exp.kw.as_str();
587            let is_upper = kw.chars().all(char::is_uppercase);
588            let is_lower = kw.chars().all(char::is_lowercase);
589
590            let case = match (is_upper, is_lower) {
591                (true, true) => {
592                    {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("keyword that is both fully upper- and fully lowercase")));
}unreachable!("keyword that is both fully upper- and fully lowercase")
593                }
594                (true, false) => errors::Case::Upper,
595                (false, true) => errors::Case::Lower,
596                (false, false) => errors::Case::Mixed,
597            };
598
599            self.dcx().emit_err(errors::KwBadCase { span: ident.span, kw, case });
600            self.bump();
601            true
602        } else {
603            false
604        }
605    }
606
607    /// If the next token is the given keyword, eats it and returns `true`.
608    /// Otherwise, returns `false`. No expectation is added.
609    // Public for rustc_builtin_macros usage.
610    #[inline]
611    #[must_use]
612    pub fn eat_keyword_noexpect(&mut self, kw: Symbol) -> bool {
613        let is_keyword = self.token.is_keyword(kw);
614        if is_keyword {
615            self.bump();
616        }
617        is_keyword
618    }
619
620    /// If the given word is not a keyword, signals an error.
621    /// If the next token is not the given word, signals an error.
622    /// Otherwise, eats it.
623    pub fn expect_keyword(&mut self, exp: ExpKeywordPair) -> PResult<'a, ()> {
624        if !self.eat_keyword(exp) { self.unexpected() } else { Ok(()) }
625    }
626
627    /// Consume a sequence produced by a metavar expansion, if present.
628    pub fn eat_metavar_seq<T>(
629        &mut self,
630        mv_kind: MetaVarKind,
631        f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
632    ) -> Option<T> {
633        self.eat_metavar_seq_with_matcher(|mvk| mvk == mv_kind, f)
634    }
635
636    /// A slightly more general form of `eat_metavar_seq`, for use with the
637    /// `MetaVarKind` variants that have parameters, where an exact match isn't
638    /// desired.
639    fn eat_metavar_seq_with_matcher<T>(
640        &mut self,
641        match_mv_kind: impl Fn(MetaVarKind) -> bool,
642        mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
643    ) -> Option<T> {
644        if let token::OpenInvisible(InvisibleOrigin::MetaVar(mv_kind)) = self.token.kind
645            && match_mv_kind(mv_kind)
646        {
647            self.bump();
648
649            // Recovery is disabled when parsing macro arguments, so it must
650            // also be disabled when reparsing pasted macro arguments,
651            // otherwise we get inconsistent results (e.g. #137874).
652            let res = self.with_recovery(Recovery::Forbidden, |this| f(this));
653
654            let res = match res {
655                Ok(res) => res,
656                Err(err) => {
657                    // This can occur in unusual error cases, e.g. #139445.
658                    err.delay_as_bug();
659                    return None;
660                }
661            };
662
663            if let token::CloseInvisible(InvisibleOrigin::MetaVar(mv_kind)) = self.token.kind
664                && match_mv_kind(mv_kind)
665            {
666                self.bump();
667                Some(res)
668            } else {
669                // This can occur when invalid syntax is passed to a decl macro. E.g. see #139248,
670                // where the reparse attempt of an invalid expr consumed the trailing invisible
671                // delimiter.
672                self.dcx()
673                    .span_delayed_bug(self.token.span, "no close delim with reparsing {mv_kind:?}");
674                None
675            }
676        } else {
677            None
678        }
679    }
680
681    /// Is the given keyword `kw` followed by a non-reserved identifier?
682    fn is_kw_followed_by_ident(&self, kw: Symbol) -> bool {
683        self.token.is_keyword(kw) && self.look_ahead(1, |t| t.is_non_reserved_ident())
684    }
685
686    #[inline]
687    fn check_or_expected(&mut self, ok: bool, token_type: TokenType) -> bool {
688        if !ok {
689            self.expected_token_types.insert(token_type);
690        }
691        ok
692    }
693
694    fn check_ident(&mut self) -> bool {
695        self.check_or_expected(self.token.is_ident(), TokenType::Ident)
696    }
697
698    fn check_path(&mut self) -> bool {
699        self.check_or_expected(self.token.is_path_start(), TokenType::Path)
700    }
701
702    fn check_type(&mut self) -> bool {
703        self.check_or_expected(self.token.can_begin_type(), TokenType::Type)
704    }
705
706    fn check_const_arg(&mut self) -> bool {
707        let is_mcg_arg = self.check_or_expected(self.token.can_begin_const_arg(), TokenType::Const);
708        let is_mgca_arg = self.is_keyword_ahead(0, &[kw::Const])
709            && self.look_ahead(1, |t| *t == token::OpenBrace);
710        is_mcg_arg || is_mgca_arg
711    }
712
713    fn check_const_closure(&self) -> bool {
714        self.is_keyword_ahead(0, &[kw::Const])
715            && self.look_ahead(1, |t| match &t.kind {
716                // async closures do not work with const closures, so we do not parse that here.
717                token::Ident(kw::Move | kw::Use | kw::Static, IdentIsRaw::No)
718                | token::OrOr
719                | token::Or => true,
720                _ => false,
721            })
722    }
723
724    fn check_inline_const(&self, dist: usize) -> bool {
725        self.is_keyword_ahead(dist, &[kw::Const])
726            && self.look_ahead(dist + 1, |t| match &t.kind {
727                token::OpenBrace => true,
728                token::OpenInvisible(InvisibleOrigin::MetaVar(MetaVarKind::Block)) => true,
729                _ => false,
730            })
731    }
732
733    /// Checks to see if the next token is either `+` or `+=`.
734    /// Otherwise returns `false`.
735    #[inline]
736    fn check_plus(&mut self) -> bool {
737        self.check_or_expected(self.token.is_like_plus(), TokenType::Plus)
738    }
739
740    /// Eats the expected token if it's present possibly breaking
741    /// compound tokens like multi-character operators in process.
742    /// Returns `true` if the token was eaten.
743    fn break_and_eat(&mut self, exp: ExpTokenPair) -> bool {
744        if self.token == exp.tok {
745            self.bump();
746            return true;
747        }
748        match self.token.kind.break_two_token_op(1) {
749            Some((first, second)) if first == exp.tok => {
750                let first_span = self.psess.source_map().start_point(self.token.span);
751                let second_span = self.token.span.with_lo(first_span.hi());
752                self.token = Token::new(first, first_span);
753                // Keep track of this token - if we end token capturing now,
754                // we'll want to append this token to the captured stream.
755                //
756                // If we consume any additional tokens, then this token
757                // is not needed (we'll capture the entire 'glued' token),
758                // and `bump` will set this field to 0.
759                self.break_last_token += 1;
760                // Use the spacing of the glued token as the spacing of the
761                // unglued second token.
762                self.bump_with((Token::new(second, second_span), self.token_spacing));
763                true
764            }
765            _ => {
766                self.expected_token_types.insert(exp.token_type);
767                false
768            }
769        }
770    }
771
772    /// Eats `+` possibly breaking tokens like `+=` in process.
773    fn eat_plus(&mut self) -> bool {
774        self.break_and_eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Plus,
    token_type: crate::parser::token_type::TokenType::Plus,
}exp!(Plus))
775    }
776
777    /// Eats `&` possibly breaking tokens like `&&` in process.
778    /// Signals an error if `&` is not eaten.
779    fn expect_and(&mut self) -> PResult<'a, ()> {
780        if self.break_and_eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::And,
    token_type: crate::parser::token_type::TokenType::And,
}exp!(And)) { Ok(()) } else { self.unexpected() }
781    }
782
783    /// Eats `|` possibly breaking tokens like `||` in process.
784    /// Signals an error if `|` was not eaten.
785    fn expect_or(&mut self) -> PResult<'a, ()> {
786        if self.break_and_eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Or,
    token_type: crate::parser::token_type::TokenType::Or,
}exp!(Or)) { Ok(()) } else { self.unexpected() }
787    }
788
789    /// Eats `<` possibly breaking tokens like `<<` in process.
790    fn eat_lt(&mut self) -> bool {
791        let ate = self.break_and_eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Lt,
    token_type: crate::parser::token_type::TokenType::Lt,
}exp!(Lt));
792        if ate {
793            // See doc comment for `unmatched_angle_bracket_count`.
794            self.unmatched_angle_bracket_count += 1;
795            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/parser/mod.rs:795",
                        "rustc_parse::parser", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(795u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_parse::parser"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("eat_lt: (increment) count={0:?}",
                                                    self.unmatched_angle_bracket_count) as &dyn Value))])
            });
    } else { ; }
};debug!("eat_lt: (increment) count={:?}", self.unmatched_angle_bracket_count);
796        }
797        ate
798    }
799
800    /// Eats `<` possibly breaking tokens like `<<` in process.
801    /// Signals an error if `<` was not eaten.
802    fn expect_lt(&mut self) -> PResult<'a, ()> {
803        if self.eat_lt() { Ok(()) } else { self.unexpected() }
804    }
805
806    /// Eats `>` possibly breaking tokens like `>>` in process.
807    /// Signals an error if `>` was not eaten.
808    fn expect_gt(&mut self) -> PResult<'a, ()> {
809        if self.break_and_eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Gt,
    token_type: crate::parser::token_type::TokenType::Gt,
}exp!(Gt)) {
810            // See doc comment for `unmatched_angle_bracket_count`.
811            if self.unmatched_angle_bracket_count > 0 {
812                self.unmatched_angle_bracket_count -= 1;
813                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/parser/mod.rs:813",
                        "rustc_parse::parser", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/parser/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(813u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_parse::parser"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("expect_gt: (decrement) count={0:?}",
                                                    self.unmatched_angle_bracket_count) as &dyn Value))])
            });
    } else { ; }
};debug!("expect_gt: (decrement) count={:?}", self.unmatched_angle_bracket_count);
814            }
815            Ok(())
816        } else {
817            self.unexpected()
818        }
819    }
820
821    /// Checks if the next token is contained within `closes`, and returns `true` if so.
822    fn expect_any_with_type(
823        &mut self,
824        closes_expected: &[ExpTokenPair],
825        closes_not_expected: &[&TokenKind],
826    ) -> bool {
827        closes_expected.iter().any(|&close| self.check(close))
828            || closes_not_expected.iter().any(|k| self.check_noexpect(k))
829    }
830
831    /// Parses a sequence until the specified delimiters. The function
832    /// `f` must consume tokens until reaching the next separator or
833    /// closing bracket.
834    fn parse_seq_to_before_tokens<T>(
835        &mut self,
836        closes_expected: &[ExpTokenPair],
837        closes_not_expected: &[&TokenKind],
838        sep: SeqSep,
839        mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
840    ) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
841        let mut first = true;
842        let mut recovered = Recovered::No;
843        let mut trailing = Trailing::No;
844        let mut v = ThinVec::new();
845
846        while !self.expect_any_with_type(closes_expected, closes_not_expected) {
847            if self.token.kind.is_close_delim_or_eof() {
848                break;
849            }
850            if let Some(exp) = sep.sep {
851                if first {
852                    // no separator for the first element
853                    first = false;
854                } else {
855                    // check for separator
856                    match self.expect(exp) {
857                        Ok(Recovered::No) => {
858                            self.current_closure.take();
859                        }
860                        Ok(Recovered::Yes(guar)) => {
861                            self.current_closure.take();
862                            recovered = Recovered::Yes(guar);
863                            break;
864                        }
865                        Err(mut expect_err) => {
866                            let sp = self.prev_token.span.shrink_to_hi();
867                            let token_str = pprust::token_kind_to_string(&exp.tok);
868
869                            match self.current_closure.take() {
870                                Some(closure_spans) if self.token == TokenKind::Semi => {
871                                    // Finding a semicolon instead of a comma
872                                    // after a closure body indicates that the
873                                    // closure body may be a block but the user
874                                    // forgot to put braces around its
875                                    // statements.
876
877                                    self.recover_missing_braces_around_closure_body(
878                                        closure_spans,
879                                        expect_err,
880                                    )?;
881
882                                    continue;
883                                }
884
885                                _ => {
886                                    // Attempt to keep parsing if it was a similar separator.
887                                    if exp.tok.similar_tokens().contains(&self.token.kind) {
888                                        self.bump();
889                                    }
890                                }
891                            }
892
893                            // If this was a missing `@` in a binding pattern
894                            // bail with a suggestion
895                            // https://github.com/rust-lang/rust/issues/72373
896                            if self.prev_token.is_ident() && self.token == token::DotDot {
897                                let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("if you meant to bind the contents of the rest of the array pattern into `{0}`, use `@`",
                pprust::token_to_string(&self.prev_token)))
    })format!(
898                                    "if you meant to bind the contents of the rest of the array \
899                                     pattern into `{}`, use `@`",
900                                    pprust::token_to_string(&self.prev_token)
901                                );
902                                expect_err
903                                    .with_span_suggestion_verbose(
904                                        self.prev_token.span.shrink_to_hi().until(self.token.span),
905                                        msg,
906                                        " @ ",
907                                        Applicability::MaybeIncorrect,
908                                    )
909                                    .emit();
910                                break;
911                            }
912
913                            // Attempt to keep parsing if it was an omitted separator.
914                            self.last_unexpected_token_span = None;
915                            match f(self) {
916                                Ok(t) => {
917                                    // Parsed successfully, therefore most probably the code only
918                                    // misses a separator.
919                                    expect_err
920                                        .with_span_suggestion_short(
921                                            sp,
922                                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("missing `{0}`", token_str))
    })format!("missing `{token_str}`"),
923                                            token_str,
924                                            Applicability::MaybeIncorrect,
925                                        )
926                                        .emit();
927
928                                    v.push(t);
929                                    continue;
930                                }
931                                Err(e) => {
932                                    // Parsing failed, therefore it must be something more serious
933                                    // than just a missing separator.
934                                    for xx in &e.children {
935                                        // Propagate the help message from sub error `e` to main
936                                        // error `expect_err`.
937                                        expect_err.children.push(xx.clone());
938                                    }
939                                    e.cancel();
940                                    if self.token == token::Colon {
941                                        // We will try to recover in
942                                        // `maybe_recover_struct_lit_bad_delims`.
943                                        return Err(expect_err);
944                                    } else if let [exp] = closes_expected
945                                        && exp.token_type == TokenType::CloseParen
946                                    {
947                                        return Err(expect_err);
948                                    } else {
949                                        expect_err.emit();
950                                        break;
951                                    }
952                                }
953                            }
954                        }
955                    }
956                }
957            }
958            if sep.trailing_sep_allowed
959                && self.expect_any_with_type(closes_expected, closes_not_expected)
960            {
961                trailing = Trailing::Yes;
962                break;
963            }
964
965            let t = f(self)?;
966            v.push(t);
967        }
968
969        Ok((v, trailing, recovered))
970    }
971
972    fn recover_missing_braces_around_closure_body(
973        &mut self,
974        closure_spans: ClosureSpans,
975        mut expect_err: Diag<'_>,
976    ) -> PResult<'a, ()> {
977        let initial_semicolon = self.token.span;
978
979        while self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Semi,
    token_type: crate::parser::token_type::TokenType::Semi,
}exp!(Semi)) {
980            let _ = self
981                .parse_stmt_without_recovery(false, ForceCollect::No, false)
982                .unwrap_or_else(|e| {
983                    e.cancel();
984                    None
985                });
986        }
987
988        expect_err
989            .primary_message("closure bodies that contain statements must be surrounded by braces");
990
991        let preceding_pipe_span = closure_spans.closing_pipe;
992        let following_token_span = self.token.span;
993
994        let mut first_note = MultiSpan::from(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [initial_semicolon]))vec![initial_semicolon]);
995        first_note.push_span_label(
996            initial_semicolon,
997            "this `;` turns the preceding closure into a statement",
998        );
999        first_note.push_span_label(
1000            closure_spans.body,
1001            "this expression is a statement because of the trailing semicolon",
1002        );
1003        expect_err.span_note(first_note, "statement found outside of a block");
1004
1005        let mut second_note = MultiSpan::from(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [closure_spans.whole_closure]))vec![closure_spans.whole_closure]);
1006        second_note.push_span_label(closure_spans.whole_closure, "this is the parsed closure...");
1007        second_note.push_span_label(
1008            following_token_span,
1009            "...but likely you meant the closure to end here",
1010        );
1011        expect_err.span_note(second_note, "the closure body may be incorrectly delimited");
1012
1013        expect_err.span(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [preceding_pipe_span, following_token_span]))vec![preceding_pipe_span, following_token_span]);
1014
1015        let opening_suggestion_str = " {".to_string();
1016        let closing_suggestion_str = "}".to_string();
1017
1018        expect_err.multipart_suggestion(
1019            "try adding braces",
1020            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(preceding_pipe_span.shrink_to_hi(), opening_suggestion_str),
                (following_token_span.shrink_to_lo(),
                    closing_suggestion_str)]))vec![
1021                (preceding_pipe_span.shrink_to_hi(), opening_suggestion_str),
1022                (following_token_span.shrink_to_lo(), closing_suggestion_str),
1023            ],
1024            Applicability::MaybeIncorrect,
1025        );
1026
1027        expect_err.emit();
1028
1029        Ok(())
1030    }
1031
1032    /// Parses a sequence, not including the delimiters. The function
1033    /// `f` must consume tokens until reaching the next separator or
1034    /// closing bracket.
1035    fn parse_seq_to_before_end<T>(
1036        &mut self,
1037        close: ExpTokenPair,
1038        sep: SeqSep,
1039        f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1040    ) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
1041        self.parse_seq_to_before_tokens(&[close], &[], sep, f)
1042    }
1043
1044    /// Parses a sequence, including only the closing delimiter. The function
1045    /// `f` must consume tokens until reaching the next separator or
1046    /// closing bracket.
1047    fn parse_seq_to_end<T>(
1048        &mut self,
1049        close: ExpTokenPair,
1050        sep: SeqSep,
1051        f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1052    ) -> PResult<'a, (ThinVec<T>, Trailing)> {
1053        let (val, trailing, recovered) = self.parse_seq_to_before_end(close, sep, f)?;
1054        if #[allow(non_exhaustive_omitted_patterns)] match recovered {
    Recovered::No => true,
    _ => false,
}matches!(recovered, Recovered::No) && !self.eat(close) {
1055            self.dcx().span_delayed_bug(
1056                self.token.span,
1057                "recovered but `parse_seq_to_before_end` did not give us the close token",
1058            );
1059        }
1060        Ok((val, trailing))
1061    }
1062
1063    /// Parses a sequence, including both delimiters. The function
1064    /// `f` must consume tokens until reaching the next separator or
1065    /// closing bracket.
1066    fn parse_unspanned_seq<T>(
1067        &mut self,
1068        open: ExpTokenPair,
1069        close: ExpTokenPair,
1070        sep: SeqSep,
1071        f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1072    ) -> PResult<'a, (ThinVec<T>, Trailing)> {
1073        self.expect(open)?;
1074        self.parse_seq_to_end(close, sep, f)
1075    }
1076
1077    /// Parses a comma-separated sequence, including both delimiters.
1078    /// The function `f` must consume tokens until reaching the next separator or
1079    /// closing bracket.
1080    fn parse_delim_comma_seq<T>(
1081        &mut self,
1082        open: ExpTokenPair,
1083        close: ExpTokenPair,
1084        f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1085    ) -> PResult<'a, (ThinVec<T>, Trailing)> {
1086        self.parse_unspanned_seq(open, close, SeqSep::trailing_allowed(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma)), f)
1087    }
1088
1089    /// Parses a comma-separated sequence delimited by parentheses (e.g. `(x, y)`).
1090    /// The function `f` must consume tokens until reaching the next separator or
1091    /// closing bracket.
1092    pub fn parse_paren_comma_seq<T>(
1093        &mut self,
1094        f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1095    ) -> PResult<'a, (ThinVec<T>, Trailing)> {
1096        self.parse_delim_comma_seq(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen), crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen), f)
1097    }
1098
1099    /// Advance the parser by one token using provided token as the next one.
1100    fn bump_with(&mut self, next: (Token, Spacing)) {
1101        self.inlined_bump_with(next)
1102    }
1103
1104    /// This always-inlined version should only be used on hot code paths.
1105    #[inline(always)]
1106    fn inlined_bump_with(&mut self, (next_token, next_spacing): (Token, Spacing)) {
1107        // Update the current and previous tokens.
1108        self.prev_token = mem::replace(&mut self.token, next_token);
1109        self.token_spacing = next_spacing;
1110
1111        // Diagnostics.
1112        self.expected_token_types.clear();
1113    }
1114
1115    /// Advance the parser by one token.
1116    pub fn bump(&mut self) {
1117        // Note: destructuring here would give nicer code, but it was found in #96210 to be slower
1118        // than `.0`/`.1` access.
1119        let mut next = self.token_cursor.inlined_next();
1120        self.num_bump_calls += 1;
1121        // We got a token from the underlying cursor and no longer need to
1122        // worry about an unglued token. See `break_and_eat` for more details.
1123        self.break_last_token = 0;
1124        if next.0.span.is_dummy() {
1125            // Tweak the location for better diagnostics, but keep syntactic context intact.
1126            let fallback_span = self.token.span;
1127            next.0.span = fallback_span.with_ctxt(next.0.span.ctxt());
1128        }
1129        if true {
    if !!#[allow(non_exhaustive_omitted_patterns)] match next.0.kind {
                    token::OpenInvisible(origin) | token::CloseInvisible(origin)
                        if origin.skip() => true,
                    _ => false,
                } {
        ::core::panicking::panic("assertion failed: !matches!(next.0.kind, token::OpenInvisible(origin) |\n        token::CloseInvisible(origin) if origin.skip())")
    };
};debug_assert!(!matches!(
1130            next.0.kind,
1131            token::OpenInvisible(origin) | token::CloseInvisible(origin) if origin.skip()
1132        ));
1133        self.inlined_bump_with(next)
1134    }
1135
1136    /// Look-ahead `dist` tokens of `self.token` and get access to that token there.
1137    /// When `dist == 0` then the current token is looked at. `Eof` will be
1138    /// returned if the look-ahead is any distance past the end of the tokens.
1139    pub fn look_ahead<R>(&self, dist: usize, looker: impl FnOnce(&Token) -> R) -> R {
1140        if dist == 0 {
1141            return looker(&self.token);
1142        }
1143
1144        // Typically around 98% of the `dist > 0` cases have `dist == 1`, so we
1145        // have a fast special case for that.
1146        if dist == 1 {
1147            // The index is zero because the tree cursor's index always points
1148            // to the next token to be gotten.
1149            match self.token_cursor.curr.curr() {
1150                Some(tree) => {
1151                    // Indexing stayed within the current token tree.
1152                    match tree {
1153                        TokenTree::Token(token, _) => return looker(token),
1154                        &TokenTree::Delimited(dspan, _, delim, _) => {
1155                            if !delim.skip() {
1156                                return looker(&Token::new(delim.as_open_token_kind(), dspan.open));
1157                            }
1158                        }
1159                    }
1160                }
1161                None => {
1162                    // The tree cursor lookahead went (one) past the end of the
1163                    // current token tree. Try to return a close delimiter.
1164                    if let Some(last) = self.token_cursor.stack.last()
1165                        && let Some(&TokenTree::Delimited(span, _, delim, _)) = last.curr()
1166                        && !delim.skip()
1167                    {
1168                        // We are not in the outermost token stream, so we have
1169                        // delimiters. Also, those delimiters are not skipped.
1170                        return looker(&Token::new(delim.as_close_token_kind(), span.close));
1171                    }
1172                }
1173            }
1174        }
1175
1176        // Just clone the token cursor and use `next`, skipping delimiters as
1177        // necessary. Slow but simple.
1178        let mut cursor = self.token_cursor.clone();
1179        let mut i = 0;
1180        let mut token = Token::dummy();
1181        while i < dist {
1182            token = cursor.next().0;
1183            if let token::OpenInvisible(origin) | token::CloseInvisible(origin) = token.kind
1184                && origin.skip()
1185            {
1186                continue;
1187            }
1188            i += 1;
1189        }
1190        looker(&token)
1191    }
1192
1193    /// Like `lookahead`, but skips over token trees rather than tokens. Useful
1194    /// when looking past possible metavariable pasting sites.
1195    pub fn tree_look_ahead<R>(
1196        &self,
1197        dist: usize,
1198        looker: impl FnOnce(&TokenTree) -> R,
1199    ) -> Option<R> {
1200        match (&dist, &0) {
    (left_val, right_val) => {
        if *left_val == *right_val {
            let kind = ::core::panicking::AssertKind::Ne;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_ne!(dist, 0);
1201        self.token_cursor.curr.look_ahead(dist - 1).map(looker)
1202    }
1203
1204    /// Returns whether any of the given keywords are `dist` tokens ahead of the current one.
1205    pub(crate) fn is_keyword_ahead(&self, dist: usize, kws: &[Symbol]) -> bool {
1206        self.look_ahead(dist, |t| kws.iter().any(|&kw| t.is_keyword(kw)))
1207    }
1208
1209    /// Parses asyncness: `async` or nothing.
1210    fn parse_coroutine_kind(&mut self, case: Case) -> Option<CoroutineKind> {
1211        let span = self.token_uninterpolated_span();
1212        if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Async,
    token_type: crate::parser::token_type::TokenType::KwAsync,
}exp!(Async), case) {
1213            // FIXME(gen_blocks): Do we want to unconditionally parse `gen` and then
1214            // error if edition <= 2024, like we do with async and edition <= 2018?
1215            if self.token_uninterpolated_span().at_least_rust_2024()
1216                && self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Gen,
    token_type: crate::parser::token_type::TokenType::KwGen,
}exp!(Gen), case)
1217            {
1218                let gen_span = self.prev_token_uninterpolated_span();
1219                Some(CoroutineKind::AsyncGen {
1220                    span: span.to(gen_span),
1221                    closure_id: DUMMY_NODE_ID,
1222                    return_impl_trait_id: DUMMY_NODE_ID,
1223                })
1224            } else {
1225                Some(CoroutineKind::Async {
1226                    span,
1227                    closure_id: DUMMY_NODE_ID,
1228                    return_impl_trait_id: DUMMY_NODE_ID,
1229                })
1230            }
1231        } else if self.token_uninterpolated_span().at_least_rust_2024()
1232            && self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Gen,
    token_type: crate::parser::token_type::TokenType::KwGen,
}exp!(Gen), case)
1233        {
1234            Some(CoroutineKind::Gen {
1235                span,
1236                closure_id: DUMMY_NODE_ID,
1237                return_impl_trait_id: DUMMY_NODE_ID,
1238            })
1239        } else {
1240            None
1241        }
1242    }
1243
1244    /// Parses fn unsafety: `unsafe`, `safe` or nothing.
1245    fn parse_safety(&mut self, case: Case) -> Safety {
1246        if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Unsafe,
    token_type: crate::parser::token_type::TokenType::KwUnsafe,
}exp!(Unsafe), case) {
1247            Safety::Unsafe(self.prev_token_uninterpolated_span())
1248        } else if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Safe,
    token_type: crate::parser::token_type::TokenType::KwSafe,
}exp!(Safe), case) {
1249            Safety::Safe(self.prev_token_uninterpolated_span())
1250        } else {
1251            Safety::Default
1252        }
1253    }
1254
1255    /// Parses constness: `const` or nothing.
1256    fn parse_constness(&mut self, case: Case) -> Const {
1257        self.parse_constness_(case, false)
1258    }
1259
1260    /// Parses constness for closures (case sensitive, feature-gated)
1261    fn parse_closure_constness(&mut self) -> Const {
1262        let constness = self.parse_constness_(Case::Sensitive, true);
1263        if let Const::Yes(span) = constness {
1264            self.psess.gated_spans.gate(sym::const_closures, span);
1265        }
1266        constness
1267    }
1268
1269    fn parse_constness_(&mut self, case: Case, is_closure: bool) -> Const {
1270        // Avoid const blocks and const closures to be parsed as const items
1271        if (self.check_const_closure() == is_closure)
1272            && !self.look_ahead(1, |t| *t == token::OpenBrace || t.is_metavar_block())
1273            && self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Const,
    token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const), case)
1274        {
1275            Const::Yes(self.prev_token_uninterpolated_span())
1276        } else {
1277            Const::No
1278        }
1279    }
1280
1281    /// Parses inline const expressions.
1282    fn parse_const_block(&mut self, span: Span, pat: bool) -> PResult<'a, Box<Expr>> {
1283        self.expect_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Const,
    token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const))?;
1284        let (attrs, blk) = self.parse_inner_attrs_and_block(None)?;
1285        let anon_const = AnonConst {
1286            id: DUMMY_NODE_ID,
1287            value: self.mk_expr(blk.span, ExprKind::Block(blk, None)),
1288            mgca_disambiguation: MgcaDisambiguation::AnonConst,
1289        };
1290        let blk_span = anon_const.value.span;
1291        let kind = if pat {
1292            let guar = self
1293                .dcx()
1294                .struct_span_err(blk_span, "const blocks cannot be used as patterns")
1295                .with_help(
1296                    "use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead",
1297                )
1298                .emit();
1299            ExprKind::Err(guar)
1300        } else {
1301            ExprKind::ConstBlock(anon_const)
1302        };
1303        Ok(self.mk_expr_with_attrs(span.to(blk_span), kind, attrs))
1304    }
1305
1306    /// Parses mutability (`mut` or nothing).
1307    fn parse_mutability(&mut self) -> Mutability {
1308        if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Mut,
    token_type: crate::parser::token_type::TokenType::KwMut,
}exp!(Mut)) { Mutability::Mut } else { Mutability::Not }
1309    }
1310
1311    /// Parses reference binding mode (`ref`, `ref mut`, `ref pin const`, `ref pin mut`, or nothing).
1312    fn parse_byref(&mut self) -> ByRef {
1313        if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Ref,
    token_type: crate::parser::token_type::TokenType::KwRef,
}exp!(Ref)) {
1314            let (pinnedness, mutability) = self.parse_pin_and_mut();
1315            ByRef::Yes(pinnedness, mutability)
1316        } else {
1317            ByRef::No
1318        }
1319    }
1320
1321    /// Possibly parses mutability (`const` or `mut`).
1322    fn parse_const_or_mut(&mut self) -> Option<Mutability> {
1323        if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Mut,
    token_type: crate::parser::token_type::TokenType::KwMut,
}exp!(Mut)) {
1324            Some(Mutability::Mut)
1325        } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Const,
    token_type: crate::parser::token_type::TokenType::KwConst,
}exp!(Const)) {
1326            Some(Mutability::Not)
1327        } else {
1328            None
1329        }
1330    }
1331
1332    fn parse_field_name(&mut self) -> PResult<'a, Ident> {
1333        if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) = self.token.kind
1334        {
1335            if let Some(suffix) = suffix {
1336                self.dcx().emit_err(errors::InvalidLiteralSuffixOnTupleIndex {
1337                    span: self.token.span,
1338                    suffix,
1339                });
1340            }
1341            self.bump();
1342            Ok(Ident::new(symbol, self.prev_token.span))
1343        } else {
1344            self.parse_ident_common(true)
1345        }
1346    }
1347
1348    fn parse_delim_args(&mut self) -> PResult<'a, Box<DelimArgs>> {
1349        if let Some(args) = self.parse_delim_args_inner() {
1350            Ok(Box::new(args))
1351        } else {
1352            self.unexpected_any()
1353        }
1354    }
1355
1356    fn parse_attr_args(&mut self) -> PResult<'a, AttrArgs> {
1357        Ok(if let Some(args) = self.parse_delim_args_inner() {
1358            AttrArgs::Delimited(args)
1359        } else if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Eq,
    token_type: crate::parser::token_type::TokenType::Eq,
}exp!(Eq)) {
1360            let eq_span = self.prev_token.span;
1361            let expr = self.parse_expr_force_collect()?;
1362            AttrArgs::Eq { eq_span, expr }
1363        } else {
1364            AttrArgs::Empty
1365        })
1366    }
1367
1368    fn parse_delim_args_inner(&mut self) -> Option<DelimArgs> {
1369        let delimited = self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen))
1370            || self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenBracket,
    token_type: crate::parser::token_type::TokenType::OpenBracket,
}exp!(OpenBracket))
1371            || self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenBrace,
    token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace));
1372
1373        delimited.then(|| {
1374            let TokenTree::Delimited(dspan, _, delim, tokens) = self.parse_token_tree() else {
1375                ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
1376            };
1377            DelimArgs { dspan, delim, tokens }
1378        })
1379    }
1380
1381    /// Parses a single token tree from the input.
1382    pub fn parse_token_tree(&mut self) -> TokenTree {
1383        if self.token.kind.open_delim().is_some() {
1384            // Clone the `TokenTree::Delimited` that we are currently
1385            // within. That's what we are going to return.
1386            let tree = self.token_cursor.stack.last().unwrap().curr().unwrap().clone();
1387            if true {
    match tree {
        TokenTree::Delimited(..) => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "TokenTree::Delimited(..)", ::core::option::Option::None);
        }
    };
};debug_assert_matches!(tree, TokenTree::Delimited(..));
1388
1389            // Advance the token cursor through the entire delimited
1390            // sequence. After getting the `OpenDelim` we are *within* the
1391            // delimited sequence, i.e. at depth `d`. After getting the
1392            // matching `CloseDelim` we are *after* the delimited sequence,
1393            // i.e. at depth `d - 1`.
1394            let target_depth = self.token_cursor.stack.len() - 1;
1395
1396            if let Capturing::No = self.capture_state.capturing {
1397                // We are not capturing tokens, so skip to the end of the
1398                // delimited sequence. This is a perf win when dealing with
1399                // declarative macros that pass large `tt` fragments through
1400                // multiple rules, as seen in the uom-0.37.0 crate.
1401                self.token_cursor.curr.bump_to_end();
1402                self.bump();
1403                if true {
    match (&self.token_cursor.stack.len(), &target_depth) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(self.token_cursor.stack.len(), target_depth);
1404            } else {
1405                loop {
1406                    // Advance one token at a time, so `TokenCursor::next()`
1407                    // can capture these tokens if necessary.
1408                    self.bump();
1409                    if self.token_cursor.stack.len() == target_depth {
1410                        break;
1411                    }
1412                }
1413            }
1414            if true {
    if !self.token.kind.close_delim().is_some() {
        ::core::panicking::panic("assertion failed: self.token.kind.close_delim().is_some()")
    };
};debug_assert!(self.token.kind.close_delim().is_some());
1415
1416            // Consume close delimiter
1417            self.bump();
1418            tree
1419        } else {
1420            if !!self.token.kind.is_close_delim_or_eof() {
    ::core::panicking::panic("assertion failed: !self.token.kind.is_close_delim_or_eof()")
};assert!(!self.token.kind.is_close_delim_or_eof());
1421            let prev_spacing = self.token_spacing;
1422            self.bump();
1423            TokenTree::Token(self.prev_token, prev_spacing)
1424        }
1425    }
1426
1427    pub fn parse_tokens(&mut self) -> TokenStream {
1428        let mut result = Vec::new();
1429        loop {
1430            if self.token.kind.is_close_delim_or_eof() {
1431                break;
1432            } else {
1433                result.push(self.parse_token_tree());
1434            }
1435        }
1436        TokenStream::new(result)
1437    }
1438
1439    /// Evaluates the closure with restrictions in place.
1440    ///
1441    /// Afters the closure is evaluated, restrictions are reset.
1442    fn with_res<T>(&mut self, res: Restrictions, f: impl FnOnce(&mut Self) -> T) -> T {
1443        let old = self.restrictions;
1444        self.restrictions = res;
1445        let res = f(self);
1446        self.restrictions = old;
1447        res
1448    }
1449
1450    /// Parses `pub` and `pub(in path)` plus shortcuts `pub(crate)` for `pub(in crate)`, `pub(self)`
1451    /// for `pub(in self)` and `pub(super)` for `pub(in super)`.
1452    /// If the following element can't be a tuple (i.e., it's a function definition), then
1453    /// it's not a tuple struct field), and the contents within the parentheses aren't valid,
1454    /// so emit a proper diagnostic.
1455    // Public for rustfmt usage.
1456    pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> {
1457        if let Some(vis) = self
1458            .eat_metavar_seq(MetaVarKind::Vis, |this| this.parse_visibility(FollowedByType::Yes))
1459        {
1460            return Ok(vis);
1461        }
1462
1463        if !self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Pub,
    token_type: crate::parser::token_type::TokenType::KwPub,
}exp!(Pub)) {
1464            // We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
1465            // keyword to grab a span from for inherited visibility; an empty span at the
1466            // beginning of the current token would seem to be the "Schelling span".
1467            return Ok(Visibility {
1468                span: self.token.span.shrink_to_lo(),
1469                kind: VisibilityKind::Inherited,
1470                tokens: None,
1471            });
1472        }
1473        let lo = self.prev_token.span;
1474
1475        if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
1476            // We don't `self.bump()` the `(` yet because this might be a struct definition where
1477            // `()` or a tuple might be allowed. For example, `struct Struct(pub (), pub (usize));`.
1478            // Because of this, we only `bump` the `(` if we're assured it is appropriate to do so
1479            // by the following tokens.
1480            if self.is_keyword_ahead(1, &[kw::In]) {
1481                // Parse `pub(in path)`.
1482                self.bump(); // `(`
1483                self.bump(); // `in`
1484                let path = self.parse_path(PathStyle::Mod)?; // `path`
1485                self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?; // `)`
1486                let vis = VisibilityKind::Restricted {
1487                    path: Box::new(path),
1488                    id: ast::DUMMY_NODE_ID,
1489                    shorthand: false,
1490                };
1491                return Ok(Visibility {
1492                    span: lo.to(self.prev_token.span),
1493                    kind: vis,
1494                    tokens: None,
1495                });
1496            } else if self.look_ahead(2, |t| t == &token::CloseParen)
1497                && self.is_keyword_ahead(1, &[kw::Crate, kw::Super, kw::SelfLower])
1498            {
1499                // Parse `pub(crate)`, `pub(self)`, or `pub(super)`.
1500                self.bump(); // `(`
1501                let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
1502                self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?; // `)`
1503                let vis = VisibilityKind::Restricted {
1504                    path: Box::new(path),
1505                    id: ast::DUMMY_NODE_ID,
1506                    shorthand: true,
1507                };
1508                return Ok(Visibility {
1509                    span: lo.to(self.prev_token.span),
1510                    kind: vis,
1511                    tokens: None,
1512                });
1513            } else if let FollowedByType::No = fbt {
1514                // Provide this diagnostic if a type cannot follow;
1515                // in particular, if this is not a tuple struct.
1516                self.recover_incorrect_vis_restriction()?;
1517                // Emit diagnostic, but continue with public visibility.
1518            }
1519        }
1520
1521        Ok(Visibility { span: lo, kind: VisibilityKind::Public, tokens: None })
1522    }
1523
1524    /// Recovery for e.g. `pub(something) fn ...` or `struct X { pub(something) y: Z }`
1525    fn recover_incorrect_vis_restriction(&mut self) -> PResult<'a, ()> {
1526        self.bump(); // `(`
1527        let path = self.parse_path(PathStyle::Mod)?;
1528        self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?; // `)`
1529
1530        let path_str = pprust::path_to_string(&path);
1531        self.dcx()
1532            .emit_err(IncorrectVisibilityRestriction { span: path.span, inner_str: path_str });
1533
1534        Ok(())
1535    }
1536
1537    /// Parses an optional `impl` restriction.
1538    /// Enforces the `impl_restriction` feature gate whenever an explicit restriction is encountered.
1539    fn parse_impl_restriction(&mut self) -> PResult<'a, ImplRestriction> {
1540        if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Impl,
    token_type: crate::parser::token_type::TokenType::KwImpl,
}exp!(Impl)) {
1541            let lo = self.prev_token.span;
1542            // No units or tuples are allowed to follow `impl` here, so we can safely bump `(`.
1543            self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen))?;
1544            if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::In,
    token_type: crate::parser::token_type::TokenType::KwIn,
}exp!(In)) {
1545                let path = self.parse_path(PathStyle::Mod)?; // `in path`
1546                self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?; // `)`
1547                let restriction = RestrictionKind::Restricted {
1548                    path: Box::new(path),
1549                    id: ast::DUMMY_NODE_ID,
1550                    shorthand: false,
1551                };
1552                let span = lo.to(self.prev_token.span);
1553                self.psess.gated_spans.gate(sym::impl_restriction, span);
1554                return Ok(ImplRestriction { kind: restriction, span, tokens: None });
1555            } else if self.look_ahead(1, |t| t == &token::CloseParen)
1556                && self.is_keyword_ahead(0, &[kw::Crate, kw::Super, kw::SelfLower])
1557            {
1558                let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
1559                self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?; // `)`
1560                let restriction = RestrictionKind::Restricted {
1561                    path: Box::new(path),
1562                    id: ast::DUMMY_NODE_ID,
1563                    shorthand: true,
1564                };
1565                let span = lo.to(self.prev_token.span);
1566                self.psess.gated_spans.gate(sym::impl_restriction, span);
1567                return Ok(ImplRestriction { kind: restriction, span, tokens: None });
1568            } else {
1569                self.recover_incorrect_impl_restriction(lo)?;
1570                // Emit diagnostic, but continue with no impl restriction.
1571            }
1572        }
1573        Ok(ImplRestriction {
1574            kind: RestrictionKind::Unrestricted,
1575            span: self.token.span.shrink_to_lo(),
1576            tokens: None,
1577        })
1578    }
1579
1580    /// Recovery for e.g. `impl(something) trait`
1581    fn recover_incorrect_impl_restriction(&mut self, lo: Span) -> PResult<'a, ()> {
1582        let path = self.parse_path(PathStyle::Mod)?;
1583        self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?; // `)`
1584        let path_str = pprust::path_to_string(&path);
1585        self.dcx().emit_err(IncorrectImplRestriction { span: path.span, inner_str: path_str });
1586        let end = self.prev_token.span;
1587        self.psess.gated_spans.gate(sym::impl_restriction, lo.to(end));
1588        Ok(())
1589    }
1590
1591    /// Parses `extern string_literal?`.
1592    fn parse_extern(&mut self, case: Case) -> Extern {
1593        if self.eat_keyword_case(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Extern,
    token_type: crate::parser::token_type::TokenType::KwExtern,
}exp!(Extern), case) {
1594            let mut extern_span = self.prev_token.span;
1595            let abi = self.parse_abi();
1596            if let Some(abi) = abi {
1597                extern_span = extern_span.to(abi.span);
1598            }
1599            Extern::from_abi(abi, extern_span)
1600        } else {
1601            Extern::None
1602        }
1603    }
1604
1605    /// Parses a string literal as an ABI spec.
1606    fn parse_abi(&mut self) -> Option<StrLit> {
1607        match self.parse_str_lit() {
1608            Ok(str_lit) => Some(str_lit),
1609            Err(Some(lit)) => match lit.kind {
1610                ast::LitKind::Err(_) => None,
1611                _ => {
1612                    self.dcx().emit_err(NonStringAbiLiteral { span: lit.span });
1613                    None
1614                }
1615            },
1616            Err(None) => None,
1617        }
1618    }
1619
1620    fn collect_tokens_no_attrs<R: HasAttrs + HasTokens>(
1621        &mut self,
1622        f: impl FnOnce(&mut Self) -> PResult<'a, R>,
1623    ) -> PResult<'a, R> {
1624        // The only reason to call `collect_tokens_no_attrs` is if you want tokens, so use
1625        // `ForceCollect::Yes`
1626        self.collect_tokens(None, AttrWrapper::empty(), ForceCollect::Yes, |this, _attrs| {
1627            Ok((f(this)?, Trailing::No, UsePreAttrPos::No))
1628        })
1629    }
1630
1631    /// Checks for `::` or, potentially, `:::` and then look ahead after it.
1632    fn check_path_sep_and_look_ahead(&mut self, looker: impl Fn(&Token) -> bool) -> bool {
1633        if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::PathSep,
    token_type: crate::parser::token_type::TokenType::PathSep,
}exp!(PathSep)) {
1634            if self.may_recover() && self.look_ahead(1, |t| t.kind == token::Colon) {
1635                if true {
    if !!self.look_ahead(1, &looker) {
        {
            ::core::panicking::panic_fmt(format_args!("Looker must not match on colon"));
        }
    };
};debug_assert!(!self.look_ahead(1, &looker), "Looker must not match on colon");
1636                self.look_ahead(2, looker)
1637            } else {
1638                self.look_ahead(1, looker)
1639            }
1640        } else {
1641            false
1642        }
1643    }
1644
1645    /// `::{` or `::*`
1646    fn is_import_coupler(&mut self) -> bool {
1647        self.check_path_sep_and_look_ahead(|t| #[allow(non_exhaustive_omitted_patterns)] match t.kind {
    token::OpenBrace | token::Star => true,
    _ => false,
}matches!(t.kind, token::OpenBrace | token::Star))
1648    }
1649
1650    // Debug view of the parser's token stream, up to `{lookahead}` tokens.
1651    // Only used when debugging.
1652    #[allow(unused)]
1653    pub(crate) fn debug_lookahead(&self, lookahead: usize) -> impl fmt::Debug {
1654        fmt::from_fn(move |f| {
1655            let mut dbg_fmt = f.debug_struct("Parser"); // or at least, one view of
1656
1657            // we don't need N spans, but we want at least one, so print all of prev_token
1658            dbg_fmt.field("prev_token", &self.prev_token);
1659            let mut tokens = ::alloc::vec::Vec::new()vec![];
1660            for i in 0..lookahead {
1661                let tok = self.look_ahead(i, |tok| tok.kind);
1662                let is_eof = tok == TokenKind::Eof;
1663                tokens.push(tok);
1664                if is_eof {
1665                    // Don't look ahead past EOF.
1666                    break;
1667                }
1668            }
1669            dbg_fmt.field_with("tokens", |field| field.debug_list().entries(tokens).finish());
1670            dbg_fmt.field("approx_token_stream_pos", &self.num_bump_calls);
1671
1672            // some fields are interesting for certain values, as they relate to macro parsing
1673            if let Some(subparser) = self.subparser_name {
1674                dbg_fmt.field("subparser_name", &subparser);
1675            }
1676            if let Recovery::Forbidden = self.recovery {
1677                dbg_fmt.field("recovery", &self.recovery);
1678            }
1679
1680            // imply there's "more to know" than this view
1681            dbg_fmt.finish_non_exhaustive()
1682        })
1683    }
1684
1685    pub fn clear_expected_token_types(&mut self) {
1686        self.expected_token_types.clear();
1687    }
1688
1689    pub fn approx_token_stream_pos(&self) -> u32 {
1690        self.num_bump_calls
1691    }
1692
1693    /// For interpolated `self.token`, returns a span of the fragment to which
1694    /// the interpolated token refers. For all other tokens this is just a
1695    /// regular span. It is particularly important to use this for identifiers
1696    /// and lifetimes for which spans affect name resolution and edition
1697    /// checks. Note that keywords are also identifiers, so they should use
1698    /// this if they keep spans or perform edition checks.
1699    pub fn token_uninterpolated_span(&self) -> Span {
1700        match &self.token.kind {
1701            token::NtIdent(ident, _) | token::NtLifetime(ident, _) => ident.span,
1702            token::OpenInvisible(InvisibleOrigin::MetaVar(_)) => self.look_ahead(1, |t| t.span),
1703            _ => self.token.span,
1704        }
1705    }
1706
1707    /// Like `token_uninterpolated_span`, but works on `self.prev_token`.
1708    pub fn prev_token_uninterpolated_span(&self) -> Span {
1709        match &self.prev_token.kind {
1710            token::NtIdent(ident, _) | token::NtLifetime(ident, _) => ident.span,
1711            token::OpenInvisible(InvisibleOrigin::MetaVar(_)) => self.look_ahead(0, |t| t.span),
1712            _ => self.prev_token.span,
1713        }
1714    }
1715
1716    fn missing_semi_from_binop(
1717        &self,
1718        kind_desc: &str,
1719        expr: &Expr,
1720        decl_lo: Option<Span>,
1721    ) -> Option<(Span, ErrorGuaranteed)> {
1722        if self.token == TokenKind::Semi {
1723            return None;
1724        }
1725        if !self.may_recover() || expr.span.from_expansion() {
1726            return None;
1727        }
1728        let sm = self.psess.source_map();
1729        if let ExprKind::Binary(op, lhs, rhs) = &expr.kind
1730            && sm.is_multiline(lhs.span.shrink_to_hi().until(rhs.span.shrink_to_lo()))
1731            && #[allow(non_exhaustive_omitted_patterns)] match op.node {
    BinOpKind::Mul | BinOpKind::BitAnd => true,
    _ => false,
}matches!(op.node, BinOpKind::Mul | BinOpKind::BitAnd)
1732            && classify::expr_requires_semi_to_be_stmt(rhs)
1733        {
1734            let lhs_end_span = lhs.span.shrink_to_hi();
1735            let token_str = token_descr(&self.token);
1736            let mut err = self
1737                .dcx()
1738                .struct_span_err(lhs_end_span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected `;`, found {0}",
                token_str))
    })format!("expected `;`, found {token_str}"));
1739            err.span_label(self.token.span, "unexpected token");
1740
1741            // Use the declaration start if provided, otherwise fall back to lhs_end_span.
1742            let continuation_start = decl_lo.unwrap_or(lhs_end_span);
1743            let continuation_span = continuation_start.until(rhs.span.shrink_to_hi());
1744            err.span_label(
1745                continuation_span,
1746                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("to finish parsing this {0}, expected this to be followed by a `;`",
                kind_desc))
    })format!(
1747                    "to finish parsing this {kind_desc}, expected this to be followed by a `;`",
1748                ),
1749            );
1750            let op_desc = match op.node {
1751                BinOpKind::BitAnd => "a bit-and",
1752                BinOpKind::Mul => "a multiplication",
1753                _ => "a binary",
1754            };
1755            let mut note_spans = MultiSpan::new();
1756            note_spans.push_span_label(lhs.span, "parsed as the left-hand expression");
1757            note_spans.push_span_label(rhs.span, "parsed as the right-hand expression");
1758            note_spans.push_span_label(op.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this was parsed as {0}", op_desc))
    })format!("this was parsed as {op_desc}"));
1759            err.span_note(
1760                note_spans,
1761                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the {0} was parsed as having {1} binary expression",
                kind_desc, op_desc))
    })format!("the {kind_desc} was parsed as having {op_desc} binary expression"),
1762            );
1763
1764            err.span_suggestion(
1765                lhs_end_span,
1766                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you may have meant to write a `;` to terminate the {0} earlier",
                kind_desc))
    })format!("you may have meant to write a `;` to terminate the {kind_desc} earlier"),
1767                ";",
1768                Applicability::MaybeIncorrect,
1769            );
1770            return Some((lhs.span, err.emit()));
1771        }
1772        None
1773    }
1774}
1775
1776// Metavar captures of various kinds.
1777#[derive(#[automatically_derived]
impl ::core::clone::Clone for ParseNtResult {
    #[inline]
    fn clone(&self) -> ParseNtResult {
        match self {
            ParseNtResult::Tt(__self_0) =>
                ParseNtResult::Tt(::core::clone::Clone::clone(__self_0)),
            ParseNtResult::Ident(__self_0, __self_1) =>
                ParseNtResult::Ident(::core::clone::Clone::clone(__self_0),
                    ::core::clone::Clone::clone(__self_1)),
            ParseNtResult::Lifetime(__self_0, __self_1) =>
                ParseNtResult::Lifetime(::core::clone::Clone::clone(__self_0),
                    ::core::clone::Clone::clone(__self_1)),
            ParseNtResult::Item(__self_0) =>
                ParseNtResult::Item(::core::clone::Clone::clone(__self_0)),
            ParseNtResult::Block(__self_0) =>
                ParseNtResult::Block(::core::clone::Clone::clone(__self_0)),
            ParseNtResult::Stmt(__self_0) =>
                ParseNtResult::Stmt(::core::clone::Clone::clone(__self_0)),
            ParseNtResult::Pat(__self_0, __self_1) =>
                ParseNtResult::Pat(::core::clone::Clone::clone(__self_0),
                    ::core::clone::Clone::clone(__self_1)),
            ParseNtResult::Expr(__self_0, __self_1) =>
                ParseNtResult::Expr(::core::clone::Clone::clone(__self_0),
                    ::core::clone::Clone::clone(__self_1)),
            ParseNtResult::Literal(__self_0) =>
                ParseNtResult::Literal(::core::clone::Clone::clone(__self_0)),
            ParseNtResult::Ty(__self_0) =>
                ParseNtResult::Ty(::core::clone::Clone::clone(__self_0)),
            ParseNtResult::Meta(__self_0) =>
                ParseNtResult::Meta(::core::clone::Clone::clone(__self_0)),
            ParseNtResult::Path(__self_0) =>
                ParseNtResult::Path(::core::clone::Clone::clone(__self_0)),
            ParseNtResult::Vis(__self_0) =>
                ParseNtResult::Vis(::core::clone::Clone::clone(__self_0)),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ParseNtResult {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            ParseNtResult::Tt(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Tt",
                    &__self_0),
            ParseNtResult::Ident(__self_0, __self_1) =>
                ::core::fmt::Formatter::debug_tuple_field2_finish(f, "Ident",
                    __self_0, &__self_1),
            ParseNtResult::Lifetime(__self_0, __self_1) =>
                ::core::fmt::Formatter::debug_tuple_field2_finish(f,
                    "Lifetime", __self_0, &__self_1),
            ParseNtResult::Item(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Item",
                    &__self_0),
            ParseNtResult::Block(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Block",
                    &__self_0),
            ParseNtResult::Stmt(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Stmt",
                    &__self_0),
            ParseNtResult::Pat(__self_0, __self_1) =>
                ::core::fmt::Formatter::debug_tuple_field2_finish(f, "Pat",
                    __self_0, &__self_1),
            ParseNtResult::Expr(__self_0, __self_1) =>
                ::core::fmt::Formatter::debug_tuple_field2_finish(f, "Expr",
                    __self_0, &__self_1),
            ParseNtResult::Literal(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "Literal", &__self_0),
            ParseNtResult::Ty(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Ty",
                    &__self_0),
            ParseNtResult::Meta(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Meta",
                    &__self_0),
            ParseNtResult::Path(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Path",
                    &__self_0),
            ParseNtResult::Vis(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Vis",
                    &__self_0),
        }
    }
}Debug)]
1778pub enum ParseNtResult {
1779    Tt(TokenTree),
1780    Ident(Ident, IdentIsRaw),
1781    Lifetime(Ident, IdentIsRaw),
1782    Item(Box<ast::Item>),
1783    Block(Box<ast::Block>),
1784    Stmt(Box<ast::Stmt>),
1785    Pat(Box<ast::Pat>, NtPatKind),
1786    Expr(Box<ast::Expr>, NtExprKind),
1787    Literal(Box<ast::Expr>),
1788    Ty(Box<ast::Ty>),
1789    Meta(Box<ast::AttrItem>),
1790    Path(Box<ast::Path>),
1791    Vis(Box<ast::Visibility>),
1792}