rustc_parse/parser/
diagnostics.rs

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