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