compiler/rustc_parse/src/parser/diagnostics.rs RUST 3,166 lines View on github.com → Search inside
File is large — showing lines 1–2,000 of 3,166.
1use std::mem::take;2use std::ops::{Deref, DerefMut};34use ast::token::IdentIsRaw;5use rustc_ast::token::{self, Lit, LitKind, Token, TokenKind};6use rustc_ast::util::parser::AssocOp;7use rustc_ast::{8    self as ast, AngleBracketedArg, AngleBracketedArgs, AnonConst, AttrVec, BinOpKind, BindingMode,9    Block, BlockCheckMode, Expr, ExprKind, GenericArg, Generics, Item, ItemKind,10    MgcaDisambiguation, Param, Pat, PatKind, Path, PathSegment, QSelf, Recovered, Ty, TyKind,11};12use rustc_ast_pretty::pprust;13use rustc_data_structures::fx::FxHashSet;14use rustc_errors::{15    Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, PResult, Subdiagnostic, Suggestions, msg,16    pluralize,17};18use rustc_session::errors::ExprParenthesesNeeded;19use rustc_span::symbol::used_keywords;20use rustc_span::{BytePos, DUMMY_SP, Ident, Span, SpanSnippetError, Spanned, Symbol, kw, sym};21use thin_vec::{ThinVec, thin_vec};22use tracing::{debug, trace};2324use super::pat::Expected;25use super::{26    BlockMode, CommaRecoveryMode, ExpTokenPair, Parser, PathStyle, Restrictions, SemiColonMode,27    SeqSep, TokenType,28};29use crate::errors::{30    AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AsyncUseBlockIn2015, AttributeOnParamType,31    AwaitSuggestion, BadQPathStage2, BadTypePlus, BadTypePlusSub, ColonAsSemi,32    ComparisonOperatorsCannotBeChained, ComparisonOperatorsCannotBeChainedSugg,33    DocCommentDoesNotDocumentAnything, DocCommentOnParamType, DoubleColonInBound,34    ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg, GenericParamsWithoutAngleBrackets,35    GenericParamsWithoutAngleBracketsSugg, HelpIdentifierStartsWithNumber, HelpUseLatestEdition,36    InInTypo, IncorrectAwait, IncorrectSemicolon, IncorrectUseOfAwait, IncorrectUseOfUse,37    MisspelledKw, PatternMethodParamWithoutBody, QuestionMarkInType, QuestionMarkInTypeSugg,38    SelfParamNotFirst, StructLiteralBodyWithoutPath, StructLiteralBodyWithoutPathSugg,39    SuggAddMissingLetStmt, SuggEscapeIdentifier, SuggRemoveComma, TernaryOperator,40    TernaryOperatorSuggestion, UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,41    UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead, WrapType,42};43use crate::exp;44use crate::parser::FnContext;45use crate::parser::attr::InnerAttrPolicy;46use crate::parser::item::IsDotDotDot;4748/// Creates a placeholder argument.49pub(super) fn dummy_arg(ident: Ident, guar: ErrorGuaranteed) -> Param {50    let pat = Box::new(Pat {51        id: ast::DUMMY_NODE_ID,52        kind: PatKind::Ident(BindingMode::NONE, ident, None),53        span: ident.span,54        tokens: None,55    });56    let ty = Ty { kind: TyKind::Err(guar), span: ident.span, id: ast::DUMMY_NODE_ID, tokens: None };57    Param {58        attrs: AttrVec::default(),59        id: ast::DUMMY_NODE_ID,60        pat,61        span: ident.span,62        ty: Box::new(ty),63        is_placeholder: false,64    }65}6667pub(super) trait RecoverQPath: Sized + 'static {68    const PATH_STYLE: PathStyle = PathStyle::Expr;69    fn to_ty(&self) -> Option<Box<Ty>>;70    fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self;71}7273impl<T: RecoverQPath> RecoverQPath for Box<T> {74    const PATH_STYLE: PathStyle = T::PATH_STYLE;75    fn to_ty(&self) -> Option<Box<Ty>> {76        T::to_ty(self)77    }78    fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {79        Box::new(T::recovered(qself, path))80    }81}8283impl RecoverQPath for Ty {84    const PATH_STYLE: PathStyle = PathStyle::Type;85    fn to_ty(&self) -> Option<Box<Ty>> {86        Some(Box::new(self.clone()))87    }88    fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {89        Self {90            span: path.span,91            kind: TyKind::Path(qself, path),92            id: ast::DUMMY_NODE_ID,93            tokens: None,94        }95    }96}9798impl RecoverQPath for Pat {99    const PATH_STYLE: PathStyle = PathStyle::Pat;100    fn to_ty(&self) -> Option<Box<Ty>> {101        self.to_ty()102    }103    fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {104        Self {105            span: path.span,106            kind: PatKind::Path(qself, path),107            id: ast::DUMMY_NODE_ID,108            tokens: None,109        }110    }111}112113impl RecoverQPath for Expr {114    fn to_ty(&self) -> Option<Box<Ty>> {115        self.to_ty()116    }117    fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {118        Self {119            span: path.span,120            kind: ExprKind::Path(qself, path),121            attrs: AttrVec::new(),122            id: ast::DUMMY_NODE_ID,123            tokens: None,124        }125    }126}127128/// Control whether the closing delimiter should be consumed when calling `Parser::consume_block`.129pub(crate) enum ConsumeClosingDelim {130    Yes,131    No,132}133134#[derive(Clone, Copy)]135pub enum AttemptLocalParseRecovery {136    Yes,137    No,138}139140impl AttemptLocalParseRecovery {141    pub(super) fn yes(&self) -> bool {142        match self {143            AttemptLocalParseRecovery::Yes => true,144            AttemptLocalParseRecovery::No => false,145        }146    }147148    pub(super) fn no(&self) -> bool {149        match self {150            AttemptLocalParseRecovery::Yes => false,151            AttemptLocalParseRecovery::No => true,152        }153    }154}155156/// Information for emitting suggestions and recovering from157/// C-style `i++`, `--i`, etc.158#[derive(Debug, Copy, Clone)]159struct IncDecRecovery {160    /// Is this increment/decrement its own statement?161    standalone: IsStandalone,162    /// Is this an increment or decrement?163    op: IncOrDec,164    /// Is this pre- or postfix?165    fixity: UnaryFixity,166}167168/// Is an increment or decrement expression its own statement?169#[derive(Debug, Copy, Clone)]170enum IsStandalone {171    /// It's standalone, i.e., its own statement.172    Standalone,173    /// It's a subexpression, i.e., *not* standalone.174    Subexpr,175}176177#[derive(Debug, Copy, Clone, PartialEq, Eq)]178enum IncOrDec {179    Inc,180    Dec,181}182183#[derive(Debug, Copy, Clone, PartialEq, Eq)]184enum UnaryFixity {185    Pre,186    Post,187}188189impl IncOrDec {190    fn chr(&self) -> char {191        match self {192            Self::Inc => '+',193            Self::Dec => '-',194        }195    }196197    fn name(&self) -> &'static str {198        match self {199            Self::Inc => "increment",200            Self::Dec => "decrement",201        }202    }203}204205impl std::fmt::Display for UnaryFixity {206    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {207        match self {208            Self::Pre => write!(f, "prefix"),209            Self::Post => write!(f, "postfix"),210        }211    }212}213214/// Checks if the given `lookup` identifier is similar to any keyword symbol in `candidates`.215///216/// This is a specialized version of [`Symbol::find_similar`] that constructs an error when a217/// candidate is found.218fn find_similar_kw(lookup: Ident, candidates: &[Symbol]) -> Option<MisspelledKw> {219    lookup.name.find_similar(candidates).map(|(similar_kw, is_incorrect_case)| MisspelledKw {220        similar_kw: similar_kw.to_string(),221        is_incorrect_case,222        span: lookup.span,223    })224}225226struct MultiSugg {227    msg: String,228    patches: Vec<(Span, String)>,229    applicability: Applicability,230}231232impl MultiSugg {233    fn emit(self, err: &mut Diag<'_>) {234        err.multipart_suggestion(self.msg, self.patches, self.applicability);235    }236237    fn emit_verbose(self, err: &mut Diag<'_>) {238        err.multipart_suggestion(self.msg, self.patches, self.applicability);239    }240}241242/// SnapshotParser is used to create a snapshot of the parser243/// without causing duplicate errors being emitted when the `Parser`244/// is dropped.245pub struct SnapshotParser<'a> {246    parser: Parser<'a>,247}248249impl<'a> Deref for SnapshotParser<'a> {250    type Target = Parser<'a>;251252    fn deref(&self) -> &Self::Target {253        &self.parser254    }255}256257impl<'a> DerefMut for SnapshotParser<'a> {258    fn deref_mut(&mut self) -> &mut Self::Target {259        &mut self.parser260    }261}262263impl<'a> Parser<'a> {264    pub fn dcx(&self) -> DiagCtxtHandle<'a> {265        self.psess.dcx()266    }267268    /// Replace `self` with `snapshot.parser`.269    pub fn restore_snapshot(&mut self, snapshot: SnapshotParser<'a>) {270        *self = snapshot.parser;271    }272273    /// Create a snapshot of the `Parser`.274    pub fn create_snapshot_for_diagnostic(&self) -> SnapshotParser<'a> {275        let snapshot = self.clone();276        SnapshotParser { parser: snapshot }277    }278279    pub(super) fn span_to_snippet(&self, span: Span) -> Result<String, SpanSnippetError> {280        self.psess.source_map().span_to_snippet(span)281    }282283    /// Emits an error with suggestions if an identifier was expected but not found.284    ///285    /// Returns a possibly recovered identifier.286    pub(super) fn expected_ident_found(287        &mut self,288        recover: bool,289    ) -> PResult<'a, (Ident, IdentIsRaw)> {290        let valid_follow = &[291            TokenKind::Eq,292            TokenKind::Colon,293            TokenKind::Comma,294            TokenKind::Semi,295            TokenKind::PathSep,296            TokenKind::OpenBrace,297            TokenKind::OpenParen,298            TokenKind::CloseBrace,299            TokenKind::CloseParen,300        ];301        if let TokenKind::DocComment(..) = self.prev_token.kind302            && valid_follow.contains(&self.token.kind)303        {304            let err = self.dcx().create_err(DocCommentDoesNotDocumentAnything {305                span: self.prev_token.span,306                missing_comma: None,307            });308            return Err(err);309        }310311        let mut recovered_ident = None;312        // we take this here so that the correct original token is retained in313        // the diagnostic, regardless of eager recovery.314        let bad_token = self.token;315316        // suggest prepending a keyword in identifier position with `r#`317        let suggest_raw = if let Some((ident, IdentIsRaw::No)) = self.token.ident()318            && ident.is_raw_guess()319            && self.look_ahead(1, |t| valid_follow.contains(&t.kind))320        {321            recovered_ident = Some((ident, IdentIsRaw::Yes));322323            // `Symbol::to_string()` is different from `Symbol::into_diag_arg()`,324            // which uses `Symbol::to_ident_string()` and "helpfully" adds an implicit `r#`325            let ident_name = ident.name.to_string();326327            Some(SuggEscapeIdentifier { span: ident.span.shrink_to_lo(), ident_name })328        } else {329            None330        };331332        let suggest_remove_comma =333            if self.token == token::Comma && self.look_ahead(1, |t| t.is_ident()) {334                if recover {335                    self.bump();336                    recovered_ident = self.ident_or_err(false).ok();337                };338339                Some(SuggRemoveComma { span: bad_token.span })340            } else {341                None342            };343344        let help_cannot_start_number = self.is_lit_bad_ident().map(|(len, valid_portion)| {345            let (invalid, valid) = self.token.span.split_at(len as u32);346347            recovered_ident = Some((Ident::new(valid_portion, valid), IdentIsRaw::No));348349            HelpIdentifierStartsWithNumber { num_span: invalid }350        });351352        let err = ExpectedIdentifier {353            span: bad_token.span,354            token: bad_token,355            suggest_raw,356            suggest_remove_comma,357            help_cannot_start_number,358        };359        let mut err = self.dcx().create_err(err);360361        // if the token we have is a `<`362        // it *might* be a misplaced generic363        // FIXME: could we recover with this?364        if self.token == token::Lt {365            // all keywords that could have generic applied366            let valid_prev_keywords =367                [kw::Fn, kw::Type, kw::Struct, kw::Enum, kw::Union, kw::Trait];368369            // If we've expected an identifier,370            // and the current token is a '<'371            // if the previous token is a valid keyword372            // that might use a generic, then suggest a correct373            // generic placement (later on)374            let maybe_keyword = self.prev_token;375            if valid_prev_keywords.into_iter().any(|x| maybe_keyword.is_keyword(x)) {376                // if we have a valid keyword, attempt to parse generics377                // also obtain the keywords symbol378                match self.parse_generics() {379                    Ok(generic) => {380                        if let TokenKind::Ident(symbol, _) = maybe_keyword.kind {381                            let ident_name = symbol;382                            // at this point, we've found something like383                            // `fn <T>id`384                            // and current token should be Ident with the item name (i.e. the function name)385                            // if there is a `<` after the fn name, then don't show a suggestion, show help386387                            if !self.look_ahead(1, |t| *t == token::Lt)388                                && let Ok(snippet) =389                                    self.psess.source_map().span_to_snippet(generic.span)390                            {391                                err.multipart_suggestion(392                                        format!("place the generic parameter name after the {ident_name} name"),393                                        vec![394                                            (self.token.span.shrink_to_hi(), snippet),395                                            (generic.span, String::new())396                                        ],397                                        Applicability::MaybeIncorrect,398                                    );399                            } else {400                                err.help(format!(401                                    "place the generic parameter name after the {ident_name} name"402                                ));403                            }404                        }405                    }406                    Err(err) => {407                        // if there's an error parsing the generics,408                        // then don't do a misplaced generics suggestion409                        // and emit the expected ident error instead;410                        err.cancel();411                    }412                }413            }414        }415416        if let Some(recovered_ident) = recovered_ident417            && recover418        {419            err.emit();420            Ok(recovered_ident)421        } else {422            Err(err)423        }424    }425426    pub(super) fn expected_ident_found_err(&mut self) -> Diag<'a> {427        self.expected_ident_found(false).unwrap_err()428    }429430    /// Checks if the current token is a integer or float literal and looks like431    /// it could be a invalid identifier with digits at the start.432    ///433    /// Returns the number of characters (bytes) composing the invalid portion434    /// of the identifier and the valid portion of the identifier.435    pub(super) fn is_lit_bad_ident(&mut self) -> Option<(usize, Symbol)> {436        // ensure that the integer literal is followed by a *invalid*437        // suffix: this is how we know that it is a identifier with an438        // invalid beginning.439        if let token::Literal(Lit {440            kind: token::LitKind::Integer | token::LitKind::Float,441            symbol,442            suffix: Some(suffix), // no suffix makes it a valid literal443        }) = self.token.kind444            && rustc_ast::MetaItemLit::from_token(&self.token).is_none()445        {446            Some((symbol.as_str().len(), suffix))447        } else {448            None449        }450    }451452    pub(super) fn expected_one_of_not_found(453        &mut self,454        edible: &[ExpTokenPair],455        inedible: &[ExpTokenPair],456    ) -> PResult<'a, ErrorGuaranteed> {457        debug!("expected_one_of_not_found(edible: {:?}, inedible: {:?})", edible, inedible);458        fn tokens_to_string(tokens: &[TokenType]) -> String {459            let mut i = tokens.iter();460            // This might be a sign we need a connect method on `Iterator`.461            let b = i.next().map_or_else(String::new, |t| t.to_string());462            i.enumerate().fold(b, |mut b, (i, a)| {463                if tokens.len() > 2 && i == tokens.len() - 2 {464                    b.push_str(", or ");465                } else if tokens.len() == 2 && i == tokens.len() - 2 {466                    b.push_str(" or ");467                } else {468                    b.push_str(", ");469                }470                b.push_str(&a.to_string());471                b472            })473        }474475        for exp in edible.iter().chain(inedible.iter()) {476            self.expected_token_types.insert(exp.token_type);477        }478        let mut expected: Vec<_> = self.expected_token_types.iter().collect();479        expected.sort_by_cached_key(|x| x.to_string());480        expected.dedup();481482        let sm = self.psess.source_map();483484        // Special-case "expected `;`" errors.485        if expected.contains(&TokenType::Semi) {486            // If the user is trying to write a ternary expression, recover it and487            // return an Err to prevent a cascade of irrelevant diagnostics.488            if self.prev_token == token::Question489                && let Err(e) = self.maybe_recover_from_ternary_operator(None)490            {491                return Err(e);492            }493494            if self.token.span == DUMMY_SP || self.prev_token.span == DUMMY_SP {495                // Likely inside a macro, can't provide meaningful suggestions.496            } else if !sm.is_multiline(self.prev_token.span.until(self.token.span)) {497                // The current token is in the same line as the prior token, not recoverable.498            } else if [token::Comma, token::Colon].contains(&self.token.kind)499                && self.prev_token == token::CloseParen500            {501                // Likely typo: The current token is on a new line and is expected to be502                // `.`, `;`, `?`, or an operator after a close delimiter token.503                //504                // let a = std::process::Command::new("echo")505                //         .arg("1")506                //         ,arg("2")507                //         ^508                // https://github.com/rust-lang/rust/issues/72253509            } else if self.look_ahead(1, |t| {510                t == &token::CloseBrace || t.can_begin_expr() && *t != token::Colon511            }) && [token::Comma, token::Colon].contains(&self.token.kind)512            {513                // Likely typo: `,` → `;` or `:` → `;`. This is triggered if the current token is514                // either `,` or `:`, and the next token could either start a new statement or is a515                // block close. For example:516                //517                //   let x = 32:518                //   let y = 42;519                let guar = self.dcx().emit_err(ExpectedSemi {520                    span: self.token.span,521                    token: self.token,522                    unexpected_token_label: None,523                    sugg: ExpectedSemiSugg::ChangeToSemi(self.token.span),524                });525                self.bump();526                return Ok(guar);527            } else if self.look_ahead(0, |t| {528                t == &token::CloseBrace529                    || ((t.can_begin_expr() || t.can_begin_item())530                        && t != &token::Semi531                        && t != &token::Pound)532                    // Avoid triggering with too many trailing `#` in raw string.533                    || (sm.is_multiline(534                        self.prev_token.span.shrink_to_hi().until(self.token.span.shrink_to_lo()),535                    ) && t == &token::Pound)536            }) && !expected.contains(&TokenType::Comma)537            {538                // Missing semicolon typo. This is triggered if the next token could either start a539                // new statement or is a block close. For example:540                //541                //   let x = 32542                //   let y = 42;543                let span = self.prev_token.span.shrink_to_hi();544                let guar = self.dcx().emit_err(ExpectedSemi {545                    span,546                    token: self.token,547                    unexpected_token_label: Some(self.token.span),548                    sugg: ExpectedSemiSugg::AddSemi(span),549                });550                return Ok(guar);551            }552        }553554        if self.token == TokenKind::EqEq555            && self.prev_token.is_ident()556            && expected.contains(&TokenType::Eq)557        {558            // Likely typo: `=` → `==` in let expr or enum item559            return Err(self.dcx().create_err(UseEqInstead { span: self.token.span }));560        }561562        if (self.token.is_keyword(kw::Move) || self.token.is_keyword(kw::Use))563            && self.prev_token.is_keyword(kw::Async)564        {565            // The 2015 edition is in use because parsing of `async move` or `async use` has failed.566            let span = self.prev_token.span.to(self.token.span);567            if self.token.is_keyword(kw::Move) {568                return Err(self.dcx().create_err(AsyncMoveBlockIn2015 { span }));569            } else {570                // kw::Use571                return Err(self.dcx().create_err(AsyncUseBlockIn2015 { span }));572            }573        }574575        let expect = tokens_to_string(&expected);576        let actual = super::token_descr(&self.token);577        let (msg_exp, (label_sp, label_exp)) = if expected.len() > 1 {578            let fmt = format!("expected one of {expect}, found {actual}");579            let short_expect = if expected.len() > 6 {580                format!("{} possible tokens", expected.len())581            } else {582                expect583            };584            (fmt, (self.prev_token.span.shrink_to_hi(), format!("expected one of {short_expect}")))585        } else if expected.is_empty() {586            (587                format!("unexpected token: {actual}"),588                (self.prev_token.span, "unexpected token after this".to_string()),589            )590        } else {591            (592                format!("expected {expect}, found {actual}"),593                (self.prev_token.span.shrink_to_hi(), format!("expected {expect}")),594            )595        };596        self.last_unexpected_token_span = Some(self.token.span);597        // FIXME: translation requires list formatting (for `expect`)598        let mut err = self.dcx().struct_span_err(self.token.span, msg_exp);599600        self.label_expected_raw_ref(&mut err);601602        // Look for usages of '=>' where '>=' was probably intended603        if self.token == token::FatArrow604            && expected.iter().any(|tok| matches!(tok, TokenType::Operator | TokenType::Le))605            && !expected.iter().any(|tok| matches!(tok, TokenType::FatArrow | TokenType::Comma))606        {607            err.span_suggestion(608                self.token.span,609                "you might have meant to write a \"greater than or equal to\" comparison",610                ">=",611                Applicability::MaybeIncorrect,612            );613        }614615        if let TokenKind::Ident(symbol, _) = &self.prev_token.kind {616            if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {617                err.span_suggestion_short(618                    self.prev_token.span,619                    format!("write `fn` instead of `{symbol}` to declare a function"),620                    "fn",621                    Applicability::MachineApplicable,622                );623            }624        }625626        if let TokenKind::Ident(prev, _) = &self.prev_token.kind627            && let TokenKind::Ident(cur, _) = &self.token.kind628        {629            let concat = Symbol::intern(&format!("{prev}{cur}"));630            let ident = Ident::new(concat, DUMMY_SP);631            if ident.is_used_keyword() || ident.is_reserved() || ident.is_raw_guess() {632                let concat_span = self.prev_token.span.to(self.token.span);633                err.span_suggestion_verbose(634                    concat_span,635                    format!("consider removing the space to spell keyword `{concat}`"),636                    concat,637                    Applicability::MachineApplicable,638                );639            }640        }641642        // Try to detect an intended c-string literal while using a pre-2021 edition. The heuristic643        // here is to identify a cooked, uninterpolated `c` id immediately followed by a string, or644        // a cooked, uninterpolated `cr` id immediately followed by a string or a `#`, in an edition645        // where c-string literals are not allowed. There is the very slight possibility of a false646        // positive for a `cr#` that wasn't intended to start a c-string literal, but identifying647        // that in the parser requires unbounded lookahead, so we only add a hint to the existing648        // error rather than replacing it entirely.649        if ((self.prev_token == TokenKind::Ident(sym::character('c'), IdentIsRaw::No)650            && matches!(&self.token.kind, TokenKind::Literal(token::Lit { kind: token::Str, .. })))651            || (self.prev_token == TokenKind::Ident(sym::cr, IdentIsRaw::No)652                && matches!(653                    &self.token.kind,654                    TokenKind::Literal(token::Lit { kind: token::Str, .. }) | token::Pound655                )))656            && self.prev_token.span.hi() == self.token.span.lo()657            && !self.token.span.at_least_rust_2021()658        {659            err.note("you may be trying to write a c-string literal");660            err.note("c-string literals require Rust 2021 or later");661            err.subdiagnostic(HelpUseLatestEdition::new());662        }663664        // `pub` may be used for an item or `pub(crate)`665        if self.prev_token.is_ident_named(sym::public)666            && (self.token.can_begin_item() || self.token == TokenKind::OpenParen)667        {668            err.span_suggestion_short(669                self.prev_token.span,670                "write `pub` instead of `public` to make the item public",671                "pub",672                Applicability::MachineApplicable,673            );674        }675676        if let token::DocComment(kind, style, _) = self.token.kind {677            // This is to avoid suggesting converting a doc comment to a regular comment678            // when missing a comma before the doc comment in lists (#142311):679            //680            // ```681            // enum Foo{682            //     A /// xxxxxxx683            //     B,684            // }685            // ```686            if !expected.contains(&TokenType::Comma) {687                // We have something like `expr //!val` where the user likely meant `expr // !val`688                let pos = self.token.span.lo() + BytePos(2);689                let span = self.token.span.with_lo(pos).with_hi(pos);690                err.span_suggestion_verbose(691                    span,692                    format!(693                        "add a space before {} to write a regular comment",694                        match (kind, style) {695                            (token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",696                            (token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",697                            (token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",698                            (token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",699                        },700                    ),701                    " ".to_string(),702                    Applicability::MaybeIncorrect,703                );704            }705        }706707        let sp = if self.token == token::Eof {708            // This is EOF; don't want to point at the following char, but rather the last token.709            self.prev_token.span710        } else {711            label_sp712        };713714        if self.check_too_many_raw_str_terminators(&mut err) {715            if expected.contains(&TokenType::Semi) && self.eat(exp!(Semi)) {716                let guar = err.emit();717                return Ok(guar);718            } else {719                return Err(err);720            }721        }722723        if self.prev_token.span == DUMMY_SP {724            // Account for macro context where the previous span might not be725            // available to avoid incorrect output (#54841).726            err.span_label(self.token.span, label_exp);727        } else if !sm.is_multiline(self.token.span.shrink_to_hi().until(sp.shrink_to_lo())) {728            // When the spans are in the same line, it means that the only content between729            // them is whitespace, point at the found token in that case:730            //731            // X |     () => { syntax error };732            //   |                    ^^^^^ expected one of 8 possible tokens here733            //734            // instead of having:735            //736            // X |     () => { syntax error };737            //   |                   -^^^^^ unexpected token738            //   |                   |739            //   |                   expected one of 8 possible tokens here740            err.span_label(self.token.span, label_exp);741        } else {742            err.span_label(sp, label_exp);743            err.span_label(self.token.span, "unexpected token");744        }745746        // Check for misspelled keywords if there are no suggestions added to the diagnostic.747        if let Suggestions::Enabled(list) = &err.suggestions748            && list.is_empty()749        {750            self.check_for_misspelled_kw(&mut err, &expected);751        }752        Err(err)753    }754755    pub(super) fn is_expected_raw_ref_mut(&self) -> bool {756        self.prev_token.is_keyword(kw::Raw)757            && self.expected_token_types.contains(TokenType::KwMut)758            && self.expected_token_types.contains(TokenType::KwConst)759            && self.token.can_begin_expr()760    }761762    /// Adds a label when `&raw EXPR` was written instead of `&raw const EXPR`/`&raw mut EXPR`.763    ///764    /// Given that not all parser diagnostics flow through `expected_one_of_not_found`, this765    /// label may need added to other diagnostics emission paths as needed.766    pub(super) fn label_expected_raw_ref(&mut self, err: &mut Diag<'_>) {767        if self.is_expected_raw_ref_mut() {768            err.span_suggestions(769                self.prev_token.span.shrink_to_hi(),770                "`&raw` must be followed by `const` or `mut` to be a raw reference expression",771                [" const".to_string(), " mut".to_string()],772                Applicability::MaybeIncorrect,773            );774        }775    }776777    /// Checks if the current token or the previous token are misspelled keywords778    /// and adds a helpful suggestion.779    fn check_for_misspelled_kw(&self, err: &mut Diag<'_>, expected: &[TokenType]) {780        let Some((curr_ident, _)) = self.token.ident() else {781            return;782        };783        let expected_token_types: &[TokenType] =784            expected.len().checked_sub(10).map_or(&expected, |index| &expected[index..]);785        let expected_keywords: Vec<Symbol> =786            expected_token_types.iter().filter_map(|token| token.is_keyword()).collect();787788        // When there are a few keywords in the last ten elements of `self.expected_token_types`789        // and the current token is an identifier, it's probably a misspelled keyword. This handles790        // code like `async Move {}`, misspelled `if` in match guard, misspelled `else` in791        // `if`-`else` and misspelled `where` in a where clause.792        if !expected_keywords.is_empty()793            && !curr_ident.is_used_keyword()794            && let Some(misspelled_kw) = find_similar_kw(curr_ident, &expected_keywords)795        {796            err.subdiagnostic(misspelled_kw);797            // We don't want other suggestions to be added as they are most likely meaningless798            // when there is a misspelled keyword.799            err.seal_suggestions();800        } else if let Some((prev_ident, _)) = self.prev_token.ident()801            && !prev_ident.is_used_keyword()802        {803            // We generate a list of all keywords at runtime rather than at compile time804            // so that it gets generated only when the diagnostic needs it.805            // Also, it is unlikely that this list is generated multiple times because the806            // parser halts after execution hits this path.807            let all_keywords = used_keywords(|| prev_ident.span.edition());808809            // Otherwise, check the previous token with all the keywords as possible candidates.810            // This handles code like `Struct Human;` and `While a < b {}`.811            // We check the previous token only when the current token is an identifier to avoid812            // false positives like suggesting keyword `for` for `extern crate foo {}`.813            if let Some(misspelled_kw) = find_similar_kw(prev_ident, &all_keywords) {814                err.subdiagnostic(misspelled_kw);815                // We don't want other suggestions to be added as they are most likely meaningless816                // when there is a misspelled keyword.817                err.seal_suggestions();818            }819        }820    }821822    /// The user has written `#[attr] expr` which is unsupported. (#106020)823    pub(super) fn attr_on_non_tail_expr(&self, expr: &Expr) -> ErrorGuaranteed {824        // Missing semicolon typo error.825        let span = self.prev_token.span.shrink_to_hi();826        let mut err = self.dcx().create_err(ExpectedSemi {827            span,828            token: self.token,829            unexpected_token_label: Some(self.token.span),830            sugg: ExpectedSemiSugg::AddSemi(span),831        });832        let attr_span = match &expr.attrs[..] {833            [] => unreachable!(),834            [only] => only.span,835            [first, rest @ ..] => {836                for attr in rest {837                    err.span_label(attr.span, "");838                }839                first.span840            }841        };842        err.span_label(843            attr_span,844            format!(845                "only `;` terminated statements or tail expressions are allowed after {}",846                if expr.attrs.len() == 1 { "this attribute" } else { "these attributes" },847            ),848        );849        if self.token == token::Pound && self.look_ahead(1, |t| *t == token::OpenBracket) {850            // We have851            // #[attr]852            // expr853            // #[not_attr]854            // other_expr855            err.span_label(span, "expected `;` here");856            err.multipart_suggestion(857                "alternatively, consider surrounding the expression with a block",858                vec![859                    (expr.span.shrink_to_lo(), "{ ".to_string()),860                    (expr.span.shrink_to_hi(), " }".to_string()),861                ],862                Applicability::MachineApplicable,863            );864865            // Special handling for `#[cfg(...)]` chains866            let mut snapshot = self.create_snapshot_for_diagnostic();867            if let [attr] = &expr.attrs[..]868                && let ast::AttrKind::Normal(attr_kind) = &attr.kind869                && let [segment] = &attr_kind.item.path.segments[..]870                && segment.ident.name == sym::cfg871                && let Some(args_span) = attr_kind.item.args.span()872                && let next_attr = match snapshot.parse_attribute(InnerAttrPolicy::Forbidden(None))873                {874                    Ok(next_attr) => next_attr,875                    Err(inner_err) => {876                        inner_err.cancel();877                        return err.emit();878                    }879                }880                && let ast::AttrKind::Normal(next_attr_kind) = next_attr.kind881                && let Some(next_attr_args_span) = next_attr_kind.item.args.span()882                && let [next_segment] = &next_attr_kind.item.path.segments[..]883                && segment.ident.name == sym::cfg884            {885                let next_expr = match snapshot.parse_expr() {886                    Ok(next_expr) => next_expr,887                    Err(inner_err) => {888                        inner_err.cancel();889                        return err.emit();890                    }891                };892                // We have for sure893                // #[cfg(..)]894                // expr895                // #[cfg(..)]896                // other_expr897                // So we suggest using `if cfg!(..) { expr } else if cfg!(..) { other_expr }`.898                let margin = self.psess.source_map().span_to_margin(next_expr.span).unwrap_or(0);899                let sugg = vec![900                    (attr.span.with_hi(segment.span().hi()), "if cfg!".to_string()),901                    (args_span.shrink_to_hi().with_hi(attr.span.hi()), " {".to_string()),902                    (expr.span.shrink_to_lo(), "    ".to_string()),903                    (904                        next_attr.span.with_hi(next_segment.span().hi()),905                        "} else if cfg!".to_string(),906                    ),907                    (908                        next_attr_args_span.shrink_to_hi().with_hi(next_attr.span.hi()),909                        " {".to_string(),910                    ),911                    (next_expr.span.shrink_to_lo(), "    ".to_string()),912                    (next_expr.span.shrink_to_hi(), format!("\n{}}}", " ".repeat(margin))),913                ];914                err.multipart_suggestion(915                    "it seems like you are trying to provide different expressions depending on \916                     `cfg`, consider using `if cfg!(..)`",917                    sugg,918                    Applicability::MachineApplicable,919                );920            }921        }922923        err.emit()924    }925926    fn check_too_many_raw_str_terminators(&mut self, err: &mut Diag<'_>) -> bool {927        let sm = self.psess.source_map();928        match (&self.prev_token.kind, &self.token.kind) {929            (930                TokenKind::Literal(Lit {931                    kind: LitKind::StrRaw(n_hashes) | LitKind::ByteStrRaw(n_hashes),932                    ..933                }),934                TokenKind::Pound,935            ) if !sm.is_multiline(936                self.prev_token.span.shrink_to_hi().until(self.token.span.shrink_to_lo()),937            ) =>938            {939                let n_hashes: u8 = *n_hashes;940                err.primary_message("too many `#` when terminating raw string");941                let str_span = self.prev_token.span;942                let mut span = self.token.span;943                let mut count = 0;944                while self.token == TokenKind::Pound945                    && !sm.is_multiline(span.shrink_to_hi().until(self.token.span.shrink_to_lo()))946                {947                    span = span.with_hi(self.token.span.hi());948                    self.bump();949                    count += 1;950                }951                err.span(span);952                err.span_suggestion(953                    span,954                    format!("remove the extra `#`{}", pluralize!(count)),955                    "",956                    Applicability::MachineApplicable,957                );958                err.span_label(959                    str_span,960                    format!("this raw string started with {n_hashes} `#`{}", pluralize!(n_hashes)),961                );962                true963            }964            _ => false,965        }966    }967968    pub(super) fn maybe_suggest_struct_literal(969        &mut self,970        lo: Span,971        s: BlockCheckMode,972        maybe_struct_name: token::Token,973    ) -> Option<PResult<'a, Box<Block>>> {974        if self.token.is_ident() && self.look_ahead(1, |t| t == &token::Colon) {975            // We might be having a struct literal where people forgot to include the path:976            // fn foo() -> Foo {977            //     field: value,978            // }979            debug!(?maybe_struct_name, ?self.token);980            let mut snapshot = self.create_snapshot_for_diagnostic();981            let path = Path {982                segments: ThinVec::new(),983                span: self.prev_token.span.shrink_to_lo(),984                tokens: None,985            };986            let struct_expr = snapshot.parse_expr_struct(None, path, false);987            let block_tail = self.parse_block_tail(lo, s, AttemptLocalParseRecovery::No);988            return Some(match (struct_expr, block_tail) {989                (Ok(expr), Err(err)) => {990                    // We have encountered the following:991                    // fn foo() -> Foo {992                    //     field: value,993                    // }994                    // Suggest:995                    // fn foo() -> Foo { Path {996                    //     field: value,997                    // } }998                    err.cancel();999                    self.restore_snapshot(snapshot);1000                    let guar = self.dcx().emit_err(StructLiteralBodyWithoutPath {1001                        span: expr.span,1002                        sugg: StructLiteralBodyWithoutPathSugg {1003                            before: expr.span.shrink_to_lo(),1004                            after: expr.span.shrink_to_hi(),1005                        },1006                    });1007                    Ok(self.mk_block(1008                        thin_vec![self.mk_stmt_err(expr.span, guar)],1009                        s,1010                        lo.to(self.prev_token.span),1011                    ))1012                }1013                (Err(err), Ok(tail)) => {1014                    // We have a block tail that contains a somehow valid expr.1015                    err.cancel();1016                    Ok(tail)1017                }1018                (Err(snapshot_err), Err(err)) => {1019                    // We don't know what went wrong, emit the normal error.1020                    snapshot_err.cancel();1021                    self.consume_block(exp!(OpenBrace), exp!(CloseBrace), ConsumeClosingDelim::Yes);1022                    Err(err)1023                }1024                (Ok(_), Ok(tail)) => Ok(tail),1025            });1026        }1027        None1028    }10291030    pub(super) fn recover_closure_body(1031        &mut self,1032        mut err: Diag<'a>,1033        before: token::Token,1034        prev: token::Token,1035        token: token::Token,1036        lo: Span,1037        decl_hi: Span,1038    ) -> PResult<'a, Box<Expr>> {1039        err.span_label(lo.to(decl_hi), "while parsing the body of this closure");1040        let guar = match before.kind {1041            token::OpenBrace if token.kind != token::OpenBrace => {1042                // `{ || () }` should have been `|| { () }`1043                err.multipart_suggestion(1044                    "you might have meant to open the body of the closure, instead of enclosing \1045                     the closure in a block",1046                    vec![1047                        (before.span, String::new()),1048                        (prev.span.shrink_to_hi(), " {".to_string()),1049                    ],1050                    Applicability::MaybeIncorrect,1051                );1052                let guar = err.emit();1053                self.eat_to_tokens(&[exp!(CloseBrace)]);1054                guar1055            }1056            token::OpenParen if token.kind != token::OpenBrace => {1057                // We are within a function call or tuple, we can emit the error1058                // and recover.1059                self.eat_to_tokens(&[exp!(CloseParen), exp!(Comma)]);10601061                err.multipart_suggestion(1062                    "you might have meant to open the body of the closure",1063                    vec![1064                        (prev.span.shrink_to_hi(), " {".to_string()),1065                        (self.token.span.shrink_to_lo(), "}".to_string()),1066                    ],1067                    Applicability::MaybeIncorrect,1068                );1069                err.emit()1070            }1071            _ if token.kind != token::OpenBrace => {1072                // We don't have a heuristic to correctly identify where the block1073                // should be closed.1074                err.multipart_suggestion(1075                    "you might have meant to open the body of the closure",1076                    vec![(prev.span.shrink_to_hi(), " {".to_string())],1077                    Applicability::HasPlaceholders,1078                );1079                return Err(err);1080            }1081            _ => return Err(err),1082        };1083        Ok(self.mk_expr_err(lo.to(self.token.span), guar))1084    }10851086    /// Eats and discards tokens until one of `closes` is encountered. Respects token trees,1087    /// passes through any errors encountered. Used for error recovery.1088    pub(super) fn eat_to_tokens(&mut self, closes: &[ExpTokenPair]) {1089        if let Err(err) = self1090            .parse_seq_to_before_tokens(closes, &[], SeqSep::none(), |p| Ok(p.parse_token_tree()))1091        {1092            err.cancel();1093        }1094    }10951096    /// This function checks if there are trailing angle brackets and produces1097    /// a diagnostic to suggest removing them.1098    ///1099    /// ```ignore (diagnostic)1100    /// let _ = [1, 2, 3].into_iter().collect::<Vec<usize>>>>();1101    ///                                                    ^^ help: remove extra angle brackets1102    /// ```1103    ///1104    /// If `true` is returned, then trailing brackets were recovered, tokens were consumed1105    /// up until one of the tokens in 'end' was encountered, and an error was emitted.1106    pub(super) fn check_trailing_angle_brackets(1107        &mut self,1108        segment: &PathSegment,1109        end: &[ExpTokenPair],1110    ) -> Option<ErrorGuaranteed> {1111        if !self.may_recover() {1112            return None;1113        }11141115        // This function is intended to be invoked after parsing a path segment where there are two1116        // cases:1117        //1118        // 1. A specific token is expected after the path segment.1119        //    eg. `x.foo(`, `x.foo::<u32>(` (parenthesis - method call),1120        //        `Foo::`, or `Foo::<Bar>::` (mod sep - continued path).1121        // 2. No specific token is expected after the path segment.1122        //    eg. `x.foo` (field access)1123        //1124        // This function is called after parsing `.foo` and before parsing the token `end` (if1125        // present). This includes any angle bracket arguments, such as `.foo::<u32>` or1126        // `Foo::<Bar>`.11271128        // We only care about trailing angle brackets if we previously parsed angle bracket1129        // arguments. This helps stop us incorrectly suggesting that extra angle brackets be1130        // removed in this case:1131        //1132        // `x.foo >> (3)` (where `x.foo` is a `u32` for example)1133        //1134        // This case is particularly tricky as we won't notice it just looking at the tokens -1135        // it will appear the same (in terms of upcoming tokens) as below (since the `::<u32>` will1136        // have already been parsed):1137        //1138        // `x.foo::<u32>>>(3)`1139        let parsed_angle_bracket_args =1140            segment.args.as_ref().is_some_and(|args| args.is_angle_bracketed());11411142        debug!(1143            "check_trailing_angle_brackets: parsed_angle_bracket_args={:?}",1144            parsed_angle_bracket_args,1145        );1146        if !parsed_angle_bracket_args {1147            return None;1148        }11491150        // Keep the span at the start so we can highlight the sequence of `>` characters to be1151        // removed.1152        let lo = self.token.span;11531154        // We need to look-ahead to see if we have `>` characters without moving the cursor forward1155        // (since we might have the field access case and the characters we're eating are1156        // actual operators and not trailing characters - ie `x.foo >> 3`).1157        let mut position = 0;11581159        // We can encounter `>` or `>>` tokens in any order, so we need to keep track of how1160        // many of each (so we can correctly pluralize our error messages) and continue to1161        // advance.1162        let mut number_of_shr = 0;1163        let mut number_of_gt = 0;1164        while self.look_ahead(position, |t| {1165            trace!("check_trailing_angle_brackets: t={:?}", t);1166            if *t == token::Shr {1167                number_of_shr += 1;1168                true1169            } else if *t == token::Gt {1170                number_of_gt += 1;1171                true1172            } else {1173                false1174            }1175        }) {1176            position += 1;1177        }11781179        // If we didn't find any trailing `>` characters, then we have nothing to error about.1180        debug!(1181            "check_trailing_angle_brackets: number_of_gt={:?} number_of_shr={:?}",1182            number_of_gt, number_of_shr,1183        );1184        if number_of_gt < 1 && number_of_shr < 1 {1185            return None;1186        }11871188        // Finally, double check that we have our end token as otherwise this is the1189        // second case.1190        if self.look_ahead(position, |t| {1191            trace!("check_trailing_angle_brackets: t={:?}", t);1192            end.iter().any(|exp| exp.tok == t.kind)1193        }) {1194            // Eat from where we started until the end token so that parsing can continue1195            // as if we didn't have those extra angle brackets.1196            self.eat_to_tokens(end);1197            let span = lo.to(self.prev_token.span);11981199            let num_extra_brackets = number_of_gt + number_of_shr * 2;1200            return Some(self.dcx().emit_err(UnmatchedAngleBrackets { span, num_extra_brackets }));1201        }1202        None1203    }12041205    /// Check if a method call with an intended turbofish has been written without surrounding1206    /// angle brackets.1207    pub(super) fn check_turbofish_missing_angle_brackets(&mut self, segment: &mut PathSegment) {1208        if !self.may_recover() {1209            return;1210        }12111212        if self.token == token::PathSep && segment.args.is_none() {1213            let snapshot = self.create_snapshot_for_diagnostic();1214            self.bump();1215            let lo = self.token.span;1216            match self.parse_angle_args(None) {1217                Ok(args) => {1218                    let span = lo.to(self.prev_token.span);1219                    // Detect trailing `>` like in `x.collect::Vec<_>>()`.1220                    let mut trailing_span = self.prev_token.span.shrink_to_hi();1221                    while self.token == token::Shr || self.token == token::Gt {1222                        trailing_span = trailing_span.to(self.token.span);1223                        self.bump();1224                    }1225                    if self.token == token::OpenParen {1226                        // Recover from bad turbofish: `foo.collect::Vec<_>()`.1227                        segment.args = Some(AngleBracketedArgs { args, span }.into());12281229                        self.dcx().emit_err(GenericParamsWithoutAngleBrackets {1230                            span,1231                            sugg: GenericParamsWithoutAngleBracketsSugg {1232                                left: span.shrink_to_lo(),1233                                right: trailing_span,1234                            },1235                        });1236                    } else {1237                        // This doesn't look like an invalid turbofish, can't recover parse state.1238                        self.restore_snapshot(snapshot);1239                    }1240                }1241                Err(err) => {1242                    // We couldn't parse generic parameters, unlikely to be a turbofish. Rely on1243                    // generic parse error instead.1244                    err.cancel();1245                    self.restore_snapshot(snapshot);1246                }1247            }1248        }1249    }12501251    /// When writing a turbofish with multiple type parameters missing the leading `::`, we will1252    /// encounter a parse error when encountering the first `,`.1253    pub(super) fn check_mistyped_turbofish_with_multiple_type_params(1254        &mut self,1255        mut e: Diag<'a>,1256        expr: &mut Box<Expr>,1257    ) -> PResult<'a, ErrorGuaranteed> {1258        if let ExprKind::Binary(binop, _, _) = &expr.kind1259            && let ast::BinOpKind::Lt = binop.node1260            && self.eat(exp!(Comma))1261        {1262            let x = self.parse_seq_to_before_end(1263                exp!(Gt),1264                SeqSep::trailing_allowed(exp!(Comma)),1265                |p| match p.parse_generic_arg(None)? {1266                    Some(arg) => Ok(arg),1267                    // If we didn't eat a generic arg, then we should error.1268                    None => p.unexpected_any(),1269                },1270            );1271            match x {1272                Ok((_, _, Recovered::No)) => {1273                    if self.eat(exp!(Gt)) {1274                        // We made sense of it. Improve the error message.1275                        e.span_suggestion_verbose(1276                            binop.span.shrink_to_lo(),1277                            msg!("use `::<...>` instead of `<...>` to specify lifetime, type, or const arguments"),1278                            "::",1279                            Applicability::MaybeIncorrect,1280                        );1281                        match self.parse_expr() {1282                            Ok(_) => {1283                                // The subsequent expression is valid. Mark1284                                // `expr` as erroneous and emit `e` now, but1285                                // return `Ok` so parsing can continue.1286                                let guar = e.emit();1287                                *expr = self.mk_expr_err(expr.span.to(self.prev_token.span), guar);1288                                return Ok(guar);1289                            }1290                            Err(err) => {1291                                err.cancel();1292                            }1293                        }1294                    }1295                }1296                Ok((_, _, Recovered::Yes(_))) => {}1297                Err(err) => {1298                    err.cancel();1299                }1300            }1301        }1302        Err(e)1303    }13041305    /// Suggest add the missing `let` before the identifier in stmt1306    /// `a: Ty = 1` -> `let a: Ty = 1`1307    pub(super) fn suggest_add_missing_let_for_stmt(&mut self, err: &mut Diag<'a>) {1308        if self.token == token::Colon {1309            let prev_span = self.prev_token.span.shrink_to_lo();1310            let snapshot = self.create_snapshot_for_diagnostic();1311            self.bump();1312            match self.parse_ty() {1313                Ok(_) => {1314                    if self.token == token::Eq {1315                        let sugg = SuggAddMissingLetStmt { span: prev_span };1316                        sugg.add_to_diag(err);1317                    }1318                }1319                Err(e) => {1320                    e.cancel();1321                }1322            }1323            self.restore_snapshot(snapshot);1324        }1325    }13261327    /// Check to see if a pair of chained operators looks like an attempt at chained comparison,1328    /// e.g. `1 < x <= 3`. If so, suggest either splitting the comparison into two, or1329    /// parenthesising the leftmost comparison. The return value indicates if recovery happened.1330    fn attempt_chained_comparison_suggestion(1331        &mut self,1332        err: &mut ComparisonOperatorsCannotBeChained,1333        inner_op: &Expr,1334        outer_op: &Spanned<AssocOp>,1335    ) -> bool {1336        if let ExprKind::Binary(op, l1, r1) = &inner_op.kind {1337            if let ExprKind::Field(_, ident) = l1.kind1338                && !ident.is_numeric()1339                && !matches!(r1.kind, ExprKind::Lit(_))1340            {1341                // The parser has encountered `foo.bar<baz`, the likelihood of the turbofish1342                // suggestion being the only one to apply is high.1343                return false;1344            }1345            return match (op.node, &outer_op.node) {1346                // `x == y == z`1347                (BinOpKind::Eq, AssocOp::Binary(BinOpKind::Eq)) |1348                // `x < y < z` and friends.1349                (BinOpKind::Lt, AssocOp::Binary(BinOpKind::Lt | BinOpKind::Le)) |1350                (BinOpKind::Le, AssocOp::Binary(BinOpKind::Lt | BinOpKind::Le)) |1351                // `x > y > z` and friends.1352                (BinOpKind::Gt, AssocOp::Binary(BinOpKind::Gt | BinOpKind::Ge)) |1353                (BinOpKind::Ge, AssocOp::Binary(BinOpKind::Gt | BinOpKind::Ge)) => {1354                    let expr_to_str = |e: &Expr| {1355                        self.span_to_snippet(e.span).unwrap_or_else(|_| pprust::expr_to_string(e))1356                    };1357                    err.chaining_sugg =1358                        Some(ComparisonOperatorsCannotBeChainedSugg::SplitComparison {1359                            span: inner_op.span.shrink_to_hi(),1360                            middle_term: expr_to_str(r1),1361                        });1362                    false // Keep the current parse behavior, where the AST is `(x < y) < z`.1363                }1364                // `x == y < z`1365                (1366                    BinOpKind::Eq,1367                    AssocOp::Binary(BinOpKind::Lt | BinOpKind::Le | BinOpKind::Gt | BinOpKind::Ge),1368                ) => {1369                    // Consume `z`/outer-op-rhs.1370                    let snapshot = self.create_snapshot_for_diagnostic();1371                    match self.parse_expr() {1372                        Ok(r2) => {1373                            // We are sure that outer-op-rhs could be consumed, the suggestion is1374                            // likely correct.1375                            err.chaining_sugg =1376                                Some(ComparisonOperatorsCannotBeChainedSugg::Parenthesize {1377                                    left: r1.span.shrink_to_lo(),1378                                    right: r2.span.shrink_to_hi(),1379                                });1380                            true1381                        }1382                        Err(expr_err) => {1383                            expr_err.cancel();1384                            self.restore_snapshot(snapshot);1385                            true1386                        }1387                    }1388                }1389                // `x > y == z`1390                (1391                    BinOpKind::Lt | BinOpKind::Le | BinOpKind::Gt | BinOpKind::Ge,1392                    AssocOp::Binary(BinOpKind::Eq),1393                ) => {1394                    let snapshot = self.create_snapshot_for_diagnostic();1395                    // At this point it is always valid to enclose the lhs in parentheses, no1396                    // further checks are necessary.1397                    match self.parse_expr() {1398                        Ok(_) => {1399                            err.chaining_sugg =1400                                Some(ComparisonOperatorsCannotBeChainedSugg::Parenthesize {1401                                    left: l1.span.shrink_to_lo(),1402                                    right: r1.span.shrink_to_hi(),1403                                });1404                            true1405                        }1406                        Err(expr_err) => {1407                            expr_err.cancel();1408                            self.restore_snapshot(snapshot);1409                            false1410                        }1411                    }1412                }1413                _ => false,1414            };1415        }1416        false1417    }14181419    /// Produces an error if comparison operators are chained (RFC #558).1420    /// We only need to check the LHS, not the RHS, because all comparison ops have same1421    /// precedence (see `fn precedence`) and are left-associative (see `fn fixity`).1422    ///1423    /// This can also be hit if someone incorrectly writes `foo<bar>()` when they should have used1424    /// the turbofish (`foo::<bar>()`) syntax. We attempt some heuristic recovery if that is the1425    /// case.1426    ///1427    /// Keep in mind that given that `outer_op.is_comparison()` holds and comparison ops are left1428    /// associative we can infer that we have:1429    ///1430    /// ```text1431    ///           outer_op1432    ///           /   \1433    ///     inner_op   r21434    ///        /  \1435    ///      l1    r11436    /// ```1437    pub(super) fn check_no_chained_comparison(1438        &mut self,1439        inner_op: &Expr,1440        outer_op: &Spanned<AssocOp>,1441    ) -> PResult<'a, Option<Box<Expr>>> {1442        debug_assert!(1443            outer_op.node.is_comparison(),1444            "check_no_chained_comparison: {:?} is not comparison",1445            outer_op.node,1446        );14471448        let mk_err_expr =1449            |this: &Self, span, guar| Ok(Some(this.mk_expr(span, ExprKind::Err(guar))));14501451        match &inner_op.kind {1452            ExprKind::Binary(op, l1, r1) if op.node.is_comparison() => {1453                let mut err = ComparisonOperatorsCannotBeChained {1454                    span: vec![op.span, self.prev_token.span],1455                    suggest_turbofish: None,1456                    help_turbofish: false,1457                    chaining_sugg: None,1458                };14591460                // Include `<` to provide this recommendation even in a case like1461                // `Foo<Bar<Baz<Qux, ()>>>`1462                if op.node == BinOpKind::Lt && outer_op.node == AssocOp::Binary(BinOpKind::Lt)1463                    || outer_op.node == AssocOp::Binary(BinOpKind::Gt)1464                {1465                    if outer_op.node == AssocOp::Binary(BinOpKind::Lt) {1466                        let snapshot = self.create_snapshot_for_diagnostic();1467                        self.bump();1468                        // So far we have parsed `foo<bar<`, consume the rest of the type args.1469                        let modifiers = [(token::Lt, 1), (token::Gt, -1), (token::Shr, -2)];1470                        self.consume_tts(1, &modifiers);14711472                        if !matches!(self.token.kind, token::OpenParen | token::PathSep) {1473                            // We don't have `foo< bar >(` or `foo< bar >::`, so we rewind the1474                            // parser and bail out.1475                            self.restore_snapshot(snapshot);1476                        }1477                    }1478                    return if self.token == token::PathSep {1479                        // We have some certainty that this was a bad turbofish at this point.1480                        // `foo< bar >::`1481                        if let ExprKind::Binary(o, ..) = inner_op.kind1482                            && o.node == BinOpKind::Lt1483                        {1484                            err.suggest_turbofish = Some(op.span.shrink_to_lo());1485                        } else {1486                            err.help_turbofish = true;1487                        }14881489                        let snapshot = self.create_snapshot_for_diagnostic();1490                        self.bump(); // `::`14911492                        // Consume the rest of the likely `foo<bar>::new()` or return at `foo<bar>`.1493                        match self.parse_expr() {1494                            Ok(_) => {1495                                // 99% certain that the suggestion is correct, continue parsing.1496                                let guar = self.dcx().emit_err(err);1497                                // FIXME: actually check that the two expressions in the binop are1498                                // paths and resynthesize new fn call expression instead of using1499                                // `ExprKind::Err` placeholder.1500                                mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar)1501                            }1502                            Err(expr_err) => {1503                                expr_err.cancel();1504                                // Not entirely sure now, but we bubble the error up with the1505                                // suggestion.1506                                self.restore_snapshot(snapshot);1507                                Err(self.dcx().create_err(err))1508                            }1509                        }1510                    } else if self.token == token::OpenParen {1511                        // We have high certainty that this was a bad turbofish at this point.1512                        // `foo< bar >(`1513                        if let ExprKind::Binary(o, ..) = inner_op.kind1514                            && o.node == BinOpKind::Lt1515                        {1516                            err.suggest_turbofish = Some(op.span.shrink_to_lo());1517                        } else {1518                            err.help_turbofish = true;1519                        }1520                        // Consume the fn call arguments.1521                        match self.consume_fn_args() {1522                            Err(()) => Err(self.dcx().create_err(err)),1523                            Ok(()) => {1524                                let guar = self.dcx().emit_err(err);1525                                // FIXME: actually check that the two expressions in the binop are1526                                // paths and resynthesize new fn call expression instead of using1527                                // `ExprKind::Err` placeholder.1528                                mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar)1529                            }1530                        }1531                    } else {1532                        if !matches!(l1.kind, ExprKind::Lit(_))1533                            && !matches!(r1.kind, ExprKind::Lit(_))1534                        {1535                            // All we know is that this is `foo < bar >` and *nothing* else. Try to1536                            // be helpful, but don't attempt to recover.1537                            err.help_turbofish = true;1538                        }15391540                        // If it looks like a genuine attempt to chain operators (as opposed to a1541                        // misformatted turbofish, for instance), suggest a correct form.1542                        let recovered = self1543                            .attempt_chained_comparison_suggestion(&mut err, inner_op, outer_op);1544                        if recovered {1545                            let guar = self.dcx().emit_err(err);1546                            mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar)1547                        } else {1548                            // These cases cause too many knock-down errors, bail out (#61329).1549                            Err(self.dcx().create_err(err))1550                        }1551                    };1552                }1553                let recovered =1554                    self.attempt_chained_comparison_suggestion(&mut err, inner_op, outer_op);1555                let guar = self.dcx().emit_err(err);1556                if recovered {1557                    return mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar);1558                }1559            }1560            _ => {}1561        }1562        Ok(None)1563    }15641565    fn consume_fn_args(&mut self) -> Result<(), ()> {1566        let snapshot = self.create_snapshot_for_diagnostic();1567        self.bump(); // `(`15681569        // Consume the fn call arguments.1570        let modifiers = [(token::OpenParen, 1), (token::CloseParen, -1)];1571        self.consume_tts(1, &modifiers);15721573        if self.token == token::Eof {1574            // Not entirely sure that what we consumed were fn arguments, rollback.1575            self.restore_snapshot(snapshot);1576            Err(())1577        } else {1578            // 99% certain that the suggestion is correct, continue parsing.1579            Ok(())1580        }1581    }15821583    pub(super) fn maybe_report_ambiguous_plus(&mut self, impl_dyn_multi: bool, ty: &Ty) {1584        if impl_dyn_multi {1585            self.dcx().emit_err(AmbiguousPlus {1586                span: ty.span,1587                suggestion: AddParen { lo: ty.span.shrink_to_lo(), hi: ty.span.shrink_to_hi() },1588            });1589        }1590    }15911592    /// Swift lets users write `Ty?` to mean `Option<Ty>`. Parse the construct and recover from it.1593    pub(super) fn maybe_recover_from_question_mark(&mut self, ty: Box<Ty>) -> Box<Ty> {1594        if self.token == token::Question {1595            self.bump();1596            let guar = self.dcx().emit_err(QuestionMarkInType {1597                span: self.prev_token.span,1598                sugg: QuestionMarkInTypeSugg {1599                    left: ty.span.shrink_to_lo(),1600                    right: self.prev_token.span,1601                },1602            });1603            self.mk_ty(ty.span.to(self.prev_token.span), TyKind::Err(guar))1604        } else {1605            ty1606        }1607    }16081609    /// Rust has no ternary operator (`cond ? then : else`). Parse it and try1610    /// to recover from it if `then` and `else` are valid expressions. Returns1611    /// an err if this appears to be a ternary expression.1612    /// If we have the span of the condition, we can provide a better error span1613    /// and code suggestion.1614    pub(super) fn maybe_recover_from_ternary_operator(1615        &mut self,1616        cond: Option<Span>,1617    ) -> PResult<'a, ()> {1618        if self.prev_token != token::Question {1619            return PResult::Ok(());1620        }16211622        let question = self.prev_token.span;1623        let lo = cond.unwrap_or(question).lo();1624        let snapshot = self.create_snapshot_for_diagnostic();16251626        if match self.parse_expr() {1627            Ok(_) => true,1628            Err(err) => {1629                err.cancel();1630                // The colon can sometimes be mistaken for type1631                // ascription. Catch when this happens and continue.1632                self.token == token::Colon1633            }1634        } {1635            if self.eat_noexpect(&token::Colon) {1636                let colon = self.prev_token.span;1637                match self.parse_expr() {1638                    Ok(expr) => {1639                        let sugg = cond.map(|cond| TernaryOperatorSuggestion {1640                            before_cond: cond.shrink_to_lo(),1641                            question,1642                            colon,1643                            end: expr.span.shrink_to_hi(),1644                        });1645                        return Err(self.dcx().create_err(TernaryOperator {1646                            span: self.prev_token.span.with_lo(lo),1647                            sugg,1648                            no_sugg: sugg.is_none(),1649                        }));1650                    }1651                    Err(err) => {1652                        err.cancel();1653                    }1654                };1655            }1656        }1657        self.restore_snapshot(snapshot);1658        Ok(())1659    }16601661    pub(super) fn maybe_recover_from_bad_type_plus(&mut self, ty: &Ty) -> PResult<'a, ()> {1662        // Do not add `+` to expected tokens.1663        if !self.token.is_like_plus() {1664            return Ok(());1665        }16661667        self.bump(); // `+`1668        let _bounds = self.parse_generic_bounds()?;1669        let sub = match &ty.kind {1670            TyKind::Ref(_lifetime, mut_ty) => {1671                let lo = mut_ty.ty.span.shrink_to_lo();1672                let hi = self.prev_token.span.shrink_to_hi();1673                BadTypePlusSub::AddParen { suggestion: AddParen { lo, hi } }1674            }1675            TyKind::Ptr(..) | TyKind::FnPtr(..) => {1676                BadTypePlusSub::ForgotParen { span: ty.span.to(self.prev_token.span) }1677            }1678            _ => BadTypePlusSub::ExpectPath { span: ty.span },1679        };16801681        self.dcx().emit_err(BadTypePlus { span: ty.span, sub });16821683        Ok(())1684    }16851686    pub(super) fn recover_from_prefix_increment(1687        &mut self,1688        operand_expr: Box<Expr>,1689        op_span: Span,1690        start_stmt: bool,1691    ) -> PResult<'a, Box<Expr>> {1692        let standalone = if start_stmt { IsStandalone::Standalone } else { IsStandalone::Subexpr };1693        let kind = IncDecRecovery { standalone, op: IncOrDec::Inc, fixity: UnaryFixity::Pre };1694        self.recover_from_inc_dec(operand_expr, kind, op_span)1695    }16961697    pub(super) fn recover_from_postfix_increment(1698        &mut self,1699        operand_expr: Box<Expr>,1700        op_span: Span,1701        start_stmt: bool,1702    ) -> PResult<'a, Box<Expr>> {1703        let kind = IncDecRecovery {1704            standalone: if start_stmt { IsStandalone::Standalone } else { IsStandalone::Subexpr },1705            op: IncOrDec::Inc,1706            fixity: UnaryFixity::Post,1707        };1708        self.recover_from_inc_dec(operand_expr, kind, op_span)1709    }17101711    pub(super) fn recover_from_postfix_decrement(1712        &mut self,1713        operand_expr: Box<Expr>,1714        op_span: Span,1715        start_stmt: bool,1716    ) -> PResult<'a, Box<Expr>> {1717        let kind = IncDecRecovery {1718            standalone: if start_stmt { IsStandalone::Standalone } else { IsStandalone::Subexpr },1719            op: IncOrDec::Dec,1720            fixity: UnaryFixity::Post,1721        };1722        self.recover_from_inc_dec(operand_expr, kind, op_span)1723    }17241725    fn recover_from_inc_dec(1726        &mut self,1727        base: Box<Expr>,1728        kind: IncDecRecovery,1729        op_span: Span,1730    ) -> PResult<'a, Box<Expr>> {1731        let mut err = self.dcx().struct_span_err(1732            op_span,1733            format!("Rust has no {} {} operator", kind.fixity, kind.op.name()),1734        );1735        err.span_label(op_span, format!("not a valid {} operator", kind.fixity));17361737        let help_base_case = |mut err: Diag<'_, _>, base| {1738            err.help(format!("use `{}= 1` instead", kind.op.chr()));1739            err.emit();1740            Ok(base)1741        };17421743        // (pre, post)1744        let spans = match kind.fixity {1745            UnaryFixity::Pre => (op_span, base.span.shrink_to_hi()),1746            UnaryFixity::Post => (base.span.shrink_to_lo(), op_span),1747        };17481749        match kind.standalone {1750            IsStandalone::Standalone => {1751                self.inc_dec_standalone_suggest(kind, spans).emit_verbose(&mut err)1752            }1753            IsStandalone::Subexpr => {1754                let Ok(base_src) = self.span_to_snippet(base.span) else {1755                    return help_base_case(err, base);1756                };1757                match kind.fixity {1758                    UnaryFixity::Pre => {1759                        self.prefix_inc_dec_suggest(base_src, kind, spans).emit(&mut err)1760                    }1761                    UnaryFixity::Post => {1762                        // won't suggest since we can not handle the precedences1763                        // for example: `a + b++` has been parsed (a + b)++ and we can not suggest here1764                        if !matches!(base.kind, ExprKind::Binary(_, _, _)) {1765                            self.postfix_inc_dec_suggest(base_src, kind, spans).emit(&mut err)1766                        }1767                    }1768                }1769            }1770        }1771        Err(err)1772    }17731774    fn prefix_inc_dec_suggest(1775        &mut self,1776        base_src: String,1777        kind: IncDecRecovery,1778        (pre_span, post_span): (Span, Span),1779    ) -> MultiSugg {1780        MultiSugg {1781            msg: format!("use `{}= 1` instead", kind.op.chr()),1782            patches: vec![1783                (pre_span, "{ ".to_string()),1784                (post_span, format!(" {}= 1; {} }}", kind.op.chr(), base_src)),1785            ],1786            applicability: Applicability::MachineApplicable,1787        }1788    }17891790    fn postfix_inc_dec_suggest(1791        &mut self,1792        base_src: String,1793        kind: IncDecRecovery,1794        (pre_span, post_span): (Span, Span),1795    ) -> MultiSugg {1796        let tmp_var = if base_src.trim() == "tmp" { "tmp_" } else { "tmp" };1797        MultiSugg {1798            msg: format!("use `{}= 1` instead", kind.op.chr()),1799            patches: vec![1800                (pre_span, format!("{{ let {tmp_var} = ")),1801                (post_span, format!("; {} {}= 1; {} }}", base_src, kind.op.chr(), tmp_var)),1802            ],1803            applicability: Applicability::HasPlaceholders,1804        }1805    }18061807    fn inc_dec_standalone_suggest(1808        &mut self,1809        kind: IncDecRecovery,1810        (pre_span, post_span): (Span, Span),1811    ) -> MultiSugg {1812        let mut patches = Vec::new();18131814        if !pre_span.is_empty() {1815            patches.push((pre_span, String::new()));1816        }18171818        patches.push((post_span, format!(" {}= 1", kind.op.chr())));1819        MultiSugg {1820            msg: format!("use `{}= 1` instead", kind.op.chr()),1821            patches,1822            applicability: Applicability::MachineApplicable,1823        }1824    }18251826    /// Tries to recover from associated item paths like `[T]::AssocItem` / `(T, U)::AssocItem`.1827    /// Attempts to convert the base expression/pattern/type into a type, parses the `::AssocItem`1828    /// tail, and combines them into a `<Ty>::AssocItem` expression/pattern/type.1829    pub(super) fn maybe_recover_from_bad_qpath<T: RecoverQPath>(1830        &mut self,1831        base: T,1832    ) -> PResult<'a, T> {1833        // Do not add `::` to expected tokens.1834        if self.may_recover() && self.token == token::PathSep {1835            return self.recover_from_bad_qpath(base);1836        }1837        Ok(base)1838    }18391840    #[cold]1841    fn recover_from_bad_qpath<T: RecoverQPath>(&mut self, base: T) -> PResult<'a, T> {1842        if let Some(ty) = base.to_ty() {1843            return self.maybe_recover_from_bad_qpath_stage_2(ty.span, ty);1844        }1845        Ok(base)1846    }18471848    /// Given an already parsed `Ty`, parses the `::AssocItem` tail and1849    /// combines them into a `<Ty>::AssocItem` expression/pattern/type.1850    pub(super) fn maybe_recover_from_bad_qpath_stage_2<T: RecoverQPath>(1851        &mut self,1852        ty_span: Span,1853        ty: Box<Ty>,1854    ) -> PResult<'a, T> {1855        self.expect(exp!(PathSep))?;18561857        let mut path = ast::Path { segments: ThinVec::new(), span: DUMMY_SP, tokens: None };1858        self.parse_path_segments(&mut path.segments, T::PATH_STYLE, None)?;1859        path.span = ty_span.to(self.prev_token.span);18601861        self.dcx().emit_err(BadQPathStage2 {1862            span: ty_span,1863            wrap: WrapType { lo: ty_span.shrink_to_lo(), hi: ty_span.shrink_to_hi() },1864        });18651866        let path_span = ty_span.shrink_to_hi(); // Use an empty path since `position == 0`.1867        Ok(T::recovered(Some(Box::new(QSelf { ty, path_span, position: 0 })), path))1868    }18691870    /// This function gets called in places where a semicolon is NOT expected and if there's a1871    /// semicolon it emits the appropriate error and returns true.1872    pub fn maybe_consume_incorrect_semicolon(&mut self, previous_item: Option<&Item>) -> bool {1873        if self.token != TokenKind::Semi {1874            return false;1875        }18761877        // Check previous item to add it to the diagnostic, for example to say1878        // `enum declarations are not followed by a semicolon`1879        let err = match previous_item {1880            Some(previous_item) => {1881                let name = match previous_item.kind {1882                    // Say "braced struct" because tuple-structs and1883                    // braceless-empty-struct declarations do take a semicolon.1884                    ItemKind::Struct(..) => "braced struct",1885                    _ => previous_item.kind.descr(),1886                };1887                IncorrectSemicolon { span: self.token.span, name, show_help: true }1888            }1889            None => IncorrectSemicolon { span: self.token.span, name: "", show_help: false },1890        };1891        self.dcx().emit_err(err);18921893        self.bump();1894        true1895    }18961897    /// Creates a `Diag` for an unexpected token `t`1898    pub(super) fn unexpected_err(&mut self, t: &TokenKind) -> Diag<'a> {1899        let token_str = pprust::token_kind_to_string(t);1900        let this_token_str = super::token_descr(&self.token);1901        let (prev_sp, sp) = match (&self.token.kind, self.subparser_name) {1902            // Point at the end of the macro call when reaching end of macro arguments.1903            (token::Eof, Some(_)) => {1904                let sp = self.prev_token.span.shrink_to_hi();1905                (sp, sp)1906            }1907            // We don't want to point at the following span after DUMMY_SP.1908            // This happens when the parser finds an empty TokenStream.1909            _ if self.prev_token.span == DUMMY_SP => (self.token.span, self.token.span),1910            // EOF, don't want to point at the following char, but rather the last token.1911            (token::Eof, None) => (self.prev_token.span, self.token.span),1912            _ => (self.prev_token.span.shrink_to_hi(), self.token.span),1913        };1914        let msg = format!(1915            "expected `{}`, found {}",1916            token_str,1917            match (&self.token.kind, self.subparser_name) {1918                (token::Eof, Some(origin)) => format!("end of {origin}"),1919                _ => this_token_str,1920            },1921        );1922        let mut err = self.dcx().struct_span_err(sp, msg);1923        let label_exp = format!("expected `{token_str}`");1924        let sm = self.psess.source_map();1925        if !sm.is_multiline(prev_sp.until(sp)) {1926            // When the spans are in the same line, it means that the only content1927            // between them is whitespace, point only at the found token.1928            err.span_label(sp, label_exp);1929        } else {1930            err.span_label(prev_sp, label_exp);1931            err.span_label(sp, "unexpected token");1932        }1933        err1934    }19351936    pub(super) fn expect_semi(&mut self) -> PResult<'a, ()> {1937        if self.eat(exp!(Semi)) || self.recover_colon_as_semi() {1938            return Ok(());1939        }1940        self.expect(exp!(Semi)).map(drop) // Error unconditionally1941    }19421943    pub(super) fn recover_colon_as_semi(&mut self) -> bool {1944        let line_idx = |span: Span| {1945            self.psess1946                .source_map()1947                .span_to_lines(span)1948                .ok()1949                .and_then(|lines| Some(lines.lines.get(0)?.line_index))1950        };19511952        if self.may_recover()1953            && self.token == token::Colon1954            && self.look_ahead(1, |next| line_idx(self.token.span) < line_idx(next.span))1955        {1956            self.dcx().emit_err(ColonAsSemi { span: self.token.span });1957            self.bump();1958            return true;1959        }19601961        false1962    }19631964    /// Consumes alternative await syntaxes like `await!(<expr>)`, `await <expr>`,1965    /// `await? <expr>`, `await(<expr>)`, and `await { <expr> }`.1966    pub(super) fn recover_incorrect_await_syntax(1967        &mut self,1968        await_sp: Span,1969    ) -> PResult<'a, Box<Expr>> {1970        let (hi, expr, is_question) = if self.token == token::Bang {1971            // Handle `await!(<expr>)`.1972            self.recover_await_macro()?1973        } else {1974            self.recover_await_prefix(await_sp)?1975        };1976        let (sp, guar) = self.error_on_incorrect_await(await_sp, hi, &expr, is_question);1977        let expr = self.mk_expr_err(await_sp.to(sp), guar);1978        self.maybe_recover_from_bad_qpath(expr)1979    }19801981    fn recover_await_macro(&mut self) -> PResult<'a, (Span, Box<Expr>, bool)> {1982        self.expect(exp!(Bang))?;1983        self.expect(exp!(OpenParen))?;1984        let expr = self.parse_expr()?;1985        self.expect(exp!(CloseParen))?;1986        Ok((self.prev_token.span, expr, false))1987    }19881989    fn recover_await_prefix(&mut self, await_sp: Span) -> PResult<'a, (Span, Box<Expr>, bool)> {1990        let is_question = self.eat(exp!(Question)); // Handle `await? <expr>`.1991        let expr = if self.token == token::OpenBrace {1992            // Handle `await { <expr> }`.1993            // This needs to be handled separately from the next arm to avoid1994            // interpreting `await { <expr> }?` as `<expr>?.await`.1995            self.parse_expr_block(None, self.token.span, BlockCheckMode::Default)1996        } else {1997            self.parse_expr()1998        }1999        .map_err(|mut err| {2000            err.span_label(await_sp, format!("while parsing this incorrect await expression"));

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.