rustc_parse/parser/
diagnostics.rs

1use std::mem::take;
2use std::ops::{Deref, DerefMut};
3
4use ast::token::IdentIsRaw;
5use rustc_ast as ast;
6use rustc_ast::token::{self, Lit, LitKind, Token, TokenKind};
7use rustc_ast::util::parser::AssocOp;
8use rustc_ast::{
9    AngleBracketedArg, AngleBracketedArgs, AnonConst, AttrVec, BinOpKind, BindingMode, Block,
10    BlockCheckMode, Expr, ExprKind, GenericArg, Generics, Item, ItemKind, Param, Pat, PatKind,
11    Path, PathSegment, QSelf, Recovered, Ty, TyKind,
12};
13use rustc_ast_pretty::pprust;
14use rustc_data_structures::fx::FxHashSet;
15use rustc_errors::{
16    Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, PResult, Subdiagnostic, Suggestions,
17    pluralize,
18};
19use rustc_session::errors::ExprParenthesesNeeded;
20use rustc_span::edit_distance::find_best_match_for_name;
21use rustc_span::source_map::Spanned;
22use rustc_span::symbol::used_keywords;
23use rustc_span::{BytePos, DUMMY_SP, Ident, Span, SpanSnippetError, Symbol, kw, sym};
24use thin_vec::{ThinVec, thin_vec};
25use tracing::{debug, trace};
26
27use super::pat::Expected;
28use super::{
29    BlockMode, CommaRecoveryMode, ExpTokenPair, Parser, PathStyle, Restrictions, SemiColonMode,
30    SeqSep, TokenType,
31};
32use crate::errors::{
33    AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AsyncUseBlockIn2015, AttributeOnParamType,
34    AwaitSuggestion, BadQPathStage2, BadTypePlus, BadTypePlusSub, ColonAsSemi,
35    ComparisonOperatorsCannotBeChained, ComparisonOperatorsCannotBeChainedSugg,
36    ConstGenericWithoutBraces, ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything,
37    DocCommentOnParamType, DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
38    GenericParamsWithoutAngleBrackets, GenericParamsWithoutAngleBracketsSugg,
39    HelpIdentifierStartsWithNumber, HelpUseLatestEdition, InInTypo, IncorrectAwait,
40    IncorrectSemicolon, IncorrectUseOfAwait, IncorrectUseOfUse, PatternMethodParamWithoutBody,
41    QuestionMarkInType, QuestionMarkInTypeSugg, SelfParamNotFirst, StructLiteralBodyWithoutPath,
42    StructLiteralBodyWithoutPathSugg, SuggAddMissingLetStmt, SuggEscapeIdentifier, SuggRemoveComma,
43    TernaryOperator, TernaryOperatorSuggestion, UnexpectedConstInGenericParam,
44    UnexpectedConstParamDeclaration, UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets,
45    UseEqInstead, WrapType,
46};
47use crate::parser::attr::InnerAttrPolicy;
48use crate::{exp, fluent_generated as fluent};
49
50/// Creates a placeholder argument.
51pub(super) fn dummy_arg(ident: Ident, guar: ErrorGuaranteed) -> Param {
52    let pat = Box::new(Pat {
53        id: ast::DUMMY_NODE_ID,
54        kind: PatKind::Ident(BindingMode::NONE, ident, None),
55        span: ident.span,
56        tokens: None,
57    });
58    let ty = Ty { kind: TyKind::Err(guar), span: ident.span, id: ast::DUMMY_NODE_ID, tokens: None };
59    Param {
60        attrs: AttrVec::default(),
61        id: ast::DUMMY_NODE_ID,
62        pat,
63        span: ident.span,
64        ty: Box::new(ty),
65        is_placeholder: false,
66    }
67}
68
69pub(super) trait RecoverQPath: Sized + 'static {
70    const PATH_STYLE: PathStyle = PathStyle::Expr;
71    fn to_ty(&self) -> Option<Box<Ty>>;
72    fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self;
73}
74
75impl RecoverQPath for Ty {
76    const PATH_STYLE: PathStyle = PathStyle::Type;
77    fn to_ty(&self) -> Option<Box<Ty>> {
78        Some(Box::new(self.clone()))
79    }
80    fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {
81        Self {
82            span: path.span,
83            kind: TyKind::Path(qself, path),
84            id: ast::DUMMY_NODE_ID,
85            tokens: None,
86        }
87    }
88}
89
90impl RecoverQPath for Pat {
91    const PATH_STYLE: PathStyle = PathStyle::Pat;
92    fn to_ty(&self) -> Option<Box<Ty>> {
93        self.to_ty()
94    }
95    fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {
96        Self {
97            span: path.span,
98            kind: PatKind::Path(qself, path),
99            id: ast::DUMMY_NODE_ID,
100            tokens: None,
101        }
102    }
103}
104
105impl RecoverQPath for Expr {
106    fn to_ty(&self) -> Option<Box<Ty>> {
107        self.to_ty()
108    }
109    fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {
110        Self {
111            span: path.span,
112            kind: ExprKind::Path(qself, path),
113            attrs: AttrVec::new(),
114            id: ast::DUMMY_NODE_ID,
115            tokens: None,
116        }
117    }
118}
119
120/// Control whether the closing delimiter should be consumed when calling `Parser::consume_block`.
121pub(crate) enum ConsumeClosingDelim {
122    Yes,
123    No,
124}
125
126#[derive(Clone, Copy)]
127pub enum AttemptLocalParseRecovery {
128    Yes,
129    No,
130}
131
132impl AttemptLocalParseRecovery {
133    pub(super) fn yes(&self) -> bool {
134        match self {
135            AttemptLocalParseRecovery::Yes => true,
136            AttemptLocalParseRecovery::No => false,
137        }
138    }
139
140    pub(super) fn no(&self) -> bool {
141        match self {
142            AttemptLocalParseRecovery::Yes => false,
143            AttemptLocalParseRecovery::No => true,
144        }
145    }
146}
147
148/// Information for emitting suggestions and recovering from
149/// C-style `i++`, `--i`, etc.
150#[derive(Debug, Copy, Clone)]
151struct IncDecRecovery {
152    /// Is this increment/decrement its own statement?
153    standalone: IsStandalone,
154    /// Is this an increment or decrement?
155    op: IncOrDec,
156    /// Is this pre- or postfix?
157    fixity: UnaryFixity,
158}
159
160/// Is an increment or decrement expression its own statement?
161#[derive(Debug, Copy, Clone)]
162enum IsStandalone {
163    /// It's standalone, i.e., its own statement.
164    Standalone,
165    /// It's a subexpression, i.e., *not* standalone.
166    Subexpr,
167}
168
169#[derive(Debug, Copy, Clone, PartialEq, Eq)]
170enum IncOrDec {
171    Inc,
172    Dec,
173}
174
175#[derive(Debug, Copy, Clone, PartialEq, Eq)]
176enum UnaryFixity {
177    Pre,
178    Post,
179}
180
181impl IncOrDec {
182    fn chr(&self) -> char {
183        match self {
184            Self::Inc => '+',
185            Self::Dec => '-',
186        }
187    }
188
189    fn name(&self) -> &'static str {
190        match self {
191            Self::Inc => "increment",
192            Self::Dec => "decrement",
193        }
194    }
195}
196
197impl std::fmt::Display for UnaryFixity {
198    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
199        match self {
200            Self::Pre => write!(f, "prefix"),
201            Self::Post => write!(f, "postfix"),
202        }
203    }
204}
205
206#[derive(Debug, rustc_macros::Subdiagnostic)]
207#[suggestion(
208    parse_misspelled_kw,
209    applicability = "machine-applicable",
210    code = "{similar_kw}",
211    style = "verbose"
212)]
213struct MisspelledKw {
214    similar_kw: String,
215    #[primary_span]
216    span: Span,
217    is_incorrect_case: bool,
218}
219
220/// Checks if the given `lookup` identifier is similar to any keyword symbol in `candidates`.
221fn find_similar_kw(lookup: Ident, candidates: &[Symbol]) -> Option<MisspelledKw> {
222    let lowercase = lookup.name.as_str().to_lowercase();
223    let lowercase_sym = Symbol::intern(&lowercase);
224    if candidates.contains(&lowercase_sym) {
225        Some(MisspelledKw { similar_kw: lowercase, span: lookup.span, is_incorrect_case: true })
226    } else if let Some(similar_sym) = find_best_match_for_name(candidates, lookup.name, None) {
227        Some(MisspelledKw {
228            similar_kw: similar_sym.to_string(),
229            span: lookup.span,
230            is_incorrect_case: false,
231        })
232    } else {
233        None
234    }
235}
236
237struct MultiSugg {
238    msg: String,
239    patches: Vec<(Span, String)>,
240    applicability: Applicability,
241}
242
243impl MultiSugg {
244    fn emit(self, err: &mut Diag<'_>) {
245        err.multipart_suggestion(self.msg, self.patches, self.applicability);
246    }
247
248    fn emit_verbose(self, err: &mut Diag<'_>) {
249        err.multipart_suggestion_verbose(self.msg, self.patches, self.applicability);
250    }
251}
252
253/// SnapshotParser is used to create a snapshot of the parser
254/// without causing duplicate errors being emitted when the `Parser`
255/// is dropped.
256pub struct SnapshotParser<'a> {
257    parser: Parser<'a>,
258}
259
260impl<'a> Deref for SnapshotParser<'a> {
261    type Target = Parser<'a>;
262
263    fn deref(&self) -> &Self::Target {
264        &self.parser
265    }
266}
267
268impl<'a> DerefMut for SnapshotParser<'a> {
269    fn deref_mut(&mut self) -> &mut Self::Target {
270        &mut self.parser
271    }
272}
273
274impl<'a> Parser<'a> {
275    pub fn dcx(&self) -> DiagCtxtHandle<'a> {
276        self.psess.dcx()
277    }
278
279    /// Replace `self` with `snapshot.parser`.
280    pub(super) fn restore_snapshot(&mut self, snapshot: SnapshotParser<'a>) {
281        *self = snapshot.parser;
282    }
283
284    /// Create a snapshot of the `Parser`.
285    pub fn create_snapshot_for_diagnostic(&self) -> SnapshotParser<'a> {
286        let snapshot = self.clone();
287        SnapshotParser { parser: snapshot }
288    }
289
290    pub(super) fn span_to_snippet(&self, span: Span) -> Result<String, SpanSnippetError> {
291        self.psess.source_map().span_to_snippet(span)
292    }
293
294    /// Emits an error with suggestions if an identifier was expected but not found.
295    ///
296    /// Returns a possibly recovered identifier.
297    pub(super) fn expected_ident_found(
298        &mut self,
299        recover: bool,
300    ) -> PResult<'a, (Ident, IdentIsRaw)> {
301        let valid_follow = &[
302            TokenKind::Eq,
303            TokenKind::Colon,
304            TokenKind::Comma,
305            TokenKind::Semi,
306            TokenKind::PathSep,
307            TokenKind::OpenBrace,
308            TokenKind::OpenParen,
309            TokenKind::CloseBrace,
310            TokenKind::CloseParen,
311        ];
312        if let TokenKind::DocComment(..) = self.prev_token.kind
313            && valid_follow.contains(&self.token.kind)
314        {
315            let err = self.dcx().create_err(DocCommentDoesNotDocumentAnything {
316                span: self.prev_token.span,
317                missing_comma: None,
318            });
319            return Err(err);
320        }
321
322        let mut recovered_ident = None;
323        // we take this here so that the correct original token is retained in
324        // the diagnostic, regardless of eager recovery.
325        let bad_token = self.token;
326
327        // suggest prepending a keyword in identifier position with `r#`
328        let suggest_raw = if let Some((ident, IdentIsRaw::No)) = self.token.ident()
329            && ident.is_raw_guess()
330            && self.look_ahead(1, |t| valid_follow.contains(&t.kind))
331        {
332            recovered_ident = Some((ident, IdentIsRaw::Yes));
333
334            // `Symbol::to_string()` is different from `Symbol::into_diag_arg()`,
335            // which uses `Symbol::to_ident_string()` and "helpfully" adds an implicit `r#`
336            let ident_name = ident.name.to_string();
337
338            Some(SuggEscapeIdentifier { span: ident.span.shrink_to_lo(), ident_name })
339        } else {
340            None
341        };
342
343        let suggest_remove_comma =
344            if self.token == token::Comma && self.look_ahead(1, |t| t.is_ident()) {
345                if recover {
346                    self.bump();
347                    recovered_ident = self.ident_or_err(false).ok();
348                };
349
350                Some(SuggRemoveComma { span: bad_token.span })
351            } else {
352                None
353            };
354
355        let help_cannot_start_number = self.is_lit_bad_ident().map(|(len, valid_portion)| {
356            let (invalid, valid) = self.token.span.split_at(len as u32);
357
358            recovered_ident = Some((Ident::new(valid_portion, valid), IdentIsRaw::No));
359
360            HelpIdentifierStartsWithNumber { num_span: invalid }
361        });
362
363        let err = ExpectedIdentifier {
364            span: bad_token.span,
365            token: bad_token,
366            suggest_raw,
367            suggest_remove_comma,
368            help_cannot_start_number,
369        };
370        let mut err = self.dcx().create_err(err);
371
372        // if the token we have is a `<`
373        // it *might* be a misplaced generic
374        // FIXME: could we recover with this?
375        if self.token == token::Lt {
376            // all keywords that could have generic applied
377            let valid_prev_keywords =
378                [kw::Fn, kw::Type, kw::Struct, kw::Enum, kw::Union, kw::Trait];
379
380            // If we've expected an identifier,
381            // and the current token is a '<'
382            // if the previous token is a valid keyword
383            // that might use a generic, then suggest a correct
384            // generic placement (later on)
385            let maybe_keyword = self.prev_token;
386            if valid_prev_keywords.into_iter().any(|x| maybe_keyword.is_keyword(x)) {
387                // if we have a valid keyword, attempt to parse generics
388                // also obtain the keywords symbol
389                match self.parse_generics() {
390                    Ok(generic) => {
391                        if let TokenKind::Ident(symbol, _) = maybe_keyword.kind {
392                            let ident_name = symbol;
393                            // at this point, we've found something like
394                            // `fn <T>id`
395                            // and current token should be Ident with the item name (i.e. the function name)
396                            // if there is a `<` after the fn name, then don't show a suggestion, show help
397
398                            if !self.look_ahead(1, |t| *t == token::Lt)
399                                && let Ok(snippet) =
400                                    self.psess.source_map().span_to_snippet(generic.span)
401                            {
402                                err.multipart_suggestion_verbose(
403                                        format!("place the generic parameter name after the {ident_name} name"),
404                                        vec![
405                                            (self.token.span.shrink_to_hi(), snippet),
406                                            (generic.span, String::new())
407                                        ],
408                                        Applicability::MaybeIncorrect,
409                                    );
410                            } else {
411                                err.help(format!(
412                                    "place the generic parameter name after the {ident_name} name"
413                                ));
414                            }
415                        }
416                    }
417                    Err(err) => {
418                        // if there's an error parsing the generics,
419                        // then don't do a misplaced generics suggestion
420                        // and emit the expected ident error instead;
421                        err.cancel();
422                    }
423                }
424            }
425        }
426
427        if let Some(recovered_ident) = recovered_ident
428            && recover
429        {
430            err.emit();
431            Ok(recovered_ident)
432        } else {
433            Err(err)
434        }
435    }
436
437    pub(super) fn expected_ident_found_err(&mut self) -> Diag<'a> {
438        self.expected_ident_found(false).unwrap_err()
439    }
440
441    /// Checks if the current token is a integer or float literal and looks like
442    /// it could be a invalid identifier with digits at the start.
443    ///
444    /// Returns the number of characters (bytes) composing the invalid portion
445    /// of the identifier and the valid portion of the identifier.
446    pub(super) fn is_lit_bad_ident(&mut self) -> Option<(usize, Symbol)> {
447        // ensure that the integer literal is followed by a *invalid*
448        // suffix: this is how we know that it is a identifier with an
449        // invalid beginning.
450        if let token::Literal(Lit {
451            kind: token::LitKind::Integer | token::LitKind::Float,
452            symbol,
453            suffix: Some(suffix), // no suffix makes it a valid literal
454        }) = self.token.kind
455            && rustc_ast::MetaItemLit::from_token(&self.token).is_none()
456        {
457            Some((symbol.as_str().len(), suffix))
458        } else {
459            None
460        }
461    }
462
463    pub(super) fn expected_one_of_not_found(
464        &mut self,
465        edible: &[ExpTokenPair<'_>],
466        inedible: &[ExpTokenPair<'_>],
467    ) -> PResult<'a, ErrorGuaranteed> {
468        debug!("expected_one_of_not_found(edible: {:?}, inedible: {:?})", edible, inedible);
469        fn tokens_to_string(tokens: &[TokenType]) -> String {
470            let mut i = tokens.iter();
471            // This might be a sign we need a connect method on `Iterator`.
472            let b = i.next().map_or_else(String::new, |t| t.to_string());
473            i.enumerate().fold(b, |mut b, (i, a)| {
474                if tokens.len() > 2 && i == tokens.len() - 2 {
475                    b.push_str(", or ");
476                } else if tokens.len() == 2 && i == tokens.len() - 2 {
477                    b.push_str(" or ");
478                } else {
479                    b.push_str(", ");
480                }
481                b.push_str(&a.to_string());
482                b
483            })
484        }
485
486        for exp in edible.iter().chain(inedible.iter()) {
487            self.expected_token_types.insert(exp.token_type);
488        }
489        let mut expected: Vec<_> = self.expected_token_types.iter().collect();
490        expected.sort_by_cached_key(|x| x.to_string());
491        expected.dedup();
492
493        let sm = self.psess.source_map();
494
495        // Special-case "expected `;`" errors.
496        if expected.contains(&TokenType::Semi) {
497            // If the user is trying to write a ternary expression, recover it and
498            // return an Err to prevent a cascade of irrelevant diagnostics.
499            if self.prev_token == token::Question
500                && let Err(e) = self.maybe_recover_from_ternary_operator(None)
501            {
502                return Err(e);
503            }
504
505            if self.token.span == DUMMY_SP || self.prev_token.span == DUMMY_SP {
506                // Likely inside a macro, can't provide meaningful suggestions.
507            } else if !sm.is_multiline(self.prev_token.span.until(self.token.span)) {
508                // The current token is in the same line as the prior token, not recoverable.
509            } else if [token::Comma, token::Colon].contains(&self.token.kind)
510                && self.prev_token == token::CloseParen
511            {
512                // Likely typo: The current token is on a new line and is expected to be
513                // `.`, `;`, `?`, or an operator after a close delimiter token.
514                //
515                // let a = std::process::Command::new("echo")
516                //         .arg("1")
517                //         ,arg("2")
518                //         ^
519                // https://github.com/rust-lang/rust/issues/72253
520            } else if self.look_ahead(1, |t| {
521                t == &token::CloseBrace || t.can_begin_expr() && *t != token::Colon
522            }) && [token::Comma, token::Colon].contains(&self.token.kind)
523            {
524                // Likely typo: `,` → `;` or `:` → `;`. This is triggered if the current token is
525                // either `,` or `:`, and the next token could either start a new statement or is a
526                // block close. For example:
527                //
528                //   let x = 32:
529                //   let y = 42;
530                let guar = self.dcx().emit_err(ExpectedSemi {
531                    span: self.token.span,
532                    token: self.token,
533                    unexpected_token_label: None,
534                    sugg: ExpectedSemiSugg::ChangeToSemi(self.token.span),
535                });
536                self.bump();
537                return Ok(guar);
538            } else if self.look_ahead(0, |t| {
539                t == &token::CloseBrace
540                    || ((t.can_begin_expr() || t.can_begin_item())
541                        && t != &token::Semi
542                        && t != &token::Pound)
543                    // Avoid triggering with too many trailing `#` in raw string.
544                    || (sm.is_multiline(
545                        self.prev_token.span.shrink_to_hi().until(self.token.span.shrink_to_lo()),
546                    ) && t == &token::Pound)
547            }) && !expected.contains(&TokenType::Comma)
548            {
549                // Missing semicolon typo. This is triggered if the next token could either start a
550                // new statement or is a block close. For example:
551                //
552                //   let x = 32
553                //   let y = 42;
554                let span = self.prev_token.span.shrink_to_hi();
555                let guar = self.dcx().emit_err(ExpectedSemi {
556                    span,
557                    token: self.token,
558                    unexpected_token_label: Some(self.token.span),
559                    sugg: ExpectedSemiSugg::AddSemi(span),
560                });
561                return Ok(guar);
562            }
563        }
564
565        if self.token == TokenKind::EqEq
566            && self.prev_token.is_ident()
567            && expected.contains(&TokenType::Eq)
568        {
569            // Likely typo: `=` → `==` in let expr or enum item
570            return Err(self.dcx().create_err(UseEqInstead { span: self.token.span }));
571        }
572
573        if (self.token.is_keyword(kw::Move) || self.token.is_keyword(kw::Use))
574            && self.prev_token.is_keyword(kw::Async)
575        {
576            // The 2015 edition is in use because parsing of `async move` or `async use` has failed.
577            let span = self.prev_token.span.to(self.token.span);
578            if self.token.is_keyword(kw::Move) {
579                return Err(self.dcx().create_err(AsyncMoveBlockIn2015 { span }));
580            } else {
581                // kw::Use
582                return Err(self.dcx().create_err(AsyncUseBlockIn2015 { span }));
583            }
584        }
585
586        let expect = tokens_to_string(&expected);
587        let actual = super::token_descr(&self.token);
588        let (msg_exp, (label_sp, label_exp)) = if expected.len() > 1 {
589            let fmt = format!("expected one of {expect}, found {actual}");
590            let short_expect = if expected.len() > 6 {
591                format!("{} possible tokens", expected.len())
592            } else {
593                expect
594            };
595            (fmt, (self.prev_token.span.shrink_to_hi(), format!("expected one of {short_expect}")))
596        } else if expected.is_empty() {
597            (
598                format!("unexpected token: {actual}"),
599                (self.prev_token.span, "unexpected token after this".to_string()),
600            )
601        } else {
602            (
603                format!("expected {expect}, found {actual}"),
604                (self.prev_token.span.shrink_to_hi(), format!("expected {expect}")),
605            )
606        };
607        self.last_unexpected_token_span = Some(self.token.span);
608        // FIXME: translation requires list formatting (for `expect`)
609        let mut err = self.dcx().struct_span_err(self.token.span, msg_exp);
610
611        self.label_expected_raw_ref(&mut err);
612
613        // Look for usages of '=>' where '>=' was probably intended
614        if self.token == token::FatArrow
615            && expected.iter().any(|tok| matches!(tok, TokenType::Operator | TokenType::Le))
616            && !expected.iter().any(|tok| matches!(tok, TokenType::FatArrow | TokenType::Comma))
617        {
618            err.span_suggestion(
619                self.token.span,
620                "you might have meant to write a \"greater than or equal to\" comparison",
621                ">=",
622                Applicability::MaybeIncorrect,
623            );
624        }
625
626        if let TokenKind::Ident(symbol, _) = &self.prev_token.kind {
627            if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {
628                err.span_suggestion_short(
629                    self.prev_token.span,
630                    format!("write `fn` instead of `{symbol}` to declare a function"),
631                    "fn",
632                    Applicability::MachineApplicable,
633                );
634            }
635        }
636
637        if let TokenKind::Ident(prev, _) = &self.prev_token.kind
638            && let TokenKind::Ident(cur, _) = &self.token.kind
639        {
640            let concat = Symbol::intern(&format!("{prev}{cur}"));
641            let ident = Ident::new(concat, DUMMY_SP);
642            if ident.is_used_keyword() || ident.is_reserved() || ident.is_raw_guess() {
643                let concat_span = self.prev_token.span.to(self.token.span);
644                err.span_suggestion_verbose(
645                    concat_span,
646                    format!("consider removing the space to spell keyword `{concat}`"),
647                    concat,
648                    Applicability::MachineApplicable,
649                );
650            }
651        }
652
653        // Try to detect an intended c-string literal while using a pre-2021 edition. The heuristic
654        // here is to identify a cooked, uninterpolated `c` id immediately followed by a string, or
655        // a cooked, uninterpolated `cr` id immediately followed by a string or a `#`, in an edition
656        // where c-string literals are not allowed. There is the very slight possibility of a false
657        // positive for a `cr#` that wasn't intended to start a c-string literal, but identifying
658        // that in the parser requires unbounded lookahead, so we only add a hint to the existing
659        // error rather than replacing it entirely.
660        if ((self.prev_token == TokenKind::Ident(sym::c, IdentIsRaw::No)
661            && matches!(&self.token.kind, TokenKind::Literal(token::Lit { kind: token::Str, .. })))
662            || (self.prev_token == TokenKind::Ident(sym::cr, IdentIsRaw::No)
663                && matches!(
664                    &self.token.kind,
665                    TokenKind::Literal(token::Lit { kind: token::Str, .. }) | token::Pound
666                )))
667            && self.prev_token.span.hi() == self.token.span.lo()
668            && !self.token.span.at_least_rust_2021()
669        {
670            err.note("you may be trying to write a c-string literal");
671            err.note("c-string literals require Rust 2021 or later");
672            err.subdiagnostic(HelpUseLatestEdition::new());
673        }
674
675        // `pub` may be used for an item or `pub(crate)`
676        if self.prev_token.is_ident_named(sym::public)
677            && (self.token.can_begin_item() || self.token == TokenKind::OpenParen)
678        {
679            err.span_suggestion_short(
680                self.prev_token.span,
681                "write `pub` instead of `public` to make the item public",
682                "pub",
683                Applicability::MachineApplicable,
684            );
685        }
686
687        if let token::DocComment(kind, style, _) = self.token.kind {
688            // This is to avoid suggesting converting a doc comment to a regular comment
689            // when missing a comma before the doc comment in lists (#142311):
690            //
691            // ```
692            // enum Foo{
693            //     A /// xxxxxxx
694            //     B,
695            // }
696            // ```
697            if !expected.contains(&TokenType::Comma) {
698                // We have something like `expr //!val` where the user likely meant `expr // !val`
699                let pos = self.token.span.lo() + BytePos(2);
700                let span = self.token.span.with_lo(pos).with_hi(pos);
701                err.span_suggestion_verbose(
702                    span,
703                    format!(
704                        "add a space before {} to write a regular comment",
705                        match (kind, style) {
706                            (token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",
707                            (token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",
708                            (token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",
709                            (token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",
710                        },
711                    ),
712                    " ".to_string(),
713                    Applicability::MaybeIncorrect,
714                );
715            }
716        }
717
718        let sp = if self.token == token::Eof {
719            // This is EOF; don't want to point at the following char, but rather the last token.
720            self.prev_token.span
721        } else {
722            label_sp
723        };
724
725        if self.check_too_many_raw_str_terminators(&mut err) {
726            if expected.contains(&TokenType::Semi) && self.eat(exp!(Semi)) {
727                let guar = err.emit();
728                return Ok(guar);
729            } else {
730                return Err(err);
731            }
732        }
733
734        if self.prev_token.span == DUMMY_SP {
735            // Account for macro context where the previous span might not be
736            // available to avoid incorrect output (#54841).
737            err.span_label(self.token.span, label_exp);
738        } else if !sm.is_multiline(self.token.span.shrink_to_hi().until(sp.shrink_to_lo())) {
739            // When the spans are in the same line, it means that the only content between
740            // them is whitespace, point at the found token in that case:
741            //
742            // X |     () => { syntax error };
743            //   |                    ^^^^^ expected one of 8 possible tokens here
744            //
745            // instead of having:
746            //
747            // X |     () => { syntax error };
748            //   |                   -^^^^^ unexpected token
749            //   |                   |
750            //   |                   expected one of 8 possible tokens here
751            err.span_label(self.token.span, label_exp);
752        } else {
753            err.span_label(sp, label_exp);
754            err.span_label(self.token.span, "unexpected token");
755        }
756
757        // Check for misspelled keywords if there are no suggestions added to the diagnostic.
758        if matches!(&err.suggestions, Suggestions::Enabled(list) if list.is_empty()) {
759            self.check_for_misspelled_kw(&mut err, &expected);
760        }
761        Err(err)
762    }
763
764    /// Adds a label when `&raw EXPR` was written instead of `&raw const EXPR`/`&raw mut EXPR`.
765    ///
766    /// Given that not all parser diagnostics flow through `expected_one_of_not_found`, this
767    /// label may need added to other diagnostics emission paths as needed.
768    pub(super) fn label_expected_raw_ref(&mut self, err: &mut Diag<'_>) {
769        if self.prev_token.is_keyword(kw::Raw)
770            && self.expected_token_types.contains(TokenType::KwMut)
771            && self.expected_token_types.contains(TokenType::KwConst)
772            && self.token.can_begin_expr()
773        {
774            err.span_suggestions(
775                self.prev_token.span.shrink_to_hi(),
776                "`&raw` must be followed by `const` or `mut` to be a raw reference expression",
777                [" const".to_string(), " mut".to_string()],
778                Applicability::MaybeIncorrect,
779            );
780        }
781    }
782
783    /// Checks if the current token or the previous token are misspelled keywords
784    /// and adds a helpful suggestion.
785    fn check_for_misspelled_kw(&self, err: &mut Diag<'_>, expected: &[TokenType]) {
786        let Some((curr_ident, _)) = self.token.ident() else {
787            return;
788        };
789        let expected_token_types: &[TokenType] =
790            expected.len().checked_sub(10).map_or(&expected, |index| &expected[index..]);
791        let expected_keywords: Vec<Symbol> =
792            expected_token_types.iter().filter_map(|token| token.is_keyword()).collect();
793
794        // When there are a few keywords in the last ten elements of `self.expected_token_types`
795        // and the current token is an identifier, it's probably a misspelled keyword. This handles
796        // code like `async Move {}`, misspelled `if` in match guard, misspelled `else` in
797        // `if`-`else` and misspelled `where` in a where clause.
798        if !expected_keywords.is_empty()
799            && !curr_ident.is_used_keyword()
800            && let Some(misspelled_kw) = find_similar_kw(curr_ident, &expected_keywords)
801        {
802            err.subdiagnostic(misspelled_kw);
803            // We don't want other suggestions to be added as they are most likely meaningless
804            // when there is a misspelled keyword.
805            err.seal_suggestions();
806        } else if let Some((prev_ident, _)) = self.prev_token.ident()
807            && !prev_ident.is_used_keyword()
808        {
809            // We generate a list of all keywords at runtime rather than at compile time
810            // so that it gets generated only when the diagnostic needs it.
811            // Also, it is unlikely that this list is generated multiple times because the
812            // parser halts after execution hits this path.
813            let all_keywords = used_keywords(|| prev_ident.span.edition());
814
815            // Otherwise, check the previous token with all the keywords as possible candidates.
816            // This handles code like `Struct Human;` and `While a < b {}`.
817            // We check the previous token only when the current token is an identifier to avoid
818            // false positives like suggesting keyword `for` for `extern crate foo {}`.
819            if let Some(misspelled_kw) = find_similar_kw(prev_ident, &all_keywords) {
820                err.subdiagnostic(misspelled_kw);
821                // We don't want other suggestions to be added as they are most likely meaningless
822                // when there is a misspelled keyword.
823                err.seal_suggestions();
824            }
825        }
826    }
827
828    /// The user has written `#[attr] expr` which is unsupported. (#106020)
829    pub(super) fn attr_on_non_tail_expr(&self, expr: &Expr) -> ErrorGuaranteed {
830        // Missing semicolon typo error.
831        let span = self.prev_token.span.shrink_to_hi();
832        let mut err = self.dcx().create_err(ExpectedSemi {
833            span,
834            token: self.token,
835            unexpected_token_label: Some(self.token.span),
836            sugg: ExpectedSemiSugg::AddSemi(span),
837        });
838        let attr_span = match &expr.attrs[..] {
839            [] => unreachable!(),
840            [only] => only.span,
841            [first, rest @ ..] => {
842                for attr in rest {
843                    err.span_label(attr.span, "");
844                }
845                first.span
846            }
847        };
848        err.span_label(
849            attr_span,
850            format!(
851                "only `;` terminated statements or tail expressions are allowed after {}",
852                if expr.attrs.len() == 1 { "this attribute" } else { "these attributes" },
853            ),
854        );
855        if self.token == token::Pound && self.look_ahead(1, |t| *t == token::OpenBracket) {
856            // We have
857            // #[attr]
858            // expr
859            // #[not_attr]
860            // other_expr
861            err.span_label(span, "expected `;` here");
862            err.multipart_suggestion(
863                "alternatively, consider surrounding the expression with a block",
864                vec![
865                    (expr.span.shrink_to_lo(), "{ ".to_string()),
866                    (expr.span.shrink_to_hi(), " }".to_string()),
867                ],
868                Applicability::MachineApplicable,
869            );
870
871            // Special handling for `#[cfg(...)]` chains
872            let mut snapshot = self.create_snapshot_for_diagnostic();
873            if let [attr] = &expr.attrs[..]
874                && let ast::AttrKind::Normal(attr_kind) = &attr.kind
875                && let [segment] = &attr_kind.item.path.segments[..]
876                && segment.ident.name == sym::cfg
877                && let Some(args_span) = attr_kind.item.args.span()
878                && let next_attr = match snapshot.parse_attribute(InnerAttrPolicy::Forbidden(None))
879                {
880                    Ok(next_attr) => next_attr,
881                    Err(inner_err) => {
882                        inner_err.cancel();
883                        return err.emit();
884                    }
885                }
886                && let ast::AttrKind::Normal(next_attr_kind) = next_attr.kind
887                && let Some(next_attr_args_span) = next_attr_kind.item.args.span()
888                && let [next_segment] = &next_attr_kind.item.path.segments[..]
889                && segment.ident.name == sym::cfg
890            {
891                let next_expr = match snapshot.parse_expr() {
892                    Ok(next_expr) => next_expr,
893                    Err(inner_err) => {
894                        inner_err.cancel();
895                        return err.emit();
896                    }
897                };
898                // We have for sure
899                // #[cfg(..)]
900                // expr
901                // #[cfg(..)]
902                // other_expr
903                // So we suggest using `if cfg!(..) { expr } else if cfg!(..) { other_expr }`.
904                let margin = self.psess.source_map().span_to_margin(next_expr.span).unwrap_or(0);
905                let sugg = vec![
906                    (attr.span.with_hi(segment.span().hi()), "if cfg!".to_string()),
907                    (args_span.shrink_to_hi().with_hi(attr.span.hi()), " {".to_string()),
908                    (expr.span.shrink_to_lo(), "    ".to_string()),
909                    (
910                        next_attr.span.with_hi(next_segment.span().hi()),
911                        "} else if cfg!".to_string(),
912                    ),
913                    (
914                        next_attr_args_span.shrink_to_hi().with_hi(next_attr.span.hi()),
915                        " {".to_string(),
916                    ),
917                    (next_expr.span.shrink_to_lo(), "    ".to_string()),
918                    (next_expr.span.shrink_to_hi(), format!("\n{}}}", " ".repeat(margin))),
919                ];
920                err.multipart_suggestion(
921                    "it seems like you are trying to provide different expressions depending on \
922                     `cfg`, consider using `if cfg!(..)`",
923                    sugg,
924                    Applicability::MachineApplicable,
925                );
926            }
927        }
928
929        err.emit()
930    }
931
932    fn check_too_many_raw_str_terminators(&mut self, err: &mut Diag<'_>) -> bool {
933        let sm = self.psess.source_map();
934        match (&self.prev_token.kind, &self.token.kind) {
935            (
936                TokenKind::Literal(Lit {
937                    kind: LitKind::StrRaw(n_hashes) | LitKind::ByteStrRaw(n_hashes),
938                    ..
939                }),
940                TokenKind::Pound,
941            ) if !sm.is_multiline(
942                self.prev_token.span.shrink_to_hi().until(self.token.span.shrink_to_lo()),
943            ) =>
944            {
945                let n_hashes: u8 = *n_hashes;
946                err.primary_message("too many `#` when terminating raw string");
947                let str_span = self.prev_token.span;
948                let mut span = self.token.span;
949                let mut count = 0;
950                while self.token == TokenKind::Pound
951                    && !sm.is_multiline(span.shrink_to_hi().until(self.token.span.shrink_to_lo()))
952                {
953                    span = span.with_hi(self.token.span.hi());
954                    self.bump();
955                    count += 1;
956                }
957                err.span(span);
958                err.span_suggestion(
959                    span,
960                    format!("remove the extra `#`{}", pluralize!(count)),
961                    "",
962                    Applicability::MachineApplicable,
963                );
964                err.span_label(
965                    str_span,
966                    format!("this raw string started with {n_hashes} `#`{}", pluralize!(n_hashes)),
967                );
968                true
969            }
970            _ => false,
971        }
972    }
973
974    pub(super) fn maybe_suggest_struct_literal(
975        &mut self,
976        lo: Span,
977        s: BlockCheckMode,
978        maybe_struct_name: token::Token,
979    ) -> Option<PResult<'a, Box<Block>>> {
980        if self.token.is_ident() && self.look_ahead(1, |t| t == &token::Colon) {
981            // We might be having a struct literal where people forgot to include the path:
982            // fn foo() -> Foo {
983            //     field: value,
984            // }
985            debug!(?maybe_struct_name, ?self.token);
986            let mut snapshot = self.create_snapshot_for_diagnostic();
987            let path = Path {
988                segments: ThinVec::new(),
989                span: self.prev_token.span.shrink_to_lo(),
990                tokens: None,
991            };
992            let struct_expr = snapshot.parse_expr_struct(None, path, false);
993            let block_tail = self.parse_block_tail(lo, s, AttemptLocalParseRecovery::No);
994            return Some(match (struct_expr, block_tail) {
995                (Ok(expr), Err(err)) => {
996                    // We have encountered the following:
997                    // fn foo() -> Foo {
998                    //     field: value,
999                    // }
1000                    // Suggest:
1001                    // fn foo() -> Foo { Path {
1002                    //     field: value,
1003                    // } }
1004                    err.cancel();
1005                    self.restore_snapshot(snapshot);
1006                    let guar = self.dcx().emit_err(StructLiteralBodyWithoutPath {
1007                        span: expr.span,
1008                        sugg: StructLiteralBodyWithoutPathSugg {
1009                            before: expr.span.shrink_to_lo(),
1010                            after: expr.span.shrink_to_hi(),
1011                        },
1012                    });
1013                    Ok(self.mk_block(
1014                        thin_vec![self.mk_stmt_err(expr.span, guar)],
1015                        s,
1016                        lo.to(self.prev_token.span),
1017                    ))
1018                }
1019                (Err(err), Ok(tail)) => {
1020                    // We have a block tail that contains a somehow valid expr.
1021                    err.cancel();
1022                    Ok(tail)
1023                }
1024                (Err(snapshot_err), Err(err)) => {
1025                    // We don't know what went wrong, emit the normal error.
1026                    snapshot_err.cancel();
1027                    self.consume_block(exp!(OpenBrace), exp!(CloseBrace), ConsumeClosingDelim::Yes);
1028                    Err(err)
1029                }
1030                (Ok(_), Ok(tail)) => Ok(tail),
1031            });
1032        }
1033        None
1034    }
1035
1036    pub(super) fn recover_closure_body(
1037        &mut self,
1038        mut err: Diag<'a>,
1039        before: token::Token,
1040        prev: token::Token,
1041        token: token::Token,
1042        lo: Span,
1043        decl_hi: Span,
1044    ) -> PResult<'a, Box<Expr>> {
1045        err.span_label(lo.to(decl_hi), "while parsing the body of this closure");
1046        let guar = match before.kind {
1047            token::OpenBrace if token.kind != token::OpenBrace => {
1048                // `{ || () }` should have been `|| { () }`
1049                err.multipart_suggestion(
1050                    "you might have meant to open the body of the closure, instead of enclosing \
1051                     the closure in a block",
1052                    vec![
1053                        (before.span, String::new()),
1054                        (prev.span.shrink_to_hi(), " {".to_string()),
1055                    ],
1056                    Applicability::MaybeIncorrect,
1057                );
1058                let guar = err.emit();
1059                self.eat_to_tokens(&[exp!(CloseBrace)]);
1060                guar
1061            }
1062            token::OpenParen if token.kind != token::OpenBrace => {
1063                // We are within a function call or tuple, we can emit the error
1064                // and recover.
1065                self.eat_to_tokens(&[exp!(CloseParen), exp!(Comma)]);
1066
1067                err.multipart_suggestion_verbose(
1068                    "you might have meant to open the body of the closure",
1069                    vec![
1070                        (prev.span.shrink_to_hi(), " {".to_string()),
1071                        (self.token.span.shrink_to_lo(), "}".to_string()),
1072                    ],
1073                    Applicability::MaybeIncorrect,
1074                );
1075                err.emit()
1076            }
1077            _ if token.kind != token::OpenBrace => {
1078                // We don't have a heuristic to correctly identify where the block
1079                // should be closed.
1080                err.multipart_suggestion_verbose(
1081                    "you might have meant to open the body of the closure",
1082                    vec![(prev.span.shrink_to_hi(), " {".to_string())],
1083                    Applicability::HasPlaceholders,
1084                );
1085                return Err(err);
1086            }
1087            _ => return Err(err),
1088        };
1089        Ok(self.mk_expr_err(lo.to(self.token.span), guar))
1090    }
1091
1092    /// Eats and discards tokens until one of `closes` is encountered. Respects token trees,
1093    /// passes through any errors encountered. Used for error recovery.
1094    pub(super) fn eat_to_tokens(&mut self, closes: &[ExpTokenPair<'_>]) {
1095        if let Err(err) = self
1096            .parse_seq_to_before_tokens(closes, &[], SeqSep::none(), |p| Ok(p.parse_token_tree()))
1097        {
1098            err.cancel();
1099        }
1100    }
1101
1102    /// This function checks if there are trailing angle brackets and produces
1103    /// a diagnostic to suggest removing them.
1104    ///
1105    /// ```ignore (diagnostic)
1106    /// let _ = [1, 2, 3].into_iter().collect::<Vec<usize>>>>();
1107    ///                                                    ^^ help: remove extra angle brackets
1108    /// ```
1109    ///
1110    /// If `true` is returned, then trailing brackets were recovered, tokens were consumed
1111    /// up until one of the tokens in 'end' was encountered, and an error was emitted.
1112    pub(super) fn check_trailing_angle_brackets(
1113        &mut self,
1114        segment: &PathSegment,
1115        end: &[ExpTokenPair<'_>],
1116    ) -> Option<ErrorGuaranteed> {
1117        if !self.may_recover() {
1118            return None;
1119        }
1120
1121        // This function is intended to be invoked after parsing a path segment where there are two
1122        // cases:
1123        //
1124        // 1. A specific token is expected after the path segment.
1125        //    eg. `x.foo(`, `x.foo::<u32>(` (parenthesis - method call),
1126        //        `Foo::`, or `Foo::<Bar>::` (mod sep - continued path).
1127        // 2. No specific token is expected after the path segment.
1128        //    eg. `x.foo` (field access)
1129        //
1130        // This function is called after parsing `.foo` and before parsing the token `end` (if
1131        // present). This includes any angle bracket arguments, such as `.foo::<u32>` or
1132        // `Foo::<Bar>`.
1133
1134        // We only care about trailing angle brackets if we previously parsed angle bracket
1135        // arguments. This helps stop us incorrectly suggesting that extra angle brackets be
1136        // removed in this case:
1137        //
1138        // `x.foo >> (3)` (where `x.foo` is a `u32` for example)
1139        //
1140        // This case is particularly tricky as we won't notice it just looking at the tokens -
1141        // it will appear the same (in terms of upcoming tokens) as below (since the `::<u32>` will
1142        // have already been parsed):
1143        //
1144        // `x.foo::<u32>>>(3)`
1145        let parsed_angle_bracket_args =
1146            segment.args.as_ref().is_some_and(|args| args.is_angle_bracketed());
1147
1148        debug!(
1149            "check_trailing_angle_brackets: parsed_angle_bracket_args={:?}",
1150            parsed_angle_bracket_args,
1151        );
1152        if !parsed_angle_bracket_args {
1153            return None;
1154        }
1155
1156        // Keep the span at the start so we can highlight the sequence of `>` characters to be
1157        // removed.
1158        let lo = self.token.span;
1159
1160        // We need to look-ahead to see if we have `>` characters without moving the cursor forward
1161        // (since we might have the field access case and the characters we're eating are
1162        // actual operators and not trailing characters - ie `x.foo >> 3`).
1163        let mut position = 0;
1164
1165        // We can encounter `>` or `>>` tokens in any order, so we need to keep track of how
1166        // many of each (so we can correctly pluralize our error messages) and continue to
1167        // advance.
1168        let mut number_of_shr = 0;
1169        let mut number_of_gt = 0;
1170        while self.look_ahead(position, |t| {
1171            trace!("check_trailing_angle_brackets: t={:?}", t);
1172            if *t == token::Shr {
1173                number_of_shr += 1;
1174                true
1175            } else if *t == token::Gt {
1176                number_of_gt += 1;
1177                true
1178            } else {
1179                false
1180            }
1181        }) {
1182            position += 1;
1183        }
1184
1185        // If we didn't find any trailing `>` characters, then we have nothing to error about.
1186        debug!(
1187            "check_trailing_angle_brackets: number_of_gt={:?} number_of_shr={:?}",
1188            number_of_gt, number_of_shr,
1189        );
1190        if number_of_gt < 1 && number_of_shr < 1 {
1191            return None;
1192        }
1193
1194        // Finally, double check that we have our end token as otherwise this is the
1195        // second case.
1196        if self.look_ahead(position, |t| {
1197            trace!("check_trailing_angle_brackets: t={:?}", t);
1198            end.iter().any(|exp| exp.tok == &t.kind)
1199        }) {
1200            // Eat from where we started until the end token so that parsing can continue
1201            // as if we didn't have those extra angle brackets.
1202            self.eat_to_tokens(end);
1203            let span = lo.to(self.prev_token.span);
1204
1205            let num_extra_brackets = number_of_gt + number_of_shr * 2;
1206            return Some(self.dcx().emit_err(UnmatchedAngleBrackets { span, num_extra_brackets }));
1207        }
1208        None
1209    }
1210
1211    /// Check if a method call with an intended turbofish has been written without surrounding
1212    /// angle brackets.
1213    pub(super) fn check_turbofish_missing_angle_brackets(&mut self, segment: &mut PathSegment) {
1214        if !self.may_recover() {
1215            return;
1216        }
1217
1218        if self.token == token::PathSep && segment.args.is_none() {
1219            let snapshot = self.create_snapshot_for_diagnostic();
1220            self.bump();
1221            let lo = self.token.span;
1222            match self.parse_angle_args(None) {
1223                Ok(args) => {
1224                    let span = lo.to(self.prev_token.span);
1225                    // Detect trailing `>` like in `x.collect::Vec<_>>()`.
1226                    let mut trailing_span = self.prev_token.span.shrink_to_hi();
1227                    while self.token == token::Shr || self.token == token::Gt {
1228                        trailing_span = trailing_span.to(self.token.span);
1229                        self.bump();
1230                    }
1231                    if self.token == token::OpenParen {
1232                        // Recover from bad turbofish: `foo.collect::Vec<_>()`.
1233                        segment.args = Some(AngleBracketedArgs { args, span }.into());
1234
1235                        self.dcx().emit_err(GenericParamsWithoutAngleBrackets {
1236                            span,
1237                            sugg: GenericParamsWithoutAngleBracketsSugg {
1238                                left: span.shrink_to_lo(),
1239                                right: trailing_span,
1240                            },
1241                        });
1242                    } else {
1243                        // This doesn't look like an invalid turbofish, can't recover parse state.
1244                        self.restore_snapshot(snapshot);
1245                    }
1246                }
1247                Err(err) => {
1248                    // We couldn't parse generic parameters, unlikely to be a turbofish. Rely on
1249                    // generic parse error instead.
1250                    err.cancel();
1251                    self.restore_snapshot(snapshot);
1252                }
1253            }
1254        }
1255    }
1256
1257    /// When writing a turbofish with multiple type parameters missing the leading `::`, we will
1258    /// encounter a parse error when encountering the first `,`.
1259    pub(super) fn check_mistyped_turbofish_with_multiple_type_params(
1260        &mut self,
1261        mut e: Diag<'a>,
1262        expr: &mut Box<Expr>,
1263    ) -> PResult<'a, ErrorGuaranteed> {
1264        if let ExprKind::Binary(binop, _, _) = &expr.kind
1265            && let ast::BinOpKind::Lt = binop.node
1266            && self.eat(exp!(Comma))
1267        {
1268            let x = self.parse_seq_to_before_end(
1269                exp!(Gt),
1270                SeqSep::trailing_allowed(exp!(Comma)),
1271                |p| match p.parse_generic_arg(None)? {
1272                    Some(arg) => Ok(arg),
1273                    // If we didn't eat a generic arg, then we should error.
1274                    None => p.unexpected_any(),
1275                },
1276            );
1277            match x {
1278                Ok((_, _, Recovered::No)) => {
1279                    if self.eat(exp!(Gt)) {
1280                        // We made sense of it. Improve the error message.
1281                        e.span_suggestion_verbose(
1282                            binop.span.shrink_to_lo(),
1283                            fluent::parse_sugg_turbofish_syntax,
1284                            "::",
1285                            Applicability::MaybeIncorrect,
1286                        );
1287                        match self.parse_expr() {
1288                            Ok(_) => {
1289                                // The subsequent expression is valid. Mark
1290                                // `expr` as erroneous and emit `e` now, but
1291                                // return `Ok` so parsing can continue.
1292                                let guar = e.emit();
1293                                *expr = self.mk_expr_err(expr.span.to(self.prev_token.span), guar);
1294                                return Ok(guar);
1295                            }
1296                            Err(err) => {
1297                                err.cancel();
1298                            }
1299                        }
1300                    }
1301                }
1302                Ok((_, _, Recovered::Yes(_))) => {}
1303                Err(err) => {
1304                    err.cancel();
1305                }
1306            }
1307        }
1308        Err(e)
1309    }
1310
1311    /// Suggest add the missing `let` before the identifier in stmt
1312    /// `a: Ty = 1` -> `let a: Ty = 1`
1313    pub(super) fn suggest_add_missing_let_for_stmt(&mut self, err: &mut Diag<'a>) {
1314        if self.token == token::Colon {
1315            let prev_span = self.prev_token.span.shrink_to_lo();
1316            let snapshot = self.create_snapshot_for_diagnostic();
1317            self.bump();
1318            match self.parse_ty() {
1319                Ok(_) => {
1320                    if self.token == token::Eq {
1321                        let sugg = SuggAddMissingLetStmt { span: prev_span };
1322                        sugg.add_to_diag(err);
1323                    }
1324                }
1325                Err(e) => {
1326                    e.cancel();
1327                }
1328            }
1329            self.restore_snapshot(snapshot);
1330        }
1331    }
1332
1333    /// Check to see if a pair of chained operators looks like an attempt at chained comparison,
1334    /// e.g. `1 < x <= 3`. If so, suggest either splitting the comparison into two, or
1335    /// parenthesising the leftmost comparison. The return value indicates if recovery happened.
1336    fn attempt_chained_comparison_suggestion(
1337        &mut self,
1338        err: &mut ComparisonOperatorsCannotBeChained,
1339        inner_op: &Expr,
1340        outer_op: &Spanned<AssocOp>,
1341    ) -> bool {
1342        if let ExprKind::Binary(op, l1, r1) = &inner_op.kind {
1343            if let ExprKind::Field(_, ident) = l1.kind
1344                && !ident.is_numeric()
1345                && !matches!(r1.kind, ExprKind::Lit(_))
1346            {
1347                // The parser has encountered `foo.bar<baz`, the likelihood of the turbofish
1348                // suggestion being the only one to apply is high.
1349                return false;
1350            }
1351            return match (op.node, &outer_op.node) {
1352                // `x == y == z`
1353                (BinOpKind::Eq, AssocOp::Binary(BinOpKind::Eq)) |
1354                // `x < y < z` and friends.
1355                (BinOpKind::Lt, AssocOp::Binary(BinOpKind::Lt | BinOpKind::Le)) |
1356                (BinOpKind::Le, AssocOp::Binary(BinOpKind::Lt | BinOpKind::Le)) |
1357                // `x > y > z` and friends.
1358                (BinOpKind::Gt, AssocOp::Binary(BinOpKind::Gt | BinOpKind::Ge)) |
1359                (BinOpKind::Ge, AssocOp::Binary(BinOpKind::Gt | BinOpKind::Ge)) => {
1360                    let expr_to_str = |e: &Expr| {
1361                        self.span_to_snippet(e.span)
1362                            .unwrap_or_else(|_| pprust::expr_to_string(e))
1363                    };
1364                    err.chaining_sugg = Some(ComparisonOperatorsCannotBeChainedSugg::SplitComparison {
1365                        span: inner_op.span.shrink_to_hi(),
1366                        middle_term: expr_to_str(r1),
1367                    });
1368                    false // Keep the current parse behavior, where the AST is `(x < y) < z`.
1369                }
1370                // `x == y < z`
1371                (
1372                    BinOpKind::Eq,
1373                    AssocOp::Binary(BinOpKind::Lt | BinOpKind::Le | BinOpKind::Gt | BinOpKind::Ge)
1374                ) => {
1375                    // Consume `z`/outer-op-rhs.
1376                    let snapshot = self.create_snapshot_for_diagnostic();
1377                    match self.parse_expr() {
1378                        Ok(r2) => {
1379                            // We are sure that outer-op-rhs could be consumed, the suggestion is
1380                            // likely correct.
1381                            err.chaining_sugg = Some(ComparisonOperatorsCannotBeChainedSugg::Parenthesize {
1382                                left: r1.span.shrink_to_lo(),
1383                                right: r2.span.shrink_to_hi(),
1384                            });
1385                            true
1386                        }
1387                        Err(expr_err) => {
1388                            expr_err.cancel();
1389                            self.restore_snapshot(snapshot);
1390                            true
1391                        }
1392                    }
1393                }
1394                // `x > y == z`
1395                (
1396                    BinOpKind::Lt | BinOpKind::Le | BinOpKind::Gt | BinOpKind::Ge,
1397                    AssocOp::Binary(BinOpKind::Eq)
1398                ) => {
1399                    let snapshot = self.create_snapshot_for_diagnostic();
1400                    // At this point it is always valid to enclose the lhs in parentheses, no
1401                    // further checks are necessary.
1402                    match self.parse_expr() {
1403                        Ok(_) => {
1404                            err.chaining_sugg = Some(ComparisonOperatorsCannotBeChainedSugg::Parenthesize {
1405                                left: l1.span.shrink_to_lo(),
1406                                right: r1.span.shrink_to_hi(),
1407                            });
1408                            true
1409                        }
1410                        Err(expr_err) => {
1411                            expr_err.cancel();
1412                            self.restore_snapshot(snapshot);
1413                            false
1414                        }
1415                    }
1416                }
1417                _ => false
1418            };
1419        }
1420        false
1421    }
1422
1423    /// Produces an error if comparison operators are chained (RFC #558).
1424    /// We only need to check the LHS, not the RHS, because all comparison ops have same
1425    /// precedence (see `fn precedence`) and are left-associative (see `fn fixity`).
1426    ///
1427    /// This can also be hit if someone incorrectly writes `foo<bar>()` when they should have used
1428    /// the turbofish (`foo::<bar>()`) syntax. We attempt some heuristic recovery if that is the
1429    /// case.
1430    ///
1431    /// Keep in mind that given that `outer_op.is_comparison()` holds and comparison ops are left
1432    /// associative we can infer that we have:
1433    ///
1434    /// ```text
1435    ///           outer_op
1436    ///           /   \
1437    ///     inner_op   r2
1438    ///        /  \
1439    ///      l1    r1
1440    /// ```
1441    pub(super) fn check_no_chained_comparison(
1442        &mut self,
1443        inner_op: &Expr,
1444        outer_op: &Spanned<AssocOp>,
1445    ) -> PResult<'a, Option<Box<Expr>>> {
1446        debug_assert!(
1447            outer_op.node.is_comparison(),
1448            "check_no_chained_comparison: {:?} is not comparison",
1449            outer_op.node,
1450        );
1451
1452        let mk_err_expr =
1453            |this: &Self, span, guar| Ok(Some(this.mk_expr(span, ExprKind::Err(guar))));
1454
1455        match &inner_op.kind {
1456            ExprKind::Binary(op, l1, r1) if op.node.is_comparison() => {
1457                let mut err = ComparisonOperatorsCannotBeChained {
1458                    span: vec![op.span, self.prev_token.span],
1459                    suggest_turbofish: None,
1460                    help_turbofish: false,
1461                    chaining_sugg: None,
1462                };
1463
1464                // Include `<` to provide this recommendation even in a case like
1465                // `Foo<Bar<Baz<Qux, ()>>>`
1466                if op.node == BinOpKind::Lt && outer_op.node == AssocOp::Binary(BinOpKind::Lt)
1467                    || outer_op.node == AssocOp::Binary(BinOpKind::Gt)
1468                {
1469                    if outer_op.node == AssocOp::Binary(BinOpKind::Lt) {
1470                        let snapshot = self.create_snapshot_for_diagnostic();
1471                        self.bump();
1472                        // So far we have parsed `foo<bar<`, consume the rest of the type args.
1473                        let modifiers = [(token::Lt, 1), (token::Gt, -1), (token::Shr, -2)];
1474                        self.consume_tts(1, &modifiers);
1475
1476                        if !matches!(self.token.kind, token::OpenParen | token::PathSep) {
1477                            // We don't have `foo< bar >(` or `foo< bar >::`, so we rewind the
1478                            // parser and bail out.
1479                            self.restore_snapshot(snapshot);
1480                        }
1481                    }
1482                    return if self.token == token::PathSep {
1483                        // We have some certainty that this was a bad turbofish at this point.
1484                        // `foo< bar >::`
1485                        if let ExprKind::Binary(o, ..) = inner_op.kind
1486                            && o.node == BinOpKind::Lt
1487                        {
1488                            err.suggest_turbofish = Some(op.span.shrink_to_lo());
1489                        } else {
1490                            err.help_turbofish = true;
1491                        }
1492
1493                        let snapshot = self.create_snapshot_for_diagnostic();
1494                        self.bump(); // `::`
1495
1496                        // Consume the rest of the likely `foo<bar>::new()` or return at `foo<bar>`.
1497                        match self.parse_expr() {
1498                            Ok(_) => {
1499                                // 99% certain that the suggestion is correct, continue parsing.
1500                                let guar = self.dcx().emit_err(err);
1501                                // FIXME: actually check that the two expressions in the binop are
1502                                // paths and resynthesize new fn call expression instead of using
1503                                // `ExprKind::Err` placeholder.
1504                                mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar)
1505                            }
1506                            Err(expr_err) => {
1507                                expr_err.cancel();
1508                                // Not entirely sure now, but we bubble the error up with the
1509                                // suggestion.
1510                                self.restore_snapshot(snapshot);
1511                                Err(self.dcx().create_err(err))
1512                            }
1513                        }
1514                    } else if self.token == token::OpenParen {
1515                        // We have high certainty that this was a bad turbofish at this point.
1516                        // `foo< bar >(`
1517                        if let ExprKind::Binary(o, ..) = inner_op.kind
1518                            && o.node == BinOpKind::Lt
1519                        {
1520                            err.suggest_turbofish = Some(op.span.shrink_to_lo());
1521                        } else {
1522                            err.help_turbofish = true;
1523                        }
1524                        // Consume the fn call arguments.
1525                        match self.consume_fn_args() {
1526                            Err(()) => Err(self.dcx().create_err(err)),
1527                            Ok(()) => {
1528                                let guar = self.dcx().emit_err(err);
1529                                // FIXME: actually check that the two expressions in the binop are
1530                                // paths and resynthesize new fn call expression instead of using
1531                                // `ExprKind::Err` placeholder.
1532                                mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar)
1533                            }
1534                        }
1535                    } else {
1536                        if !matches!(l1.kind, ExprKind::Lit(_))
1537                            && !matches!(r1.kind, ExprKind::Lit(_))
1538                        {
1539                            // All we know is that this is `foo < bar >` and *nothing* else. Try to
1540                            // be helpful, but don't attempt to recover.
1541                            err.help_turbofish = true;
1542                        }
1543
1544                        // If it looks like a genuine attempt to chain operators (as opposed to a
1545                        // misformatted turbofish, for instance), suggest a correct form.
1546                        let recovered = self
1547                            .attempt_chained_comparison_suggestion(&mut err, inner_op, outer_op);
1548                        if recovered {
1549                            let guar = self.dcx().emit_err(err);
1550                            mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar)
1551                        } else {
1552                            // These cases cause too many knock-down errors, bail out (#61329).
1553                            Err(self.dcx().create_err(err))
1554                        }
1555                    };
1556                }
1557                let recovered =
1558                    self.attempt_chained_comparison_suggestion(&mut err, inner_op, outer_op);
1559                let guar = self.dcx().emit_err(err);
1560                if recovered {
1561                    return mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar);
1562                }
1563            }
1564            _ => {}
1565        }
1566        Ok(None)
1567    }
1568
1569    fn consume_fn_args(&mut self) -> Result<(), ()> {
1570        let snapshot = self.create_snapshot_for_diagnostic();
1571        self.bump(); // `(`
1572
1573        // Consume the fn call arguments.
1574        let modifiers = [(token::OpenParen, 1), (token::CloseParen, -1)];
1575        self.consume_tts(1, &modifiers);
1576
1577        if self.token == token::Eof {
1578            // Not entirely sure that what we consumed were fn arguments, rollback.
1579            self.restore_snapshot(snapshot);
1580            Err(())
1581        } else {
1582            // 99% certain that the suggestion is correct, continue parsing.
1583            Ok(())
1584        }
1585    }
1586
1587    pub(super) fn maybe_report_ambiguous_plus(&mut self, impl_dyn_multi: bool, ty: &Ty) {
1588        if impl_dyn_multi {
1589            self.dcx().emit_err(AmbiguousPlus {
1590                span: ty.span,
1591                suggestion: AddParen { lo: ty.span.shrink_to_lo(), hi: ty.span.shrink_to_hi() },
1592            });
1593        }
1594    }
1595
1596    /// Swift lets users write `Ty?` to mean `Option<Ty>`. Parse the construct and recover from it.
1597    pub(super) fn maybe_recover_from_question_mark(&mut self, ty: Box<Ty>) -> Box<Ty> {
1598        if self.token == token::Question {
1599            self.bump();
1600            let guar = self.dcx().emit_err(QuestionMarkInType {
1601                span: self.prev_token.span,
1602                sugg: QuestionMarkInTypeSugg {
1603                    left: ty.span.shrink_to_lo(),
1604                    right: self.prev_token.span,
1605                },
1606            });
1607            self.mk_ty(ty.span.to(self.prev_token.span), TyKind::Err(guar))
1608        } else {
1609            ty
1610        }
1611    }
1612
1613    /// Rust has no ternary operator (`cond ? then : else`). Parse it and try
1614    /// to recover from it if `then` and `else` are valid expressions. Returns
1615    /// an err if this appears to be a ternary expression.
1616    /// If we have the span of the condition, we can provide a better error span
1617    /// and code suggestion.
1618    pub(super) fn maybe_recover_from_ternary_operator(
1619        &mut self,
1620        cond: Option<Span>,
1621    ) -> PResult<'a, ()> {
1622        if self.prev_token != token::Question {
1623            return PResult::Ok(());
1624        }
1625
1626        let question = self.prev_token.span;
1627        let lo = cond.unwrap_or(question).lo();
1628        let snapshot = self.create_snapshot_for_diagnostic();
1629
1630        if match self.parse_expr() {
1631            Ok(_) => true,
1632            Err(err) => {
1633                err.cancel();
1634                // The colon can sometimes be mistaken for type
1635                // ascription. Catch when this happens and continue.
1636                self.token == token::Colon
1637            }
1638        } {
1639            if self.eat_noexpect(&token::Colon) {
1640                let colon = self.prev_token.span;
1641                match self.parse_expr() {
1642                    Ok(expr) => {
1643                        let sugg = cond.map(|cond| TernaryOperatorSuggestion {
1644                            before_cond: cond.shrink_to_lo(),
1645                            question,
1646                            colon,
1647                            end: expr.span.shrink_to_hi(),
1648                        });
1649                        return Err(self.dcx().create_err(TernaryOperator {
1650                            span: self.prev_token.span.with_lo(lo),
1651                            sugg,
1652                            no_sugg: sugg.is_none(),
1653                        }));
1654                    }
1655                    Err(err) => {
1656                        err.cancel();
1657                    }
1658                };
1659            }
1660        }
1661        self.restore_snapshot(snapshot);
1662        Ok(())
1663    }
1664
1665    pub(super) fn maybe_recover_from_bad_type_plus(&mut self, ty: &Ty) -> PResult<'a, ()> {
1666        // Do not add `+` to expected tokens.
1667        if !self.token.is_like_plus() {
1668            return Ok(());
1669        }
1670
1671        self.bump(); // `+`
1672        let _bounds = self.parse_generic_bounds()?;
1673        let sub = match &ty.kind {
1674            TyKind::Ref(_lifetime, mut_ty) => {
1675                let lo = mut_ty.ty.span.shrink_to_lo();
1676                let hi = self.prev_token.span.shrink_to_hi();
1677                BadTypePlusSub::AddParen { suggestion: AddParen { lo, hi } }
1678            }
1679            TyKind::Ptr(..) | TyKind::FnPtr(..) => {
1680                BadTypePlusSub::ForgotParen { span: ty.span.to(self.prev_token.span) }
1681            }
1682            _ => BadTypePlusSub::ExpectPath { span: ty.span },
1683        };
1684
1685        self.dcx().emit_err(BadTypePlus { span: ty.span, sub });
1686
1687        Ok(())
1688    }
1689
1690    pub(super) fn recover_from_prefix_increment(
1691        &mut self,
1692        operand_expr: Box<Expr>,
1693        op_span: Span,
1694        start_stmt: bool,
1695    ) -> PResult<'a, Box<Expr>> {
1696        let standalone = if start_stmt { IsStandalone::Standalone } else { IsStandalone::Subexpr };
1697        let kind = IncDecRecovery { standalone, op: IncOrDec::Inc, fixity: UnaryFixity::Pre };
1698        self.recover_from_inc_dec(operand_expr, kind, op_span)
1699    }
1700
1701    pub(super) fn recover_from_postfix_increment(
1702        &mut self,
1703        operand_expr: Box<Expr>,
1704        op_span: Span,
1705        start_stmt: bool,
1706    ) -> PResult<'a, Box<Expr>> {
1707        let kind = IncDecRecovery {
1708            standalone: if start_stmt { IsStandalone::Standalone } else { IsStandalone::Subexpr },
1709            op: IncOrDec::Inc,
1710            fixity: UnaryFixity::Post,
1711        };
1712        self.recover_from_inc_dec(operand_expr, kind, op_span)
1713    }
1714
1715    pub(super) fn recover_from_postfix_decrement(
1716        &mut self,
1717        operand_expr: Box<Expr>,
1718        op_span: Span,
1719        start_stmt: bool,
1720    ) -> PResult<'a, Box<Expr>> {
1721        let kind = IncDecRecovery {
1722            standalone: if start_stmt { IsStandalone::Standalone } else { IsStandalone::Subexpr },
1723            op: IncOrDec::Dec,
1724            fixity: UnaryFixity::Post,
1725        };
1726        self.recover_from_inc_dec(operand_expr, kind, op_span)
1727    }
1728
1729    fn recover_from_inc_dec(
1730        &mut self,
1731        base: Box<Expr>,
1732        kind: IncDecRecovery,
1733        op_span: Span,
1734    ) -> PResult<'a, Box<Expr>> {
1735        let mut err = self.dcx().struct_span_err(
1736            op_span,
1737            format!("Rust has no {} {} operator", kind.fixity, kind.op.name()),
1738        );
1739        err.span_label(op_span, format!("not a valid {} operator", kind.fixity));
1740
1741        let help_base_case = |mut err: Diag<'_, _>, base| {
1742            err.help(format!("use `{}= 1` instead", kind.op.chr()));
1743            err.emit();
1744            Ok(base)
1745        };
1746
1747        // (pre, post)
1748        let spans = match kind.fixity {
1749            UnaryFixity::Pre => (op_span, base.span.shrink_to_hi()),
1750            UnaryFixity::Post => (base.span.shrink_to_lo(), op_span),
1751        };
1752
1753        match kind.standalone {
1754            IsStandalone::Standalone => {
1755                self.inc_dec_standalone_suggest(kind, spans).emit_verbose(&mut err)
1756            }
1757            IsStandalone::Subexpr => {
1758                let Ok(base_src) = self.span_to_snippet(base.span) else {
1759                    return help_base_case(err, base);
1760                };
1761                match kind.fixity {
1762                    UnaryFixity::Pre => {
1763                        self.prefix_inc_dec_suggest(base_src, kind, spans).emit(&mut err)
1764                    }
1765                    UnaryFixity::Post => {
1766                        // won't suggest since we can not handle the precedences
1767                        // for example: `a + b++` has been parsed (a + b)++ and we can not suggest here
1768                        if !matches!(base.kind, ExprKind::Binary(_, _, _)) {
1769                            self.postfix_inc_dec_suggest(base_src, kind, spans).emit(&mut err)
1770                        }
1771                    }
1772                }
1773            }
1774        }
1775        Err(err)
1776    }
1777
1778    fn prefix_inc_dec_suggest(
1779        &mut self,
1780        base_src: String,
1781        kind: IncDecRecovery,
1782        (pre_span, post_span): (Span, Span),
1783    ) -> MultiSugg {
1784        MultiSugg {
1785            msg: format!("use `{}= 1` instead", kind.op.chr()),
1786            patches: vec![
1787                (pre_span, "{ ".to_string()),
1788                (post_span, format!(" {}= 1; {} }}", kind.op.chr(), base_src)),
1789            ],
1790            applicability: Applicability::MachineApplicable,
1791        }
1792    }
1793
1794    fn postfix_inc_dec_suggest(
1795        &mut self,
1796        base_src: String,
1797        kind: IncDecRecovery,
1798        (pre_span, post_span): (Span, Span),
1799    ) -> MultiSugg {
1800        let tmp_var = if base_src.trim() == "tmp" { "tmp_" } else { "tmp" };
1801        MultiSugg {
1802            msg: format!("use `{}= 1` instead", kind.op.chr()),
1803            patches: vec![
1804                (pre_span, format!("{{ let {tmp_var} = ")),
1805                (post_span, format!("; {} {}= 1; {} }}", base_src, kind.op.chr(), tmp_var)),
1806            ],
1807            applicability: Applicability::HasPlaceholders,
1808        }
1809    }
1810
1811    fn inc_dec_standalone_suggest(
1812        &mut self,
1813        kind: IncDecRecovery,
1814        (pre_span, post_span): (Span, Span),
1815    ) -> MultiSugg {
1816        let mut patches = Vec::new();
1817
1818        if !pre_span.is_empty() {
1819            patches.push((pre_span, String::new()));
1820        }
1821
1822        patches.push((post_span, format!(" {}= 1", kind.op.chr())));
1823        MultiSugg {
1824            msg: format!("use `{}= 1` instead", kind.op.chr()),
1825            patches,
1826            applicability: Applicability::MachineApplicable,
1827        }
1828    }
1829
1830    /// Tries to recover from associated item paths like `[T]::AssocItem` / `(T, U)::AssocItem`.
1831    /// Attempts to convert the base expression/pattern/type into a type, parses the `::AssocItem`
1832    /// tail, and combines them into a `<Ty>::AssocItem` expression/pattern/type.
1833    pub(super) fn maybe_recover_from_bad_qpath<T: RecoverQPath>(
1834        &mut self,
1835        base: Box<T>,
1836    ) -> PResult<'a, Box<T>> {
1837        if !self.may_recover() {
1838            return Ok(base);
1839        }
1840
1841        // Do not add `::` to expected tokens.
1842        if self.token == token::PathSep {
1843            if let Some(ty) = base.to_ty() {
1844                return self.maybe_recover_from_bad_qpath_stage_2(ty.span, ty);
1845            }
1846        }
1847        Ok(base)
1848    }
1849
1850    /// Given an already parsed `Ty`, parses the `::AssocItem` tail and
1851    /// combines them into a `<Ty>::AssocItem` expression/pattern/type.
1852    pub(super) fn maybe_recover_from_bad_qpath_stage_2<T: RecoverQPath>(
1853        &mut self,
1854        ty_span: Span,
1855        ty: Box<Ty>,
1856    ) -> PResult<'a, Box<T>> {
1857        self.expect(exp!(PathSep))?;
1858
1859        let mut path = ast::Path { segments: ThinVec::new(), span: DUMMY_SP, tokens: None };
1860        self.parse_path_segments(&mut path.segments, T::PATH_STYLE, None)?;
1861        path.span = ty_span.to(self.prev_token.span);
1862
1863        self.dcx().emit_err(BadQPathStage2 {
1864            span: ty_span,
1865            wrap: WrapType { lo: ty_span.shrink_to_lo(), hi: ty_span.shrink_to_hi() },
1866        });
1867
1868        let path_span = ty_span.shrink_to_hi(); // Use an empty path since `position == 0`.
1869        Ok(Box::new(T::recovered(Some(Box::new(QSelf { ty, path_span, position: 0 })), path)))
1870    }
1871
1872    /// This function gets called in places where a semicolon is NOT expected and if there's a
1873    /// semicolon it emits the appropriate error and returns true.
1874    pub fn maybe_consume_incorrect_semicolon(&mut self, previous_item: Option<&Item>) -> bool {
1875        if self.token != TokenKind::Semi {
1876            return false;
1877        }
1878
1879        // Check previous item to add it to the diagnostic, for example to say
1880        // `enum declarations are not followed by a semicolon`
1881        let err = match previous_item {
1882            Some(previous_item) => {
1883                let name = match previous_item.kind {
1884                    // Say "braced struct" because tuple-structs and
1885                    // braceless-empty-struct declarations do take a semicolon.
1886                    ItemKind::Struct(..) => "braced struct",
1887                    _ => previous_item.kind.descr(),
1888                };
1889                IncorrectSemicolon { span: self.token.span, name, show_help: true }
1890            }
1891            None => IncorrectSemicolon { span: self.token.span, name: "", show_help: false },
1892        };
1893        self.dcx().emit_err(err);
1894
1895        self.bump();
1896        true
1897    }
1898
1899    /// Creates a `Diag` for an unexpected token `t` and tries to recover if it is a
1900    /// closing delimiter.
1901    pub(super) fn unexpected_try_recover(&mut self, t: &TokenKind) -> PResult<'a, Recovered> {
1902        let token_str = pprust::token_kind_to_string(t);
1903        let this_token_str = super::token_descr(&self.token);
1904        let (prev_sp, sp) = match (&self.token.kind, self.subparser_name) {
1905            // Point at the end of the macro call when reaching end of macro arguments.
1906            (token::Eof, Some(_)) => {
1907                let sp = self.prev_token.span.shrink_to_hi();
1908                (sp, sp)
1909            }
1910            // We don't want to point at the following span after DUMMY_SP.
1911            // This happens when the parser finds an empty TokenStream.
1912            _ if self.prev_token.span == DUMMY_SP => (self.token.span, self.token.span),
1913            // EOF, don't want to point at the following char, but rather the last token.
1914            (token::Eof, None) => (self.prev_token.span, self.token.span),
1915            _ => (self.prev_token.span.shrink_to_hi(), self.token.span),
1916        };
1917        let msg = format!(
1918            "expected `{}`, found {}",
1919            token_str,
1920            match (&self.token.kind, self.subparser_name) {
1921                (token::Eof, Some(origin)) => format!("end of {origin}"),
1922                _ => this_token_str,
1923            },
1924        );
1925        let mut err = self.dcx().struct_span_err(sp, msg);
1926        let label_exp = format!("expected `{token_str}`");
1927        let sm = self.psess.source_map();
1928        if !sm.is_multiline(prev_sp.until(sp)) {
1929            // When the spans are in the same line, it means that the only content
1930            // between them is whitespace, point only at the found token.
1931            err.span_label(sp, label_exp);
1932        } else {
1933            err.span_label(prev_sp, label_exp);
1934            err.span_label(sp, "unexpected token");
1935        }
1936        Err(err)
1937    }
1938
1939    pub(super) fn expect_semi(&mut self) -> PResult<'a, ()> {
1940        if self.eat(exp!(Semi)) || self.recover_colon_as_semi() {
1941            return Ok(());
1942        }
1943        self.expect(exp!(Semi)).map(drop) // Error unconditionally
1944    }
1945
1946    pub(super) fn recover_colon_as_semi(&mut self) -> bool {
1947        let line_idx = |span: Span| {
1948            self.psess
1949                .source_map()
1950                .span_to_lines(span)
1951                .ok()
1952                .and_then(|lines| Some(lines.lines.get(0)?.line_index))
1953        };
1954
1955        if self.may_recover()
1956            && self.token == token::Colon
1957            && self.look_ahead(1, |next| line_idx(self.token.span) < line_idx(next.span))
1958        {
1959            self.dcx().emit_err(ColonAsSemi { span: self.token.span });
1960            self.bump();
1961            return true;
1962        }
1963
1964        false
1965    }
1966
1967    /// Consumes alternative await syntaxes like `await!(<expr>)`, `await <expr>`,
1968    /// `await? <expr>`, `await(<expr>)`, and `await { <expr> }`.
1969    pub(super) fn recover_incorrect_await_syntax(
1970        &mut self,
1971        await_sp: Span,
1972    ) -> PResult<'a, Box<Expr>> {
1973        let (hi, expr, is_question) = if self.token == token::Bang {
1974            // Handle `await!(<expr>)`.
1975            self.recover_await_macro()?
1976        } else {
1977            self.recover_await_prefix(await_sp)?
1978        };
1979        let (sp, guar) = self.error_on_incorrect_await(await_sp, hi, &expr, is_question);
1980        let expr = self.mk_expr_err(await_sp.to(sp), guar);
1981        self.maybe_recover_from_bad_qpath(expr)
1982    }
1983
1984    fn recover_await_macro(&mut self) -> PResult<'a, (Span, Box<Expr>, bool)> {
1985        self.expect(exp!(Bang))?;
1986        self.expect(exp!(OpenParen))?;
1987        let expr = self.parse_expr()?;
1988        self.expect(exp!(CloseParen))?;
1989        Ok((self.prev_token.span, expr, false))
1990    }
1991
1992    fn recover_await_prefix(&mut self, await_sp: Span) -> PResult<'a, (Span, Box<Expr>, bool)> {
1993        let is_question = self.eat(exp!(Question)); // Handle `await? <expr>`.
1994        let expr = if self.token == token::OpenBrace {
1995            // Handle `await { <expr> }`.
1996            // This needs to be handled separately from the next arm to avoid
1997            // interpreting `await { <expr> }?` as `<expr>?.await`.
1998            self.parse_expr_block(None, self.token.span, BlockCheckMode::Default)
1999        } else {
2000            self.parse_expr()
2001        }
2002        .map_err(|mut err| {
2003            err.span_label(await_sp, format!("while parsing this incorrect await expression"));
2004            err
2005        })?;
2006        Ok((expr.span, expr, is_question))
2007    }
2008
2009    fn error_on_incorrect_await(
2010        &self,
2011        lo: Span,
2012        hi: Span,
2013        expr: &Expr,
2014        is_question: bool,
2015    ) -> (Span, ErrorGuaranteed) {
2016        let span = lo.to(hi);
2017        let guar = self.dcx().emit_err(IncorrectAwait {
2018            span,
2019            suggestion: AwaitSuggestion {
2020                removal: lo.until(expr.span),
2021                dot_await: expr.span.shrink_to_hi(),
2022                question_mark: if is_question { "?" } else { "" },
2023            },
2024        });
2025        (span, guar)
2026    }
2027
2028    /// If encountering `future.await()`, consumes and emits an error.
2029    pub(super) fn recover_from_await_method_call(&mut self) {
2030        if self.token == token::OpenParen && self.look_ahead(1, |t| t == &token::CloseParen) {
2031            // future.await()
2032            let lo = self.token.span;
2033            self.bump(); // (
2034            let span = lo.to(self.token.span);
2035            self.bump(); // )
2036
2037            self.dcx().emit_err(IncorrectUseOfAwait { span });
2038        }
2039    }
2040    ///
2041    /// If encountering `x.use()`, consumes and emits an error.
2042    pub(super) fn recover_from_use(&mut self) {
2043        if self.token == token::OpenParen && self.look_ahead(1, |t| t == &token::CloseParen) {
2044            // var.use()
2045            let lo = self.token.span;
2046            self.bump(); // (
2047            let span = lo.to(self.token.span);
2048            self.bump(); // )
2049
2050            self.dcx().emit_err(IncorrectUseOfUse { span });
2051        }
2052    }
2053
2054    pub(super) fn try_macro_suggestion(&mut self) -> PResult<'a, Box<Expr>> {
2055        let is_try = self.token.is_keyword(kw::Try);
2056        let is_questionmark = self.look_ahead(1, |t| t == &token::Bang); //check for !
2057        let is_open = self.look_ahead(2, |t| t == &token::OpenParen); //check for (
2058
2059        if is_try && is_questionmark && is_open {
2060            let lo = self.token.span;
2061            self.bump(); //remove try
2062            self.bump(); //remove !
2063            let try_span = lo.to(self.token.span); //we take the try!( span
2064            self.bump(); //remove (
2065            let is_empty = self.token == token::CloseParen; //check if the block is empty
2066            self.consume_block(exp!(OpenParen), exp!(CloseParen), ConsumeClosingDelim::No); //eat the block
2067            let hi = self.token.span;
2068            self.bump(); //remove )
2069            let mut err = self.dcx().struct_span_err(lo.to(hi), "use of deprecated `try` macro");
2070            err.note("in the 2018 edition `try` is a reserved keyword, and the `try!()` macro is deprecated");
2071            let prefix = if is_empty { "" } else { "alternatively, " };
2072            if !is_empty {
2073                err.multipart_suggestion(
2074                    "you can use the `?` operator instead",
2075                    vec![(try_span, "".to_owned()), (hi, "?".to_owned())],
2076                    Applicability::MachineApplicable,
2077                );
2078            }
2079            err.span_suggestion(lo.shrink_to_lo(), format!("{prefix}you can still access the deprecated `try!()` macro using the \"raw identifier\" syntax"), "r#", Applicability::MachineApplicable);
2080            let guar = err.emit();
2081            Ok(self.mk_expr_err(lo.to(hi), guar))
2082        } else {
2083            Err(self.expected_expression_found()) // The user isn't trying to invoke the try! macro
2084        }
2085    }
2086
2087    /// When trying to close a generics list and encountering code like
2088    /// ```text
2089    /// impl<S: Into<std::borrow::Cow<'static, str>> From<S> for Canonical {}
2090    ///                                          // ^ missing > here
2091    /// ```
2092    /// we provide a structured suggestion on the error from `expect_gt`.
2093    pub(super) fn expect_gt_or_maybe_suggest_closing_generics(
2094        &mut self,
2095        params: &[ast::GenericParam],
2096    ) -> PResult<'a, ()> {
2097        let Err(mut err) = self.expect_gt() else {
2098            return Ok(());
2099        };
2100        // Attempt to find places where a missing `>` might belong.
2101        if let [.., ast::GenericParam { bounds, .. }] = params
2102            && let Some(poly) = bounds
2103                .iter()
2104                .filter_map(|bound| match bound {
2105                    ast::GenericBound::Trait(poly) => Some(poly),
2106                    _ => None,
2107                })
2108                .next_back()
2109        {
2110            err.span_suggestion_verbose(
2111                poly.span.shrink_to_hi(),
2112                "you might have meant to end the type parameters here",
2113                ">",
2114                Applicability::MaybeIncorrect,
2115            );
2116        }
2117        Err(err)
2118    }
2119
2120    pub(super) fn recover_seq_parse_error(
2121        &mut self,
2122        open: ExpTokenPair<'_>,
2123        close: ExpTokenPair<'_>,
2124        lo: Span,
2125        err: Diag<'a>,
2126    ) -> Box<Expr> {
2127        let guar = err.emit();
2128        // Recover from parse error, callers expect the closing delim to be consumed.
2129        self.consume_block(open, close, ConsumeClosingDelim::Yes);
2130        self.mk_expr(lo.to(self.prev_token.span), ExprKind::Err(guar))
2131    }
2132
2133    /// Eats tokens until we can be relatively sure we reached the end of the
2134    /// statement. This is something of a best-effort heuristic.
2135    ///
2136    /// We terminate when we find an unmatched `}` (without consuming it).
2137    pub(super) fn recover_stmt(&mut self) {
2138        self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore)
2139    }
2140
2141    /// If `break_on_semi` is `Break`, then we will stop consuming tokens after
2142    /// finding (and consuming) a `;` outside of `{}` or `[]` (note that this is
2143    /// approximate -- it can mean we break too early due to macros, but that
2144    /// should only lead to sub-optimal recovery, not inaccurate parsing).
2145    ///
2146    /// If `break_on_block` is `Break`, then we will stop consuming tokens
2147    /// after finding (and consuming) a brace-delimited block.
2148    pub(super) fn recover_stmt_(
2149        &mut self,
2150        break_on_semi: SemiColonMode,
2151        break_on_block: BlockMode,
2152    ) {
2153        let mut brace_depth = 0;
2154        let mut bracket_depth = 0;
2155        let mut in_block = false;
2156        debug!("recover_stmt_ enter loop (semi={:?}, block={:?})", break_on_semi, break_on_block);
2157        loop {
2158            debug!("recover_stmt_ loop {:?}", self.token);
2159            match self.token.kind {
2160                token::OpenBrace => {
2161                    brace_depth += 1;
2162                    self.bump();
2163                    if break_on_block == BlockMode::Break && brace_depth == 1 && bracket_depth == 0
2164                    {
2165                        in_block = true;
2166                    }
2167                }
2168                token::OpenBracket => {
2169                    bracket_depth += 1;
2170                    self.bump();
2171                }
2172                token::CloseBrace => {
2173                    if brace_depth == 0 {
2174                        debug!("recover_stmt_ return - close delim {:?}", self.token);
2175                        break;
2176                    }
2177                    brace_depth -= 1;
2178                    self.bump();
2179                    if in_block && bracket_depth == 0 && brace_depth == 0 {
2180                        debug!("recover_stmt_ return - block end {:?}", self.token);
2181                        break;
2182                    }
2183                }
2184                token::CloseBracket => {
2185                    bracket_depth -= 1;
2186                    if bracket_depth < 0 {
2187                        bracket_depth = 0;
2188                    }
2189                    self.bump();
2190                }
2191                token::Eof => {
2192                    debug!("recover_stmt_ return - Eof");
2193                    break;
2194                }
2195                token::Semi => {
2196                    self.bump();
2197                    if break_on_semi == SemiColonMode::Break
2198                        && brace_depth == 0
2199                        && bracket_depth == 0
2200                    {
2201                        debug!("recover_stmt_ return - Semi");
2202                        break;
2203                    }
2204                }
2205                token::Comma
2206                    if break_on_semi == SemiColonMode::Comma
2207                        && brace_depth == 0
2208                        && bracket_depth == 0 =>
2209                {
2210                    break;
2211                }
2212                _ => self.bump(),
2213            }
2214        }
2215    }
2216
2217    pub(super) fn check_for_for_in_in_typo(&mut self, in_span: Span) {
2218        if self.eat_keyword(exp!(In)) {
2219            // a common typo: `for _ in in bar {}`
2220            self.dcx().emit_err(InInTypo {
2221                span: self.prev_token.span,
2222                sugg_span: in_span.until(self.prev_token.span),
2223            });
2224        }
2225    }
2226
2227    pub(super) fn eat_incorrect_doc_comment_for_param_type(&mut self) {
2228        if let token::DocComment(..) = self.token.kind {
2229            self.dcx().emit_err(DocCommentOnParamType { span: self.token.span });
2230            self.bump();
2231        } else if self.token == token::Pound && self.look_ahead(1, |t| *t == token::OpenBracket) {
2232            let lo = self.token.span;
2233            // Skip every token until next possible arg.
2234            while self.token != token::CloseBracket {
2235                self.bump();
2236            }
2237            let sp = lo.to(self.token.span);
2238            self.bump();
2239            self.dcx().emit_err(AttributeOnParamType { span: sp });
2240        }
2241    }
2242
2243    pub(super) fn parameter_without_type(
2244        &mut self,
2245        err: &mut Diag<'_>,
2246        pat: Box<ast::Pat>,
2247        require_name: bool,
2248        first_param: bool,
2249    ) -> Option<Ident> {
2250        // If we find a pattern followed by an identifier, it could be an (incorrect)
2251        // C-style parameter declaration.
2252        if self.check_ident()
2253            && self.look_ahead(1, |t| *t == token::Comma || *t == token::CloseParen)
2254        {
2255            // `fn foo(String s) {}`
2256            let ident = self.parse_ident().unwrap();
2257            let span = pat.span.with_hi(ident.span.hi());
2258
2259            err.span_suggestion(
2260                span,
2261                "declare the type after the parameter binding",
2262                "<identifier>: <type>",
2263                Applicability::HasPlaceholders,
2264            );
2265            return Some(ident);
2266        } else if require_name
2267            && (self.token == token::Comma
2268                || self.token == token::Lt
2269                || self.token == token::CloseParen)
2270        {
2271            let rfc_note = "anonymous parameters are removed in the 2018 edition (see RFC 1685)";
2272
2273            let (ident, self_sugg, param_sugg, type_sugg, self_span, param_span, type_span) =
2274                match pat.kind {
2275                    PatKind::Ident(_, ident, _) => (
2276                        ident,
2277                        "self: ",
2278                        ": TypeName".to_string(),
2279                        "_: ",
2280                        pat.span.shrink_to_lo(),
2281                        pat.span.shrink_to_hi(),
2282                        pat.span.shrink_to_lo(),
2283                    ),
2284                    // Also catches `fn foo(&a)`.
2285                    PatKind::Ref(ref inner_pat, mutab)
2286                        if let PatKind::Ident(_, ident, _) = inner_pat.clone().kind =>
2287                    {
2288                        let mutab = mutab.prefix_str();
2289                        (
2290                            ident,
2291                            "self: ",
2292                            format!("{ident}: &{mutab}TypeName"),
2293                            "_: ",
2294                            pat.span.shrink_to_lo(),
2295                            pat.span,
2296                            pat.span.shrink_to_lo(),
2297                        )
2298                    }
2299                    _ => {
2300                        // Otherwise, try to get a type and emit a suggestion.
2301                        if let Some(_) = pat.to_ty() {
2302                            err.span_suggestion_verbose(
2303                                pat.span.shrink_to_lo(),
2304                                "explicitly ignore the parameter name",
2305                                "_: ".to_string(),
2306                                Applicability::MachineApplicable,
2307                            );
2308                            err.note(rfc_note);
2309                        }
2310
2311                        return None;
2312                    }
2313                };
2314
2315            // `fn foo(a, b) {}`, `fn foo(a<x>, b<y>) {}` or `fn foo(usize, usize) {}`
2316            if first_param {
2317                err.span_suggestion_verbose(
2318                    self_span,
2319                    "if this is a `self` type, give it a parameter name",
2320                    self_sugg,
2321                    Applicability::MaybeIncorrect,
2322                );
2323            }
2324            // Avoid suggesting that `fn foo(HashMap<u32>)` is fixed with a change to
2325            // `fn foo(HashMap: TypeName<u32>)`.
2326            if self.token != token::Lt {
2327                err.span_suggestion_verbose(
2328                    param_span,
2329                    "if this is a parameter name, give it a type",
2330                    param_sugg,
2331                    Applicability::HasPlaceholders,
2332                );
2333            }
2334            err.span_suggestion_verbose(
2335                type_span,
2336                "if this is a type, explicitly ignore the parameter name",
2337                type_sugg,
2338                Applicability::MachineApplicable,
2339            );
2340            err.note(rfc_note);
2341
2342            // Don't attempt to recover by using the `X` in `X<Y>` as the parameter name.
2343            return if self.token == token::Lt { None } else { Some(ident) };
2344        }
2345        None
2346    }
2347
2348    pub(super) fn recover_arg_parse(&mut self) -> PResult<'a, (Box<ast::Pat>, Box<ast::Ty>)> {
2349        let pat = self.parse_pat_no_top_alt(Some(Expected::ArgumentName), None)?;
2350        self.expect(exp!(Colon))?;
2351        let ty = self.parse_ty()?;
2352
2353        self.dcx().emit_err(PatternMethodParamWithoutBody { span: pat.span });
2354
2355        // Pretend the pattern is `_`, to avoid duplicate errors from AST validation.
2356        let pat = Box::new(Pat {
2357            kind: PatKind::Wild,
2358            span: pat.span,
2359            id: ast::DUMMY_NODE_ID,
2360            tokens: None,
2361        });
2362        Ok((pat, ty))
2363    }
2364
2365    pub(super) fn recover_bad_self_param(&mut self, mut param: Param) -> PResult<'a, Param> {
2366        let span = param.pat.span;
2367        let guar = self.dcx().emit_err(SelfParamNotFirst { span });
2368        param.ty.kind = TyKind::Err(guar);
2369        Ok(param)
2370    }
2371
2372    pub(super) fn consume_block(
2373        &mut self,
2374        open: ExpTokenPair<'_>,
2375        close: ExpTokenPair<'_>,
2376        consume_close: ConsumeClosingDelim,
2377    ) {
2378        let mut brace_depth = 0;
2379        loop {
2380            if self.eat(open) {
2381                brace_depth += 1;
2382            } else if self.check(close) {
2383                if brace_depth == 0 {
2384                    if let ConsumeClosingDelim::Yes = consume_close {
2385                        // Some of the callers of this method expect to be able to parse the
2386                        // closing delimiter themselves, so we leave it alone. Otherwise we advance
2387                        // the parser.
2388                        self.bump();
2389                    }
2390                    return;
2391                } else {
2392                    self.bump();
2393                    brace_depth -= 1;
2394                    continue;
2395                }
2396            } else if self.token == token::Eof {
2397                return;
2398            } else {
2399                self.bump();
2400            }
2401        }
2402    }
2403
2404    pub(super) fn expected_expression_found(&self) -> Diag<'a> {
2405        let (span, msg) = match (&self.token.kind, self.subparser_name) {
2406            (&token::Eof, Some(origin)) => {
2407                let sp = self.prev_token.span.shrink_to_hi();
2408                (sp, format!("expected expression, found end of {origin}"))
2409            }
2410            _ => (
2411                self.token.span,
2412                format!("expected expression, found {}", super::token_descr(&self.token)),
2413            ),
2414        };
2415        let mut err = self.dcx().struct_span_err(span, msg);
2416        let sp = self.psess.source_map().start_point(self.token.span);
2417        if let Some(sp) = self.psess.ambiguous_block_expr_parse.borrow().get(&sp) {
2418            err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp));
2419        }
2420        err.span_label(span, "expected expression");
2421        err
2422    }
2423
2424    fn consume_tts(
2425        &mut self,
2426        mut acc: i64, // `i64` because malformed code can have more closing delims than opening.
2427        // Not using `FxHashMap` due to `token::TokenKind: !Eq + !Hash`.
2428        modifier: &[(token::TokenKind, i64)],
2429    ) {
2430        while acc > 0 {
2431            if let Some((_, val)) = modifier.iter().find(|(t, _)| self.token == *t) {
2432                acc += *val;
2433            }
2434            if self.token == token::Eof {
2435                break;
2436            }
2437            self.bump();
2438        }
2439    }
2440
2441    /// Replace duplicated recovered parameters with `_` pattern to avoid unnecessary errors.
2442    ///
2443    /// This is necessary because at this point we don't know whether we parsed a function with
2444    /// anonymous parameters or a function with names but no types. In order to minimize
2445    /// unnecessary errors, we assume the parameters are in the shape of `fn foo(a, b, c)` where
2446    /// the parameters are *names* (so we don't emit errors about not being able to find `b` in
2447    /// the local scope), but if we find the same name multiple times, like in `fn foo(i8, i8)`,
2448    /// we deduplicate them to not complain about duplicated parameter names.
2449    pub(super) fn deduplicate_recovered_params_names(&self, fn_inputs: &mut ThinVec<Param>) {
2450        let mut seen_inputs = FxHashSet::default();
2451        for input in fn_inputs.iter_mut() {
2452            let opt_ident = if let (PatKind::Ident(_, ident, _), TyKind::Err(_)) =
2453                (&input.pat.kind, &input.ty.kind)
2454            {
2455                Some(*ident)
2456            } else {
2457                None
2458            };
2459            if let Some(ident) = opt_ident {
2460                if seen_inputs.contains(&ident) {
2461                    input.pat.kind = PatKind::Wild;
2462                }
2463                seen_inputs.insert(ident);
2464            }
2465        }
2466    }
2467
2468    /// Handle encountering a symbol in a generic argument list that is not a `,` or `>`. In this
2469    /// case, we emit an error and try to suggest enclosing a const argument in braces if it looks
2470    /// like the user has forgotten them.
2471    pub(super) fn handle_ambiguous_unbraced_const_arg(
2472        &mut self,
2473        args: &mut ThinVec<AngleBracketedArg>,
2474    ) -> PResult<'a, bool> {
2475        // If we haven't encountered a closing `>`, then the argument is malformed.
2476        // It's likely that the user has written a const expression without enclosing it
2477        // in braces, so we try to recover here.
2478        let arg = args.pop().unwrap();
2479        // FIXME: for some reason using `unexpected` or `expected_one_of_not_found` has
2480        // adverse side-effects to subsequent errors and seems to advance the parser.
2481        // We are causing this error here exclusively in case that a `const` expression
2482        // could be recovered from the current parser state, even if followed by more
2483        // arguments after a comma.
2484        let mut err = self.dcx().struct_span_err(
2485            self.token.span,
2486            format!("expected one of `,` or `>`, found {}", super::token_descr(&self.token)),
2487        );
2488        err.span_label(self.token.span, "expected one of `,` or `>`");
2489        match self.recover_const_arg(arg.span(), err) {
2490            Ok(arg) => {
2491                args.push(AngleBracketedArg::Arg(arg));
2492                if self.eat(exp!(Comma)) {
2493                    return Ok(true); // Continue
2494                }
2495            }
2496            Err(err) => {
2497                args.push(arg);
2498                // We will emit a more generic error later.
2499                err.delay_as_bug();
2500            }
2501        }
2502        Ok(false) // Don't continue.
2503    }
2504
2505    /// Attempt to parse a generic const argument that has not been enclosed in braces.
2506    /// There are a limited number of expressions that are permitted without being encoded
2507    /// in braces:
2508    /// - Literals.
2509    /// - Single-segment paths (i.e. standalone generic const parameters).
2510    /// All other expressions that can be parsed will emit an error suggesting the expression be
2511    /// wrapped in braces.
2512    pub(super) fn handle_unambiguous_unbraced_const_arg(&mut self) -> PResult<'a, Box<Expr>> {
2513        let start = self.token.span;
2514        let attrs = self.parse_outer_attributes()?;
2515        let (expr, _) =
2516            self.parse_expr_res(Restrictions::CONST_EXPR, attrs).map_err(|mut err| {
2517                err.span_label(
2518                    start.shrink_to_lo(),
2519                    "while parsing a const generic argument starting here",
2520                );
2521                err
2522            })?;
2523        if !self.expr_is_valid_const_arg(&expr) {
2524            self.dcx().emit_err(ConstGenericWithoutBraces {
2525                span: expr.span,
2526                sugg: ConstGenericWithoutBracesSugg {
2527                    left: expr.span.shrink_to_lo(),
2528                    right: expr.span.shrink_to_hi(),
2529                },
2530            });
2531        }
2532        Ok(expr)
2533    }
2534
2535    fn recover_const_param_decl(&mut self, ty_generics: Option<&Generics>) -> Option<GenericArg> {
2536        let snapshot = self.create_snapshot_for_diagnostic();
2537        let param = match self.parse_const_param(AttrVec::new()) {
2538            Ok(param) => param,
2539            Err(err) => {
2540                err.cancel();
2541                self.restore_snapshot(snapshot);
2542                return None;
2543            }
2544        };
2545
2546        let ident = param.ident.to_string();
2547        let sugg = match (ty_generics, self.psess.source_map().span_to_snippet(param.span())) {
2548            (Some(Generics { params, span: impl_generics, .. }), Ok(snippet)) => {
2549                Some(match &params[..] {
2550                    [] => UnexpectedConstParamDeclarationSugg::AddParam {
2551                        impl_generics: *impl_generics,
2552                        incorrect_decl: param.span(),
2553                        snippet,
2554                        ident,
2555                    },
2556                    [.., generic] => UnexpectedConstParamDeclarationSugg::AppendParam {
2557                        impl_generics_end: generic.span().shrink_to_hi(),
2558                        incorrect_decl: param.span(),
2559                        snippet,
2560                        ident,
2561                    },
2562                })
2563            }
2564            _ => None,
2565        };
2566        let guar =
2567            self.dcx().emit_err(UnexpectedConstParamDeclaration { span: param.span(), sugg });
2568
2569        let value = self.mk_expr_err(param.span(), guar);
2570        Some(GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value }))
2571    }
2572
2573    pub(super) fn recover_const_param_declaration(
2574        &mut self,
2575        ty_generics: Option<&Generics>,
2576    ) -> PResult<'a, Option<GenericArg>> {
2577        // We have to check for a few different cases.
2578        if let Some(arg) = self.recover_const_param_decl(ty_generics) {
2579            return Ok(Some(arg));
2580        }
2581
2582        // We haven't consumed `const` yet.
2583        let start = self.token.span;
2584        self.bump(); // `const`
2585
2586        // Detect and recover from the old, pre-RFC2000 syntax for const generics.
2587        let mut err = UnexpectedConstInGenericParam { span: start, to_remove: None };
2588        if self.check_const_arg() {
2589            err.to_remove = Some(start.until(self.token.span));
2590            self.dcx().emit_err(err);
2591            Ok(Some(GenericArg::Const(self.parse_const_arg()?)))
2592        } else {
2593            let after_kw_const = self.token.span;
2594            self.recover_const_arg(after_kw_const, self.dcx().create_err(err)).map(Some)
2595        }
2596    }
2597
2598    /// Try to recover from possible generic const argument without `{` and `}`.
2599    ///
2600    /// When encountering code like `foo::< bar + 3 >` or `foo::< bar - baz >` we suggest
2601    /// `foo::<{ bar + 3 }>` and `foo::<{ bar - baz }>`, respectively. We only provide a suggestion
2602    /// if we think that the resulting expression would be well formed.
2603    pub(super) fn recover_const_arg(
2604        &mut self,
2605        start: Span,
2606        mut err: Diag<'a>,
2607    ) -> PResult<'a, GenericArg> {
2608        let is_op_or_dot = AssocOp::from_token(&self.token)
2609            .and_then(|op| {
2610                if let AssocOp::Binary(
2611                    BinOpKind::Gt
2612                    | BinOpKind::Lt
2613                    | BinOpKind::Shr
2614                    | BinOpKind::Ge
2615                )
2616                // Don't recover from `foo::<bar = baz>`, because this could be an attempt to
2617                // assign a value to a defaulted generic parameter.
2618                | AssocOp::Assign
2619                | AssocOp::AssignOp(_) = op
2620                {
2621                    None
2622                } else {
2623                    Some(op)
2624                }
2625            })
2626            .is_some()
2627            || self.token == TokenKind::Dot;
2628        // This will be true when a trait object type `Foo +` or a path which was a `const fn` with
2629        // type params has been parsed.
2630        let was_op = matches!(self.prev_token.kind, token::Plus | token::Shr | token::Gt);
2631        if !is_op_or_dot && !was_op {
2632            // We perform these checks and early return to avoid taking a snapshot unnecessarily.
2633            return Err(err);
2634        }
2635        let snapshot = self.create_snapshot_for_diagnostic();
2636        if is_op_or_dot {
2637            self.bump();
2638        }
2639        match (|| {
2640            let attrs = self.parse_outer_attributes()?;
2641            self.parse_expr_res(Restrictions::CONST_EXPR, attrs)
2642        })() {
2643            Ok((expr, _)) => {
2644                // Find a mistake like `MyTrait<Assoc == S::Assoc>`.
2645                if snapshot.token == token::EqEq {
2646                    err.span_suggestion(
2647                        snapshot.token.span,
2648                        "if you meant to use an associated type binding, replace `==` with `=`",
2649                        "=",
2650                        Applicability::MaybeIncorrect,
2651                    );
2652                    let guar = err.emit();
2653                    let value = self.mk_expr_err(start.to(expr.span), guar);
2654                    return Ok(GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value }));
2655                } else if snapshot.token == token::Colon
2656                    && expr.span.lo() == snapshot.token.span.hi()
2657                    && matches!(expr.kind, ExprKind::Path(..))
2658                {
2659                    // Find a mistake like "foo::var:A".
2660                    err.span_suggestion(
2661                        snapshot.token.span,
2662                        "write a path separator here",
2663                        "::",
2664                        Applicability::MaybeIncorrect,
2665                    );
2666                    let guar = err.emit();
2667                    return Ok(GenericArg::Type(
2668                        self.mk_ty(start.to(expr.span), TyKind::Err(guar)),
2669                    ));
2670                } else if self.token == token::Comma || self.token.kind.should_end_const_arg() {
2671                    // Avoid the following output by checking that we consumed a full const arg:
2672                    // help: expressions must be enclosed in braces to be used as const generic
2673                    //       arguments
2674                    //    |
2675                    // LL |     let sr: Vec<{ (u32, _, _) = vec![] };
2676                    //    |                 ^                      ^
2677                    return Ok(self.dummy_const_arg_needs_braces(err, start.to(expr.span)));
2678                }
2679            }
2680            Err(err) => {
2681                err.cancel();
2682            }
2683        }
2684        self.restore_snapshot(snapshot);
2685        Err(err)
2686    }
2687
2688    /// Try to recover from an unbraced const argument whose first token [could begin a type][ty].
2689    ///
2690    /// [ty]: token::Token::can_begin_type
2691    pub(crate) fn recover_unbraced_const_arg_that_can_begin_ty(
2692        &mut self,
2693        mut snapshot: SnapshotParser<'a>,
2694    ) -> Option<Box<ast::Expr>> {
2695        match (|| {
2696            let attrs = self.parse_outer_attributes()?;
2697            snapshot.parse_expr_res(Restrictions::CONST_EXPR, attrs)
2698        })() {
2699            // Since we don't know the exact reason why we failed to parse the type or the
2700            // expression, employ a simple heuristic to weed out some pathological cases.
2701            Ok((expr, _)) if let token::Comma | token::Gt = snapshot.token.kind => {
2702                self.restore_snapshot(snapshot);
2703                Some(expr)
2704            }
2705            Ok(_) => None,
2706            Err(err) => {
2707                err.cancel();
2708                None
2709            }
2710        }
2711    }
2712
2713    /// Creates a dummy const argument, and reports that the expression must be enclosed in braces
2714    pub(super) fn dummy_const_arg_needs_braces(&self, mut err: Diag<'a>, span: Span) -> GenericArg {
2715        err.multipart_suggestion(
2716            "expressions must be enclosed in braces to be used as const generic \
2717             arguments",
2718            vec![(span.shrink_to_lo(), "{ ".to_string()), (span.shrink_to_hi(), " }".to_string())],
2719            Applicability::MaybeIncorrect,
2720        );
2721        let guar = err.emit();
2722        let value = self.mk_expr_err(span, guar);
2723        GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value })
2724    }
2725
2726    /// Some special error handling for the "top-level" patterns in a match arm,
2727    /// `for` loop, `let`, &c. (in contrast to subpatterns within such).
2728    pub(crate) fn maybe_recover_colon_colon_in_pat_typo(
2729        &mut self,
2730        mut first_pat: Box<Pat>,
2731        expected: Option<Expected>,
2732    ) -> Box<Pat> {
2733        if token::Colon != self.token.kind {
2734            return first_pat;
2735        }
2736        if !matches!(first_pat.kind, PatKind::Ident(_, _, None) | PatKind::Path(..))
2737            || !self.look_ahead(1, |token| token.is_non_reserved_ident())
2738        {
2739            let mut snapshot_type = self.create_snapshot_for_diagnostic();
2740            snapshot_type.bump(); // `:`
2741            match snapshot_type.parse_ty() {
2742                Err(inner_err) => {
2743                    inner_err.cancel();
2744                }
2745                Ok(ty) => {
2746                    let Err(mut err) = self.expected_one_of_not_found(&[], &[]) else {
2747                        return first_pat;
2748                    };
2749                    err.span_label(ty.span, "specifying the type of a pattern isn't supported");
2750                    self.restore_snapshot(snapshot_type);
2751                    let span = first_pat.span.to(ty.span);
2752                    first_pat = self.mk_pat(span, PatKind::Wild);
2753                    err.emit();
2754                }
2755            }
2756            return first_pat;
2757        }
2758        // The pattern looks like it might be a path with a `::` -> `:` typo:
2759        // `match foo { bar:baz => {} }`
2760        let colon_span = self.token.span;
2761        // We only emit "unexpected `:`" error here if we can successfully parse the
2762        // whole pattern correctly in that case.
2763        let mut snapshot_pat = self.create_snapshot_for_diagnostic();
2764        let mut snapshot_type = self.create_snapshot_for_diagnostic();
2765
2766        // Create error for "unexpected `:`".
2767        match self.expected_one_of_not_found(&[], &[]) {
2768            Err(mut err) => {
2769                // Skip the `:`.
2770                snapshot_pat.bump();
2771                snapshot_type.bump();
2772                match snapshot_pat.parse_pat_no_top_alt(expected, None) {
2773                    Err(inner_err) => {
2774                        inner_err.cancel();
2775                    }
2776                    Ok(mut pat) => {
2777                        // We've parsed the rest of the pattern.
2778                        let new_span = first_pat.span.to(pat.span);
2779                        let mut show_sugg = false;
2780                        // Try to construct a recovered pattern.
2781                        match &mut pat.kind {
2782                            PatKind::Struct(qself @ None, path, ..)
2783                            | PatKind::TupleStruct(qself @ None, path, _)
2784                            | PatKind::Path(qself @ None, path) => match &first_pat.kind {
2785                                PatKind::Ident(_, ident, _) => {
2786                                    path.segments.insert(0, PathSegment::from_ident(*ident));
2787                                    path.span = new_span;
2788                                    show_sugg = true;
2789                                    first_pat = pat;
2790                                }
2791                                PatKind::Path(old_qself, old_path) => {
2792                                    path.segments = old_path
2793                                        .segments
2794                                        .iter()
2795                                        .cloned()
2796                                        .chain(take(&mut path.segments))
2797                                        .collect();
2798                                    path.span = new_span;
2799                                    *qself = old_qself.clone();
2800                                    first_pat = pat;
2801                                    show_sugg = true;
2802                                }
2803                                _ => {}
2804                            },
2805                            PatKind::Ident(BindingMode::NONE, ident, None) => {
2806                                match &first_pat.kind {
2807                                    PatKind::Ident(_, old_ident, _) => {
2808                                        let path = PatKind::Path(
2809                                            None,
2810                                            Path {
2811                                                span: new_span,
2812                                                segments: thin_vec![
2813                                                    PathSegment::from_ident(*old_ident),
2814                                                    PathSegment::from_ident(*ident),
2815                                                ],
2816                                                tokens: None,
2817                                            },
2818                                        );
2819                                        first_pat = self.mk_pat(new_span, path);
2820                                        show_sugg = true;
2821                                    }
2822                                    PatKind::Path(old_qself, old_path) => {
2823                                        let mut segments = old_path.segments.clone();
2824                                        segments.push(PathSegment::from_ident(*ident));
2825                                        let path = PatKind::Path(
2826                                            old_qself.clone(),
2827                                            Path { span: new_span, segments, tokens: None },
2828                                        );
2829                                        first_pat = self.mk_pat(new_span, path);
2830                                        show_sugg = true;
2831                                    }
2832                                    _ => {}
2833                                }
2834                            }
2835                            _ => {}
2836                        }
2837                        if show_sugg {
2838                            err.span_suggestion_verbose(
2839                                colon_span.until(self.look_ahead(1, |t| t.span)),
2840                                "maybe write a path separator here",
2841                                "::",
2842                                Applicability::MaybeIncorrect,
2843                            );
2844                        } else {
2845                            first_pat = self.mk_pat(new_span, PatKind::Wild);
2846                        }
2847                        self.restore_snapshot(snapshot_pat);
2848                    }
2849                }
2850                match snapshot_type.parse_ty() {
2851                    Err(inner_err) => {
2852                        inner_err.cancel();
2853                    }
2854                    Ok(ty) => {
2855                        err.span_label(ty.span, "specifying the type of a pattern isn't supported");
2856                        self.restore_snapshot(snapshot_type);
2857                        let new_span = first_pat.span.to(ty.span);
2858                        first_pat = self.mk_pat(new_span, PatKind::Wild);
2859                    }
2860                }
2861                err.emit();
2862            }
2863            _ => {
2864                // Carry on as if we had not done anything. This should be unreachable.
2865            }
2866        };
2867        first_pat
2868    }
2869
2870    /// If `loop_header` is `Some` and an unexpected block label is encountered,
2871    /// it is suggested to be moved just before `loop_header`, else it is suggested to be removed.
2872    pub(crate) fn maybe_recover_unexpected_block_label(
2873        &mut self,
2874        loop_header: Option<Span>,
2875    ) -> bool {
2876        // Check for `'a : {`
2877        if !(self.check_lifetime()
2878            && self.look_ahead(1, |t| *t == token::Colon)
2879            && self.look_ahead(2, |t| *t == token::OpenBrace))
2880        {
2881            return false;
2882        }
2883        let label = self.eat_label().expect("just checked if a label exists");
2884        self.bump(); // eat `:`
2885        let span = label.ident.span.to(self.prev_token.span);
2886        let mut diag = self
2887            .dcx()
2888            .struct_span_err(span, "block label not supported here")
2889            .with_span_label(span, "not supported here");
2890        if let Some(loop_header) = loop_header {
2891            diag.multipart_suggestion(
2892                "if you meant to label the loop, move this label before the loop",
2893                vec![
2894                    (label.ident.span.until(self.token.span), String::from("")),
2895                    (loop_header.shrink_to_lo(), format!("{}: ", label.ident)),
2896                ],
2897                Applicability::MachineApplicable,
2898            );
2899        } else {
2900            diag.tool_only_span_suggestion(
2901                label.ident.span.until(self.token.span),
2902                "remove this block label",
2903                "",
2904                Applicability::MachineApplicable,
2905            );
2906        }
2907        diag.emit();
2908        true
2909    }
2910
2911    /// Some special error handling for the "top-level" patterns in a match arm,
2912    /// `for` loop, `let`, &c. (in contrast to subpatterns within such).
2913    pub(crate) fn maybe_recover_unexpected_comma(
2914        &mut self,
2915        lo: Span,
2916        rt: CommaRecoveryMode,
2917    ) -> PResult<'a, ()> {
2918        if self.token != token::Comma {
2919            return Ok(());
2920        }
2921
2922        // An unexpected comma after a top-level pattern is a clue that the
2923        // user (perhaps more accustomed to some other language) forgot the
2924        // parentheses in what should have been a tuple pattern; return a
2925        // suggestion-enhanced error here rather than choking on the comma later.
2926        let comma_span = self.token.span;
2927        self.bump();
2928        if let Err(err) = self.skip_pat_list() {
2929            // We didn't expect this to work anyway; we just wanted to advance to the
2930            // end of the comma-sequence so we know the span to suggest parenthesizing.
2931            err.cancel();
2932        }
2933        let seq_span = lo.to(self.prev_token.span);
2934        let mut err = self.dcx().struct_span_err(comma_span, "unexpected `,` in pattern");
2935        if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
2936            err.multipart_suggestion(
2937                format!(
2938                    "try adding parentheses to match on a tuple{}",
2939                    if let CommaRecoveryMode::LikelyTuple = rt { "" } else { "..." },
2940                ),
2941                vec![
2942                    (seq_span.shrink_to_lo(), "(".to_string()),
2943                    (seq_span.shrink_to_hi(), ")".to_string()),
2944                ],
2945                Applicability::MachineApplicable,
2946            );
2947            if let CommaRecoveryMode::EitherTupleOrPipe = rt {
2948                err.span_suggestion(
2949                    seq_span,
2950                    "...or a vertical bar to match on multiple alternatives",
2951                    seq_snippet.replace(',', " |"),
2952                    Applicability::MachineApplicable,
2953                );
2954            }
2955        }
2956        Err(err)
2957    }
2958
2959    pub(crate) fn maybe_recover_bounds_doubled_colon(&mut self, ty: &Ty) -> PResult<'a, ()> {
2960        let TyKind::Path(qself, path) = &ty.kind else { return Ok(()) };
2961        let qself_position = qself.as_ref().map(|qself| qself.position);
2962        for (i, segments) in path.segments.windows(2).enumerate() {
2963            if qself_position.is_some_and(|pos| i < pos) {
2964                continue;
2965            }
2966            if let [a, b] = segments {
2967                let (a_span, b_span) = (a.span(), b.span());
2968                let between_span = a_span.shrink_to_hi().to(b_span.shrink_to_lo());
2969                if self.span_to_snippet(between_span).as_deref() == Ok(":: ") {
2970                    return Err(self.dcx().create_err(DoubleColonInBound {
2971                        span: path.span.shrink_to_hi(),
2972                        between: between_span,
2973                    }));
2974                }
2975            }
2976        }
2977        Ok(())
2978    }
2979
2980    /// Check for exclusive ranges written as `..<`
2981    pub(crate) fn maybe_err_dotdotlt_syntax(&self, maybe_lt: Token, mut err: Diag<'a>) -> Diag<'a> {
2982        if maybe_lt == token::Lt
2983            && (self.expected_token_types.contains(TokenType::Gt)
2984                || matches!(self.token.kind, token::Literal(..)))
2985        {
2986            err.span_suggestion(
2987                maybe_lt.span,
2988                "remove the `<` to write an exclusive range",
2989                "",
2990                Applicability::MachineApplicable,
2991            );
2992        }
2993        err
2994    }
2995
2996    /// This checks if this is a conflict marker, depending of the parameter passed.
2997    ///
2998    /// * `<<<<<<<`
2999    /// * `|||||||`
3000    /// * `=======`
3001    /// * `>>>>>>>`
3002    ///
3003    pub(super) fn is_vcs_conflict_marker(
3004        &mut self,
3005        long_kind: &TokenKind,
3006        short_kind: &TokenKind,
3007    ) -> bool {
3008        (0..3).all(|i| self.look_ahead(i, |tok| tok == long_kind))
3009            && self.look_ahead(3, |tok| tok == short_kind)
3010    }
3011
3012    fn conflict_marker(&mut self, long_kind: &TokenKind, short_kind: &TokenKind) -> Option<Span> {
3013        if self.is_vcs_conflict_marker(long_kind, short_kind) {
3014            let lo = self.token.span;
3015            for _ in 0..4 {
3016                self.bump();
3017            }
3018            return Some(lo.to(self.prev_token.span));
3019        }
3020        None
3021    }
3022
3023    pub(super) fn recover_vcs_conflict_marker(&mut self) {
3024        // <<<<<<<
3025        let Some(start) = self.conflict_marker(&TokenKind::Shl, &TokenKind::Lt) else {
3026            return;
3027        };
3028        let mut spans = Vec::with_capacity(3);
3029        spans.push(start);
3030        // |||||||
3031        let mut middlediff3 = None;
3032        // =======
3033        let mut middle = None;
3034        // >>>>>>>
3035        let mut end = None;
3036        loop {
3037            if self.token == TokenKind::Eof {
3038                break;
3039            }
3040            if let Some(span) = self.conflict_marker(&TokenKind::OrOr, &TokenKind::Or) {
3041                middlediff3 = Some(span);
3042            }
3043            if let Some(span) = self.conflict_marker(&TokenKind::EqEq, &TokenKind::Eq) {
3044                middle = Some(span);
3045            }
3046            if let Some(span) = self.conflict_marker(&TokenKind::Shr, &TokenKind::Gt) {
3047                spans.push(span);
3048                end = Some(span);
3049                break;
3050            }
3051            self.bump();
3052        }
3053
3054        let mut err = self.dcx().struct_span_fatal(spans, "encountered diff marker");
3055        match middlediff3 {
3056            // We're using diff3
3057            Some(middlediff3) => {
3058                err.span_label(
3059                    start,
3060                    "between this marker and `|||||||` is the code that we're merging into",
3061                );
3062                err.span_label(middlediff3, "between this marker and `=======` is the base code (what the two refs diverged from)");
3063            }
3064            None => {
3065                err.span_label(
3066                    start,
3067                    "between this marker and `=======` is the code that we're merging into",
3068                );
3069            }
3070        };
3071
3072        if let Some(middle) = middle {
3073            err.span_label(middle, "between this marker and `>>>>>>>` is the incoming code");
3074        }
3075        if let Some(end) = end {
3076            err.span_label(end, "this marker concludes the conflict region");
3077        }
3078        err.note(
3079            "conflict markers indicate that a merge was started but could not be completed due \
3080             to merge conflicts\n\
3081             to resolve a conflict, keep only the code you want and then delete the lines \
3082             containing conflict markers",
3083        );
3084        err.help(
3085            "if you're having merge conflicts after pulling new code:\n\
3086             the top section is the code you already had and the bottom section is the remote code\n\
3087             if you're in the middle of a rebase:\n\
3088             the top section is the code being rebased onto and the bottom section is the code \
3089             coming from the current commit being rebased",
3090        );
3091
3092        err.note(
3093            "for an explanation on these markers from the `git` documentation:\n\
3094             visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>",
3095        );
3096
3097        err.emit();
3098    }
3099
3100    /// Parse and throw away a parenthesized comma separated
3101    /// sequence of patterns until `)` is reached.
3102    fn skip_pat_list(&mut self) -> PResult<'a, ()> {
3103        while !self.check(exp!(CloseParen)) {
3104            self.parse_pat_no_top_alt(None, None)?;
3105            if !self.eat(exp!(Comma)) {
3106                return Ok(());
3107            }
3108        }
3109        Ok(())
3110    }
3111}