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