1use std::mem::take;
2use std::ops::{Deref, DerefMut};
3
4use ast::token::IdentIsRaw;
5use rustc_ast as ast;
6use rustc_ast::token::{self, Lit, LitKind, Token, TokenKind};
7use rustc_ast::util::parser::AssocOp;
8use rustc_ast::{
9 AngleBracketedArg, AngleBracketedArgs, AnonConst, AttrVec, BinOpKind, BindingMode, Block,
10 BlockCheckMode, Expr, ExprKind, GenericArg, Generics, Item, ItemKind, Param, Pat, PatKind,
11 Path, PathSegment, QSelf, Recovered, Ty, TyKind,
12};
13use rustc_ast_pretty::pprust;
14use rustc_data_structures::fx::FxHashSet;
15use rustc_errors::{
16 Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, PResult, Subdiagnostic, Suggestions,
17 pluralize,
18};
19use rustc_session::errors::ExprParenthesesNeeded;
20use rustc_span::edit_distance::find_best_match_for_name;
21use rustc_span::source_map::Spanned;
22use rustc_span::symbol::used_keywords;
23use rustc_span::{BytePos, DUMMY_SP, Ident, Span, SpanSnippetError, Symbol, kw, sym};
24use thin_vec::{ThinVec, thin_vec};
25use tracing::{debug, trace};
26
27use super::pat::Expected;
28use super::{
29 BlockMode, CommaRecoveryMode, ExpTokenPair, Parser, PathStyle, Restrictions, SemiColonMode,
30 SeqSep, TokenType,
31};
32use crate::errors::{
33 AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AsyncUseBlockIn2015, AttributeOnParamType,
34 AwaitSuggestion, BadQPathStage2, BadTypePlus, BadTypePlusSub, ColonAsSemi,
35 ComparisonOperatorsCannotBeChained, ComparisonOperatorsCannotBeChainedSugg,
36 ConstGenericWithoutBraces, ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything,
37 DocCommentOnParamType, DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
38 GenericParamsWithoutAngleBrackets, GenericParamsWithoutAngleBracketsSugg,
39 HelpIdentifierStartsWithNumber, HelpUseLatestEdition, InInTypo, IncorrectAwait,
40 IncorrectSemicolon, IncorrectUseOfAwait, IncorrectUseOfUse, PatternMethodParamWithoutBody,
41 QuestionMarkInType, QuestionMarkInTypeSugg, SelfParamNotFirst, StructLiteralBodyWithoutPath,
42 StructLiteralBodyWithoutPathSugg, SuggAddMissingLetStmt, SuggEscapeIdentifier, SuggRemoveComma,
43 TernaryOperator, TernaryOperatorSuggestion, UnexpectedConstInGenericParam,
44 UnexpectedConstParamDeclaration, UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets,
45 UseEqInstead, WrapType,
46};
47use crate::parser::attr::InnerAttrPolicy;
48use crate::{exp, fluent_generated as fluent};
49
50pub(super) fn dummy_arg(ident: Ident, guar: ErrorGuaranteed) -> Param {
52 let pat = Box::new(Pat {
53 id: ast::DUMMY_NODE_ID,
54 kind: PatKind::Ident(BindingMode::NONE, ident, None),
55 span: ident.span,
56 tokens: None,
57 });
58 let ty = Ty { kind: TyKind::Err(guar), span: ident.span, id: ast::DUMMY_NODE_ID, tokens: None };
59 Param {
60 attrs: AttrVec::default(),
61 id: ast::DUMMY_NODE_ID,
62 pat,
63 span: ident.span,
64 ty: Box::new(ty),
65 is_placeholder: false,
66 }
67}
68
69pub(super) trait RecoverQPath: Sized + 'static {
70 const PATH_STYLE: PathStyle = PathStyle::Expr;
71 fn to_ty(&self) -> Option<Box<Ty>>;
72 fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self;
73}
74
75impl RecoverQPath for Ty {
76 const PATH_STYLE: PathStyle = PathStyle::Type;
77 fn to_ty(&self) -> Option<Box<Ty>> {
78 Some(Box::new(self.clone()))
79 }
80 fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {
81 Self {
82 span: path.span,
83 kind: TyKind::Path(qself, path),
84 id: ast::DUMMY_NODE_ID,
85 tokens: None,
86 }
87 }
88}
89
90impl RecoverQPath for Pat {
91 const PATH_STYLE: PathStyle = PathStyle::Pat;
92 fn to_ty(&self) -> Option<Box<Ty>> {
93 self.to_ty()
94 }
95 fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {
96 Self {
97 span: path.span,
98 kind: PatKind::Path(qself, path),
99 id: ast::DUMMY_NODE_ID,
100 tokens: None,
101 }
102 }
103}
104
105impl RecoverQPath for Expr {
106 fn to_ty(&self) -> Option<Box<Ty>> {
107 self.to_ty()
108 }
109 fn recovered(qself: Option<Box<QSelf>>, path: ast::Path) -> Self {
110 Self {
111 span: path.span,
112 kind: ExprKind::Path(qself, path),
113 attrs: AttrVec::new(),
114 id: ast::DUMMY_NODE_ID,
115 tokens: None,
116 }
117 }
118}
119
120pub(crate) enum ConsumeClosingDelim {
122 Yes,
123 No,
124}
125
126#[derive(Clone, Copy)]
127pub enum AttemptLocalParseRecovery {
128 Yes,
129 No,
130}
131
132impl AttemptLocalParseRecovery {
133 pub(super) fn yes(&self) -> bool {
134 match self {
135 AttemptLocalParseRecovery::Yes => true,
136 AttemptLocalParseRecovery::No => false,
137 }
138 }
139
140 pub(super) fn no(&self) -> bool {
141 match self {
142 AttemptLocalParseRecovery::Yes => false,
143 AttemptLocalParseRecovery::No => true,
144 }
145 }
146}
147
148#[derive(Debug, Copy, Clone)]
151struct IncDecRecovery {
152 standalone: IsStandalone,
154 op: IncOrDec,
156 fixity: UnaryFixity,
158}
159
160#[derive(Debug, Copy, Clone)]
162enum IsStandalone {
163 Standalone,
165 Subexpr,
167}
168
169#[derive(Debug, Copy, Clone, PartialEq, Eq)]
170enum IncOrDec {
171 Inc,
172 Dec,
173}
174
175#[derive(Debug, Copy, Clone, PartialEq, Eq)]
176enum UnaryFixity {
177 Pre,
178 Post,
179}
180
181impl IncOrDec {
182 fn chr(&self) -> char {
183 match self {
184 Self::Inc => '+',
185 Self::Dec => '-',
186 }
187 }
188
189 fn name(&self) -> &'static str {
190 match self {
191 Self::Inc => "increment",
192 Self::Dec => "decrement",
193 }
194 }
195}
196
197impl std::fmt::Display for UnaryFixity {
198 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
199 match self {
200 Self::Pre => write!(f, "prefix"),
201 Self::Post => write!(f, "postfix"),
202 }
203 }
204}
205
206#[derive(Debug, rustc_macros::Subdiagnostic)]
207#[suggestion(
208 parse_misspelled_kw,
209 applicability = "machine-applicable",
210 code = "{similar_kw}",
211 style = "verbose"
212)]
213struct MisspelledKw {
214 similar_kw: String,
215 #[primary_span]
216 span: Span,
217 is_incorrect_case: bool,
218}
219
220fn find_similar_kw(lookup: Ident, candidates: &[Symbol]) -> Option<MisspelledKw> {
222 let lowercase = lookup.name.as_str().to_lowercase();
223 let lowercase_sym = Symbol::intern(&lowercase);
224 if candidates.contains(&lowercase_sym) {
225 Some(MisspelledKw { similar_kw: lowercase, span: lookup.span, is_incorrect_case: true })
226 } else if let Some(similar_sym) = find_best_match_for_name(candidates, lookup.name, None) {
227 Some(MisspelledKw {
228 similar_kw: similar_sym.to_string(),
229 span: lookup.span,
230 is_incorrect_case: false,
231 })
232 } else {
233 None
234 }
235}
236
237struct MultiSugg {
238 msg: String,
239 patches: Vec<(Span, String)>,
240 applicability: Applicability,
241}
242
243impl MultiSugg {
244 fn emit(self, err: &mut Diag<'_>) {
245 err.multipart_suggestion(self.msg, self.patches, self.applicability);
246 }
247
248 fn emit_verbose(self, err: &mut Diag<'_>) {
249 err.multipart_suggestion_verbose(self.msg, self.patches, self.applicability);
250 }
251}
252
253pub struct SnapshotParser<'a> {
257 parser: Parser<'a>,
258}
259
260impl<'a> Deref for SnapshotParser<'a> {
261 type Target = Parser<'a>;
262
263 fn deref(&self) -> &Self::Target {
264 &self.parser
265 }
266}
267
268impl<'a> DerefMut for SnapshotParser<'a> {
269 fn deref_mut(&mut self) -> &mut Self::Target {
270 &mut self.parser
271 }
272}
273
274impl<'a> Parser<'a> {
275 pub fn dcx(&self) -> DiagCtxtHandle<'a> {
276 self.psess.dcx()
277 }
278
279 pub(super) fn restore_snapshot(&mut self, snapshot: SnapshotParser<'a>) {
281 *self = snapshot.parser;
282 }
283
284 pub fn create_snapshot_for_diagnostic(&self) -> SnapshotParser<'a> {
286 let snapshot = self.clone();
287 SnapshotParser { parser: snapshot }
288 }
289
290 pub(super) fn span_to_snippet(&self, span: Span) -> Result<String, SpanSnippetError> {
291 self.psess.source_map().span_to_snippet(span)
292 }
293
294 pub(super) fn expected_ident_found(
298 &mut self,
299 recover: bool,
300 ) -> PResult<'a, (Ident, IdentIsRaw)> {
301 let valid_follow = &[
302 TokenKind::Eq,
303 TokenKind::Colon,
304 TokenKind::Comma,
305 TokenKind::Semi,
306 TokenKind::PathSep,
307 TokenKind::OpenBrace,
308 TokenKind::OpenParen,
309 TokenKind::CloseBrace,
310 TokenKind::CloseParen,
311 ];
312 if let TokenKind::DocComment(..) = self.prev_token.kind
313 && valid_follow.contains(&self.token.kind)
314 {
315 let err = self.dcx().create_err(DocCommentDoesNotDocumentAnything {
316 span: self.prev_token.span,
317 missing_comma: None,
318 });
319 return Err(err);
320 }
321
322 let mut recovered_ident = None;
323 let bad_token = self.token;
326
327 let suggest_raw = if let Some((ident, IdentIsRaw::No)) = self.token.ident()
329 && ident.is_raw_guess()
330 && self.look_ahead(1, |t| valid_follow.contains(&t.kind))
331 {
332 recovered_ident = Some((ident, IdentIsRaw::Yes));
333
334 let ident_name = ident.name.to_string();
337
338 Some(SuggEscapeIdentifier { span: ident.span.shrink_to_lo(), ident_name })
339 } else {
340 None
341 };
342
343 let suggest_remove_comma =
344 if self.token == token::Comma && self.look_ahead(1, |t| t.is_ident()) {
345 if recover {
346 self.bump();
347 recovered_ident = self.ident_or_err(false).ok();
348 };
349
350 Some(SuggRemoveComma { span: bad_token.span })
351 } else {
352 None
353 };
354
355 let help_cannot_start_number = self.is_lit_bad_ident().map(|(len, valid_portion)| {
356 let (invalid, valid) = self.token.span.split_at(len as u32);
357
358 recovered_ident = Some((Ident::new(valid_portion, valid), IdentIsRaw::No));
359
360 HelpIdentifierStartsWithNumber { num_span: invalid }
361 });
362
363 let err = ExpectedIdentifier {
364 span: bad_token.span,
365 token: bad_token,
366 suggest_raw,
367 suggest_remove_comma,
368 help_cannot_start_number,
369 };
370 let mut err = self.dcx().create_err(err);
371
372 if self.token == token::Lt {
376 let valid_prev_keywords =
378 [kw::Fn, kw::Type, kw::Struct, kw::Enum, kw::Union, kw::Trait];
379
380 let maybe_keyword = self.prev_token;
386 if valid_prev_keywords.into_iter().any(|x| maybe_keyword.is_keyword(x)) {
387 match self.parse_generics() {
390 Ok(generic) => {
391 if let TokenKind::Ident(symbol, _) = maybe_keyword.kind {
392 let ident_name = symbol;
393 if !self.look_ahead(1, |t| *t == token::Lt)
399 && let Ok(snippet) =
400 self.psess.source_map().span_to_snippet(generic.span)
401 {
402 err.multipart_suggestion_verbose(
403 format!("place the generic parameter name after the {ident_name} name"),
404 vec![
405 (self.token.span.shrink_to_hi(), snippet),
406 (generic.span, String::new())
407 ],
408 Applicability::MaybeIncorrect,
409 );
410 } else {
411 err.help(format!(
412 "place the generic parameter name after the {ident_name} name"
413 ));
414 }
415 }
416 }
417 Err(err) => {
418 err.cancel();
422 }
423 }
424 }
425 }
426
427 if let Some(recovered_ident) = recovered_ident
428 && recover
429 {
430 err.emit();
431 Ok(recovered_ident)
432 } else {
433 Err(err)
434 }
435 }
436
437 pub(super) fn expected_ident_found_err(&mut self) -> Diag<'a> {
438 self.expected_ident_found(false).unwrap_err()
439 }
440
441 pub(super) fn is_lit_bad_ident(&mut self) -> Option<(usize, Symbol)> {
447 if let token::Literal(Lit {
451 kind: token::LitKind::Integer | token::LitKind::Float,
452 symbol,
453 suffix: Some(suffix), }) = self.token.kind
455 && rustc_ast::MetaItemLit::from_token(&self.token).is_none()
456 {
457 Some((symbol.as_str().len(), suffix))
458 } else {
459 None
460 }
461 }
462
463 pub(super) fn expected_one_of_not_found(
464 &mut self,
465 edible: &[ExpTokenPair<'_>],
466 inedible: &[ExpTokenPair<'_>],
467 ) -> PResult<'a, ErrorGuaranteed> {
468 debug!("expected_one_of_not_found(edible: {:?}, inedible: {:?})", edible, inedible);
469 fn tokens_to_string(tokens: &[TokenType]) -> String {
470 let mut i = tokens.iter();
471 let b = i.next().map_or_else(String::new, |t| t.to_string());
473 i.enumerate().fold(b, |mut b, (i, a)| {
474 if tokens.len() > 2 && i == tokens.len() - 2 {
475 b.push_str(", or ");
476 } else if tokens.len() == 2 && i == tokens.len() - 2 {
477 b.push_str(" or ");
478 } else {
479 b.push_str(", ");
480 }
481 b.push_str(&a.to_string());
482 b
483 })
484 }
485
486 for exp in edible.iter().chain(inedible.iter()) {
487 self.expected_token_types.insert(exp.token_type);
488 }
489 let mut expected: Vec<_> = self.expected_token_types.iter().collect();
490 expected.sort_by_cached_key(|x| x.to_string());
491 expected.dedup();
492
493 let sm = self.psess.source_map();
494
495 if expected.contains(&TokenType::Semi) {
497 if self.prev_token == token::Question
500 && let Err(e) = self.maybe_recover_from_ternary_operator(None)
501 {
502 return Err(e);
503 }
504
505 if self.token.span == DUMMY_SP || self.prev_token.span == DUMMY_SP {
506 } else if !sm.is_multiline(self.prev_token.span.until(self.token.span)) {
508 } else if [token::Comma, token::Colon].contains(&self.token.kind)
510 && self.prev_token == token::CloseParen
511 {
512 } else if self.look_ahead(1, |t| {
521 t == &token::CloseBrace || t.can_begin_expr() && *t != token::Colon
522 }) && [token::Comma, token::Colon].contains(&self.token.kind)
523 {
524 let guar = self.dcx().emit_err(ExpectedSemi {
531 span: self.token.span,
532 token: self.token,
533 unexpected_token_label: None,
534 sugg: ExpectedSemiSugg::ChangeToSemi(self.token.span),
535 });
536 self.bump();
537 return Ok(guar);
538 } else if self.look_ahead(0, |t| {
539 t == &token::CloseBrace
540 || ((t.can_begin_expr() || t.can_begin_item())
541 && t != &token::Semi
542 && t != &token::Pound)
543 || (sm.is_multiline(
545 self.prev_token.span.shrink_to_hi().until(self.token.span.shrink_to_lo()),
546 ) && t == &token::Pound)
547 }) && !expected.contains(&TokenType::Comma)
548 {
549 let span = self.prev_token.span.shrink_to_hi();
555 let guar = self.dcx().emit_err(ExpectedSemi {
556 span,
557 token: self.token,
558 unexpected_token_label: Some(self.token.span),
559 sugg: ExpectedSemiSugg::AddSemi(span),
560 });
561 return Ok(guar);
562 }
563 }
564
565 if self.token == TokenKind::EqEq
566 && self.prev_token.is_ident()
567 && expected.contains(&TokenType::Eq)
568 {
569 return Err(self.dcx().create_err(UseEqInstead { span: self.token.span }));
571 }
572
573 if (self.token.is_keyword(kw::Move) || self.token.is_keyword(kw::Use))
574 && self.prev_token.is_keyword(kw::Async)
575 {
576 let span = self.prev_token.span.to(self.token.span);
578 if self.token.is_keyword(kw::Move) {
579 return Err(self.dcx().create_err(AsyncMoveBlockIn2015 { span }));
580 } else {
581 return Err(self.dcx().create_err(AsyncUseBlockIn2015 { span }));
583 }
584 }
585
586 let expect = tokens_to_string(&expected);
587 let actual = super::token_descr(&self.token);
588 let (msg_exp, (label_sp, label_exp)) = if expected.len() > 1 {
589 let fmt = format!("expected one of {expect}, found {actual}");
590 let short_expect = if expected.len() > 6 {
591 format!("{} possible tokens", expected.len())
592 } else {
593 expect
594 };
595 (fmt, (self.prev_token.span.shrink_to_hi(), format!("expected one of {short_expect}")))
596 } else if expected.is_empty() {
597 (
598 format!("unexpected token: {actual}"),
599 (self.prev_token.span, "unexpected token after this".to_string()),
600 )
601 } else {
602 (
603 format!("expected {expect}, found {actual}"),
604 (self.prev_token.span.shrink_to_hi(), format!("expected {expect}")),
605 )
606 };
607 self.last_unexpected_token_span = Some(self.token.span);
608 let mut err = self.dcx().struct_span_err(self.token.span, msg_exp);
610
611 self.label_expected_raw_ref(&mut err);
612
613 if self.token == token::FatArrow
615 && expected.iter().any(|tok| matches!(tok, TokenType::Operator | TokenType::Le))
616 && !expected.iter().any(|tok| matches!(tok, TokenType::FatArrow | TokenType::Comma))
617 {
618 err.span_suggestion(
619 self.token.span,
620 "you might have meant to write a \"greater than or equal to\" comparison",
621 ">=",
622 Applicability::MaybeIncorrect,
623 );
624 }
625
626 if let TokenKind::Ident(symbol, _) = &self.prev_token.kind {
627 if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {
628 err.span_suggestion_short(
629 self.prev_token.span,
630 format!("write `fn` instead of `{symbol}` to declare a function"),
631 "fn",
632 Applicability::MachineApplicable,
633 );
634 }
635 }
636
637 if let TokenKind::Ident(prev, _) = &self.prev_token.kind
638 && let TokenKind::Ident(cur, _) = &self.token.kind
639 {
640 let concat = Symbol::intern(&format!("{prev}{cur}"));
641 let ident = Ident::new(concat, DUMMY_SP);
642 if ident.is_used_keyword() || ident.is_reserved() || ident.is_raw_guess() {
643 let concat_span = self.prev_token.span.to(self.token.span);
644 err.span_suggestion_verbose(
645 concat_span,
646 format!("consider removing the space to spell keyword `{concat}`"),
647 concat,
648 Applicability::MachineApplicable,
649 );
650 }
651 }
652
653 if ((self.prev_token == TokenKind::Ident(sym::c, IdentIsRaw::No)
661 && matches!(&self.token.kind, TokenKind::Literal(token::Lit { kind: token::Str, .. })))
662 || (self.prev_token == TokenKind::Ident(sym::cr, IdentIsRaw::No)
663 && matches!(
664 &self.token.kind,
665 TokenKind::Literal(token::Lit { kind: token::Str, .. }) | token::Pound
666 )))
667 && self.prev_token.span.hi() == self.token.span.lo()
668 && !self.token.span.at_least_rust_2021()
669 {
670 err.note("you may be trying to write a c-string literal");
671 err.note("c-string literals require Rust 2021 or later");
672 err.subdiagnostic(HelpUseLatestEdition::new());
673 }
674
675 if self.prev_token.is_ident_named(sym::public)
677 && (self.token.can_begin_item() || self.token == TokenKind::OpenParen)
678 {
679 err.span_suggestion_short(
680 self.prev_token.span,
681 "write `pub` instead of `public` to make the item public",
682 "pub",
683 Applicability::MachineApplicable,
684 );
685 }
686
687 if let token::DocComment(kind, style, _) = self.token.kind {
688 if !expected.contains(&TokenType::Comma) {
698 let pos = self.token.span.lo() + BytePos(2);
700 let span = self.token.span.with_lo(pos).with_hi(pos);
701 err.span_suggestion_verbose(
702 span,
703 format!(
704 "add a space before {} to write a regular comment",
705 match (kind, style) {
706 (token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`",
707 (token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`",
708 (token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`",
709 (token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`",
710 },
711 ),
712 " ".to_string(),
713 Applicability::MaybeIncorrect,
714 );
715 }
716 }
717
718 let sp = if self.token == token::Eof {
719 self.prev_token.span
721 } else {
722 label_sp
723 };
724
725 if self.check_too_many_raw_str_terminators(&mut err) {
726 if expected.contains(&TokenType::Semi) && self.eat(exp!(Semi)) {
727 let guar = err.emit();
728 return Ok(guar);
729 } else {
730 return Err(err);
731 }
732 }
733
734 if self.prev_token.span == DUMMY_SP {
735 err.span_label(self.token.span, label_exp);
738 } else if !sm.is_multiline(self.token.span.shrink_to_hi().until(sp.shrink_to_lo())) {
739 err.span_label(self.token.span, label_exp);
752 } else {
753 err.span_label(sp, label_exp);
754 err.span_label(self.token.span, "unexpected token");
755 }
756
757 if matches!(&err.suggestions, Suggestions::Enabled(list) if list.is_empty()) {
759 self.check_for_misspelled_kw(&mut err, &expected);
760 }
761 Err(err)
762 }
763
764 pub(super) fn label_expected_raw_ref(&mut self, err: &mut Diag<'_>) {
769 if self.prev_token.is_keyword(kw::Raw)
770 && self.expected_token_types.contains(TokenType::KwMut)
771 && self.expected_token_types.contains(TokenType::KwConst)
772 && self.token.can_begin_expr()
773 {
774 err.span_suggestions(
775 self.prev_token.span.shrink_to_hi(),
776 "`&raw` must be followed by `const` or `mut` to be a raw reference expression",
777 [" const".to_string(), " mut".to_string()],
778 Applicability::MaybeIncorrect,
779 );
780 }
781 }
782
783 fn check_for_misspelled_kw(&self, err: &mut Diag<'_>, expected: &[TokenType]) {
786 let Some((curr_ident, _)) = self.token.ident() else {
787 return;
788 };
789 let expected_token_types: &[TokenType] =
790 expected.len().checked_sub(10).map_or(&expected, |index| &expected[index..]);
791 let expected_keywords: Vec<Symbol> =
792 expected_token_types.iter().filter_map(|token| token.is_keyword()).collect();
793
794 if !expected_keywords.is_empty()
799 && !curr_ident.is_used_keyword()
800 && let Some(misspelled_kw) = find_similar_kw(curr_ident, &expected_keywords)
801 {
802 err.subdiagnostic(misspelled_kw);
803 err.seal_suggestions();
806 } else if let Some((prev_ident, _)) = self.prev_token.ident()
807 && !prev_ident.is_used_keyword()
808 {
809 let all_keywords = used_keywords(|| prev_ident.span.edition());
814
815 if let Some(misspelled_kw) = find_similar_kw(prev_ident, &all_keywords) {
820 err.subdiagnostic(misspelled_kw);
821 err.seal_suggestions();
824 }
825 }
826 }
827
828 pub(super) fn attr_on_non_tail_expr(&self, expr: &Expr) -> ErrorGuaranteed {
830 let span = self.prev_token.span.shrink_to_hi();
832 let mut err = self.dcx().create_err(ExpectedSemi {
833 span,
834 token: self.token,
835 unexpected_token_label: Some(self.token.span),
836 sugg: ExpectedSemiSugg::AddSemi(span),
837 });
838 let attr_span = match &expr.attrs[..] {
839 [] => unreachable!(),
840 [only] => only.span,
841 [first, rest @ ..] => {
842 for attr in rest {
843 err.span_label(attr.span, "");
844 }
845 first.span
846 }
847 };
848 err.span_label(
849 attr_span,
850 format!(
851 "only `;` terminated statements or tail expressions are allowed after {}",
852 if expr.attrs.len() == 1 { "this attribute" } else { "these attributes" },
853 ),
854 );
855 if self.token == token::Pound && self.look_ahead(1, |t| *t == token::OpenBracket) {
856 err.span_label(span, "expected `;` here");
862 err.multipart_suggestion(
863 "alternatively, consider surrounding the expression with a block",
864 vec![
865 (expr.span.shrink_to_lo(), "{ ".to_string()),
866 (expr.span.shrink_to_hi(), " }".to_string()),
867 ],
868 Applicability::MachineApplicable,
869 );
870
871 let mut snapshot = self.create_snapshot_for_diagnostic();
873 if let [attr] = &expr.attrs[..]
874 && let ast::AttrKind::Normal(attr_kind) = &attr.kind
875 && let [segment] = &attr_kind.item.path.segments[..]
876 && segment.ident.name == sym::cfg
877 && let Some(args_span) = attr_kind.item.args.span()
878 && let next_attr = match snapshot.parse_attribute(InnerAttrPolicy::Forbidden(None))
879 {
880 Ok(next_attr) => next_attr,
881 Err(inner_err) => {
882 inner_err.cancel();
883 return err.emit();
884 }
885 }
886 && let ast::AttrKind::Normal(next_attr_kind) = next_attr.kind
887 && let Some(next_attr_args_span) = next_attr_kind.item.args.span()
888 && let [next_segment] = &next_attr_kind.item.path.segments[..]
889 && segment.ident.name == sym::cfg
890 {
891 let next_expr = match snapshot.parse_expr() {
892 Ok(next_expr) => next_expr,
893 Err(inner_err) => {
894 inner_err.cancel();
895 return err.emit();
896 }
897 };
898 let margin = self.psess.source_map().span_to_margin(next_expr.span).unwrap_or(0);
905 let sugg = vec![
906 (attr.span.with_hi(segment.span().hi()), "if cfg!".to_string()),
907 (args_span.shrink_to_hi().with_hi(attr.span.hi()), " {".to_string()),
908 (expr.span.shrink_to_lo(), " ".to_string()),
909 (
910 next_attr.span.with_hi(next_segment.span().hi()),
911 "} else if cfg!".to_string(),
912 ),
913 (
914 next_attr_args_span.shrink_to_hi().with_hi(next_attr.span.hi()),
915 " {".to_string(),
916 ),
917 (next_expr.span.shrink_to_lo(), " ".to_string()),
918 (next_expr.span.shrink_to_hi(), format!("\n{}}}", " ".repeat(margin))),
919 ];
920 err.multipart_suggestion(
921 "it seems like you are trying to provide different expressions depending on \
922 `cfg`, consider using `if cfg!(..)`",
923 sugg,
924 Applicability::MachineApplicable,
925 );
926 }
927 }
928
929 err.emit()
930 }
931
932 fn check_too_many_raw_str_terminators(&mut self, err: &mut Diag<'_>) -> bool {
933 let sm = self.psess.source_map();
934 match (&self.prev_token.kind, &self.token.kind) {
935 (
936 TokenKind::Literal(Lit {
937 kind: LitKind::StrRaw(n_hashes) | LitKind::ByteStrRaw(n_hashes),
938 ..
939 }),
940 TokenKind::Pound,
941 ) if !sm.is_multiline(
942 self.prev_token.span.shrink_to_hi().until(self.token.span.shrink_to_lo()),
943 ) =>
944 {
945 let n_hashes: u8 = *n_hashes;
946 err.primary_message("too many `#` when terminating raw string");
947 let str_span = self.prev_token.span;
948 let mut span = self.token.span;
949 let mut count = 0;
950 while self.token == TokenKind::Pound
951 && !sm.is_multiline(span.shrink_to_hi().until(self.token.span.shrink_to_lo()))
952 {
953 span = span.with_hi(self.token.span.hi());
954 self.bump();
955 count += 1;
956 }
957 err.span(span);
958 err.span_suggestion(
959 span,
960 format!("remove the extra `#`{}", pluralize!(count)),
961 "",
962 Applicability::MachineApplicable,
963 );
964 err.span_label(
965 str_span,
966 format!("this raw string started with {n_hashes} `#`{}", pluralize!(n_hashes)),
967 );
968 true
969 }
970 _ => false,
971 }
972 }
973
974 pub(super) fn maybe_suggest_struct_literal(
975 &mut self,
976 lo: Span,
977 s: BlockCheckMode,
978 maybe_struct_name: token::Token,
979 ) -> Option<PResult<'a, Box<Block>>> {
980 if self.token.is_ident() && self.look_ahead(1, |t| t == &token::Colon) {
981 debug!(?maybe_struct_name, ?self.token);
986 let mut snapshot = self.create_snapshot_for_diagnostic();
987 let path = Path {
988 segments: ThinVec::new(),
989 span: self.prev_token.span.shrink_to_lo(),
990 tokens: None,
991 };
992 let struct_expr = snapshot.parse_expr_struct(None, path, false);
993 let block_tail = self.parse_block_tail(lo, s, AttemptLocalParseRecovery::No);
994 return Some(match (struct_expr, block_tail) {
995 (Ok(expr), Err(err)) => {
996 err.cancel();
1005 self.restore_snapshot(snapshot);
1006 let guar = self.dcx().emit_err(StructLiteralBodyWithoutPath {
1007 span: expr.span,
1008 sugg: StructLiteralBodyWithoutPathSugg {
1009 before: expr.span.shrink_to_lo(),
1010 after: expr.span.shrink_to_hi(),
1011 },
1012 });
1013 Ok(self.mk_block(
1014 thin_vec![self.mk_stmt_err(expr.span, guar)],
1015 s,
1016 lo.to(self.prev_token.span),
1017 ))
1018 }
1019 (Err(err), Ok(tail)) => {
1020 err.cancel();
1022 Ok(tail)
1023 }
1024 (Err(snapshot_err), Err(err)) => {
1025 snapshot_err.cancel();
1027 self.consume_block(exp!(OpenBrace), exp!(CloseBrace), ConsumeClosingDelim::Yes);
1028 Err(err)
1029 }
1030 (Ok(_), Ok(tail)) => Ok(tail),
1031 });
1032 }
1033 None
1034 }
1035
1036 pub(super) fn recover_closure_body(
1037 &mut self,
1038 mut err: Diag<'a>,
1039 before: token::Token,
1040 prev: token::Token,
1041 token: token::Token,
1042 lo: Span,
1043 decl_hi: Span,
1044 ) -> PResult<'a, Box<Expr>> {
1045 err.span_label(lo.to(decl_hi), "while parsing the body of this closure");
1046 let guar = match before.kind {
1047 token::OpenBrace if token.kind != token::OpenBrace => {
1048 err.multipart_suggestion(
1050 "you might have meant to open the body of the closure, instead of enclosing \
1051 the closure in a block",
1052 vec![
1053 (before.span, String::new()),
1054 (prev.span.shrink_to_hi(), " {".to_string()),
1055 ],
1056 Applicability::MaybeIncorrect,
1057 );
1058 let guar = err.emit();
1059 self.eat_to_tokens(&[exp!(CloseBrace)]);
1060 guar
1061 }
1062 token::OpenParen if token.kind != token::OpenBrace => {
1063 self.eat_to_tokens(&[exp!(CloseParen), exp!(Comma)]);
1066
1067 err.multipart_suggestion_verbose(
1068 "you might have meant to open the body of the closure",
1069 vec![
1070 (prev.span.shrink_to_hi(), " {".to_string()),
1071 (self.token.span.shrink_to_lo(), "}".to_string()),
1072 ],
1073 Applicability::MaybeIncorrect,
1074 );
1075 err.emit()
1076 }
1077 _ if token.kind != token::OpenBrace => {
1078 err.multipart_suggestion_verbose(
1081 "you might have meant to open the body of the closure",
1082 vec![(prev.span.shrink_to_hi(), " {".to_string())],
1083 Applicability::HasPlaceholders,
1084 );
1085 return Err(err);
1086 }
1087 _ => return Err(err),
1088 };
1089 Ok(self.mk_expr_err(lo.to(self.token.span), guar))
1090 }
1091
1092 pub(super) fn eat_to_tokens(&mut self, closes: &[ExpTokenPair<'_>]) {
1095 if let Err(err) = self
1096 .parse_seq_to_before_tokens(closes, &[], SeqSep::none(), |p| Ok(p.parse_token_tree()))
1097 {
1098 err.cancel();
1099 }
1100 }
1101
1102 pub(super) fn check_trailing_angle_brackets(
1113 &mut self,
1114 segment: &PathSegment,
1115 end: &[ExpTokenPair<'_>],
1116 ) -> Option<ErrorGuaranteed> {
1117 if !self.may_recover() {
1118 return None;
1119 }
1120
1121 let parsed_angle_bracket_args =
1146 segment.args.as_ref().is_some_and(|args| args.is_angle_bracketed());
1147
1148 debug!(
1149 "check_trailing_angle_brackets: parsed_angle_bracket_args={:?}",
1150 parsed_angle_bracket_args,
1151 );
1152 if !parsed_angle_bracket_args {
1153 return None;
1154 }
1155
1156 let lo = self.token.span;
1159
1160 let mut position = 0;
1164
1165 let mut number_of_shr = 0;
1169 let mut number_of_gt = 0;
1170 while self.look_ahead(position, |t| {
1171 trace!("check_trailing_angle_brackets: t={:?}", t);
1172 if *t == token::Shr {
1173 number_of_shr += 1;
1174 true
1175 } else if *t == token::Gt {
1176 number_of_gt += 1;
1177 true
1178 } else {
1179 false
1180 }
1181 }) {
1182 position += 1;
1183 }
1184
1185 debug!(
1187 "check_trailing_angle_brackets: number_of_gt={:?} number_of_shr={:?}",
1188 number_of_gt, number_of_shr,
1189 );
1190 if number_of_gt < 1 && number_of_shr < 1 {
1191 return None;
1192 }
1193
1194 if self.look_ahead(position, |t| {
1197 trace!("check_trailing_angle_brackets: t={:?}", t);
1198 end.iter().any(|exp| exp.tok == &t.kind)
1199 }) {
1200 self.eat_to_tokens(end);
1203 let span = lo.to(self.prev_token.span);
1204
1205 let num_extra_brackets = number_of_gt + number_of_shr * 2;
1206 return Some(self.dcx().emit_err(UnmatchedAngleBrackets { span, num_extra_brackets }));
1207 }
1208 None
1209 }
1210
1211 pub(super) fn check_turbofish_missing_angle_brackets(&mut self, segment: &mut PathSegment) {
1214 if !self.may_recover() {
1215 return;
1216 }
1217
1218 if self.token == token::PathSep && segment.args.is_none() {
1219 let snapshot = self.create_snapshot_for_diagnostic();
1220 self.bump();
1221 let lo = self.token.span;
1222 match self.parse_angle_args(None) {
1223 Ok(args) => {
1224 let span = lo.to(self.prev_token.span);
1225 let mut trailing_span = self.prev_token.span.shrink_to_hi();
1227 while self.token == token::Shr || self.token == token::Gt {
1228 trailing_span = trailing_span.to(self.token.span);
1229 self.bump();
1230 }
1231 if self.token == token::OpenParen {
1232 segment.args = Some(AngleBracketedArgs { args, span }.into());
1234
1235 self.dcx().emit_err(GenericParamsWithoutAngleBrackets {
1236 span,
1237 sugg: GenericParamsWithoutAngleBracketsSugg {
1238 left: span.shrink_to_lo(),
1239 right: trailing_span,
1240 },
1241 });
1242 } else {
1243 self.restore_snapshot(snapshot);
1245 }
1246 }
1247 Err(err) => {
1248 err.cancel();
1251 self.restore_snapshot(snapshot);
1252 }
1253 }
1254 }
1255 }
1256
1257 pub(super) fn check_mistyped_turbofish_with_multiple_type_params(
1260 &mut self,
1261 mut e: Diag<'a>,
1262 expr: &mut Box<Expr>,
1263 ) -> PResult<'a, ErrorGuaranteed> {
1264 if let ExprKind::Binary(binop, _, _) = &expr.kind
1265 && let ast::BinOpKind::Lt = binop.node
1266 && self.eat(exp!(Comma))
1267 {
1268 let x = self.parse_seq_to_before_end(
1269 exp!(Gt),
1270 SeqSep::trailing_allowed(exp!(Comma)),
1271 |p| match p.parse_generic_arg(None)? {
1272 Some(arg) => Ok(arg),
1273 None => p.unexpected_any(),
1275 },
1276 );
1277 match x {
1278 Ok((_, _, Recovered::No)) => {
1279 if self.eat(exp!(Gt)) {
1280 e.span_suggestion_verbose(
1282 binop.span.shrink_to_lo(),
1283 fluent::parse_sugg_turbofish_syntax,
1284 "::",
1285 Applicability::MaybeIncorrect,
1286 );
1287 match self.parse_expr() {
1288 Ok(_) => {
1289 let guar = e.emit();
1293 *expr = self.mk_expr_err(expr.span.to(self.prev_token.span), guar);
1294 return Ok(guar);
1295 }
1296 Err(err) => {
1297 err.cancel();
1298 }
1299 }
1300 }
1301 }
1302 Ok((_, _, Recovered::Yes(_))) => {}
1303 Err(err) => {
1304 err.cancel();
1305 }
1306 }
1307 }
1308 Err(e)
1309 }
1310
1311 pub(super) fn suggest_add_missing_let_for_stmt(&mut self, err: &mut Diag<'a>) {
1314 if self.token == token::Colon {
1315 let prev_span = self.prev_token.span.shrink_to_lo();
1316 let snapshot = self.create_snapshot_for_diagnostic();
1317 self.bump();
1318 match self.parse_ty() {
1319 Ok(_) => {
1320 if self.token == token::Eq {
1321 let sugg = SuggAddMissingLetStmt { span: prev_span };
1322 sugg.add_to_diag(err);
1323 }
1324 }
1325 Err(e) => {
1326 e.cancel();
1327 }
1328 }
1329 self.restore_snapshot(snapshot);
1330 }
1331 }
1332
1333 fn attempt_chained_comparison_suggestion(
1337 &mut self,
1338 err: &mut ComparisonOperatorsCannotBeChained,
1339 inner_op: &Expr,
1340 outer_op: &Spanned<AssocOp>,
1341 ) -> bool {
1342 if let ExprKind::Binary(op, l1, r1) = &inner_op.kind {
1343 if let ExprKind::Field(_, ident) = l1.kind
1344 && !ident.is_numeric()
1345 && !matches!(r1.kind, ExprKind::Lit(_))
1346 {
1347 return false;
1350 }
1351 return match (op.node, &outer_op.node) {
1352 (BinOpKind::Eq, AssocOp::Binary(BinOpKind::Eq)) |
1354 (BinOpKind::Lt, AssocOp::Binary(BinOpKind::Lt | BinOpKind::Le)) |
1356 (BinOpKind::Le, AssocOp::Binary(BinOpKind::Lt | BinOpKind::Le)) |
1357 (BinOpKind::Gt, AssocOp::Binary(BinOpKind::Gt | BinOpKind::Ge)) |
1359 (BinOpKind::Ge, AssocOp::Binary(BinOpKind::Gt | BinOpKind::Ge)) => {
1360 let expr_to_str = |e: &Expr| {
1361 self.span_to_snippet(e.span)
1362 .unwrap_or_else(|_| pprust::expr_to_string(e))
1363 };
1364 err.chaining_sugg = Some(ComparisonOperatorsCannotBeChainedSugg::SplitComparison {
1365 span: inner_op.span.shrink_to_hi(),
1366 middle_term: expr_to_str(r1),
1367 });
1368 false }
1370 (
1372 BinOpKind::Eq,
1373 AssocOp::Binary(BinOpKind::Lt | BinOpKind::Le | BinOpKind::Gt | BinOpKind::Ge)
1374 ) => {
1375 let snapshot = self.create_snapshot_for_diagnostic();
1377 match self.parse_expr() {
1378 Ok(r2) => {
1379 err.chaining_sugg = Some(ComparisonOperatorsCannotBeChainedSugg::Parenthesize {
1382 left: r1.span.shrink_to_lo(),
1383 right: r2.span.shrink_to_hi(),
1384 });
1385 true
1386 }
1387 Err(expr_err) => {
1388 expr_err.cancel();
1389 self.restore_snapshot(snapshot);
1390 true
1391 }
1392 }
1393 }
1394 (
1396 BinOpKind::Lt | BinOpKind::Le | BinOpKind::Gt | BinOpKind::Ge,
1397 AssocOp::Binary(BinOpKind::Eq)
1398 ) => {
1399 let snapshot = self.create_snapshot_for_diagnostic();
1400 match self.parse_expr() {
1403 Ok(_) => {
1404 err.chaining_sugg = Some(ComparisonOperatorsCannotBeChainedSugg::Parenthesize {
1405 left: l1.span.shrink_to_lo(),
1406 right: r1.span.shrink_to_hi(),
1407 });
1408 true
1409 }
1410 Err(expr_err) => {
1411 expr_err.cancel();
1412 self.restore_snapshot(snapshot);
1413 false
1414 }
1415 }
1416 }
1417 _ => false
1418 };
1419 }
1420 false
1421 }
1422
1423 pub(super) fn check_no_chained_comparison(
1442 &mut self,
1443 inner_op: &Expr,
1444 outer_op: &Spanned<AssocOp>,
1445 ) -> PResult<'a, Option<Box<Expr>>> {
1446 debug_assert!(
1447 outer_op.node.is_comparison(),
1448 "check_no_chained_comparison: {:?} is not comparison",
1449 outer_op.node,
1450 );
1451
1452 let mk_err_expr =
1453 |this: &Self, span, guar| Ok(Some(this.mk_expr(span, ExprKind::Err(guar))));
1454
1455 match &inner_op.kind {
1456 ExprKind::Binary(op, l1, r1) if op.node.is_comparison() => {
1457 let mut err = ComparisonOperatorsCannotBeChained {
1458 span: vec![op.span, self.prev_token.span],
1459 suggest_turbofish: None,
1460 help_turbofish: false,
1461 chaining_sugg: None,
1462 };
1463
1464 if op.node == BinOpKind::Lt && outer_op.node == AssocOp::Binary(BinOpKind::Lt)
1467 || outer_op.node == AssocOp::Binary(BinOpKind::Gt)
1468 {
1469 if outer_op.node == AssocOp::Binary(BinOpKind::Lt) {
1470 let snapshot = self.create_snapshot_for_diagnostic();
1471 self.bump();
1472 let modifiers = [(token::Lt, 1), (token::Gt, -1), (token::Shr, -2)];
1474 self.consume_tts(1, &modifiers);
1475
1476 if !matches!(self.token.kind, token::OpenParen | token::PathSep) {
1477 self.restore_snapshot(snapshot);
1480 }
1481 }
1482 return if self.token == token::PathSep {
1483 if let ExprKind::Binary(o, ..) = inner_op.kind
1486 && o.node == BinOpKind::Lt
1487 {
1488 err.suggest_turbofish = Some(op.span.shrink_to_lo());
1489 } else {
1490 err.help_turbofish = true;
1491 }
1492
1493 let snapshot = self.create_snapshot_for_diagnostic();
1494 self.bump(); match self.parse_expr() {
1498 Ok(_) => {
1499 let guar = self.dcx().emit_err(err);
1501 mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar)
1505 }
1506 Err(expr_err) => {
1507 expr_err.cancel();
1508 self.restore_snapshot(snapshot);
1511 Err(self.dcx().create_err(err))
1512 }
1513 }
1514 } else if self.token == token::OpenParen {
1515 if let ExprKind::Binary(o, ..) = inner_op.kind
1518 && o.node == BinOpKind::Lt
1519 {
1520 err.suggest_turbofish = Some(op.span.shrink_to_lo());
1521 } else {
1522 err.help_turbofish = true;
1523 }
1524 match self.consume_fn_args() {
1526 Err(()) => Err(self.dcx().create_err(err)),
1527 Ok(()) => {
1528 let guar = self.dcx().emit_err(err);
1529 mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar)
1533 }
1534 }
1535 } else {
1536 if !matches!(l1.kind, ExprKind::Lit(_))
1537 && !matches!(r1.kind, ExprKind::Lit(_))
1538 {
1539 err.help_turbofish = true;
1542 }
1543
1544 let recovered = self
1547 .attempt_chained_comparison_suggestion(&mut err, inner_op, outer_op);
1548 if recovered {
1549 let guar = self.dcx().emit_err(err);
1550 mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar)
1551 } else {
1552 Err(self.dcx().create_err(err))
1554 }
1555 };
1556 }
1557 let recovered =
1558 self.attempt_chained_comparison_suggestion(&mut err, inner_op, outer_op);
1559 let guar = self.dcx().emit_err(err);
1560 if recovered {
1561 return mk_err_expr(self, inner_op.span.to(self.prev_token.span), guar);
1562 }
1563 }
1564 _ => {}
1565 }
1566 Ok(None)
1567 }
1568
1569 fn consume_fn_args(&mut self) -> Result<(), ()> {
1570 let snapshot = self.create_snapshot_for_diagnostic();
1571 self.bump(); let modifiers = [(token::OpenParen, 1), (token::CloseParen, -1)];
1575 self.consume_tts(1, &modifiers);
1576
1577 if self.token == token::Eof {
1578 self.restore_snapshot(snapshot);
1580 Err(())
1581 } else {
1582 Ok(())
1584 }
1585 }
1586
1587 pub(super) fn maybe_report_ambiguous_plus(&mut self, impl_dyn_multi: bool, ty: &Ty) {
1588 if impl_dyn_multi {
1589 self.dcx().emit_err(AmbiguousPlus {
1590 span: ty.span,
1591 suggestion: AddParen { lo: ty.span.shrink_to_lo(), hi: ty.span.shrink_to_hi() },
1592 });
1593 }
1594 }
1595
1596 pub(super) fn maybe_recover_from_question_mark(&mut self, ty: Box<Ty>) -> Box<Ty> {
1598 if self.token == token::Question {
1599 self.bump();
1600 let guar = self.dcx().emit_err(QuestionMarkInType {
1601 span: self.prev_token.span,
1602 sugg: QuestionMarkInTypeSugg {
1603 left: ty.span.shrink_to_lo(),
1604 right: self.prev_token.span,
1605 },
1606 });
1607 self.mk_ty(ty.span.to(self.prev_token.span), TyKind::Err(guar))
1608 } else {
1609 ty
1610 }
1611 }
1612
1613 pub(super) fn maybe_recover_from_ternary_operator(
1619 &mut self,
1620 cond: Option<Span>,
1621 ) -> PResult<'a, ()> {
1622 if self.prev_token != token::Question {
1623 return PResult::Ok(());
1624 }
1625
1626 let question = self.prev_token.span;
1627 let lo = cond.unwrap_or(question).lo();
1628 let snapshot = self.create_snapshot_for_diagnostic();
1629
1630 if match self.parse_expr() {
1631 Ok(_) => true,
1632 Err(err) => {
1633 err.cancel();
1634 self.token == token::Colon
1637 }
1638 } {
1639 if self.eat_noexpect(&token::Colon) {
1640 let colon = self.prev_token.span;
1641 match self.parse_expr() {
1642 Ok(expr) => {
1643 let sugg = cond.map(|cond| TernaryOperatorSuggestion {
1644 before_cond: cond.shrink_to_lo(),
1645 question,
1646 colon,
1647 end: expr.span.shrink_to_hi(),
1648 });
1649 return Err(self.dcx().create_err(TernaryOperator {
1650 span: self.prev_token.span.with_lo(lo),
1651 sugg,
1652 no_sugg: sugg.is_none(),
1653 }));
1654 }
1655 Err(err) => {
1656 err.cancel();
1657 }
1658 };
1659 }
1660 }
1661 self.restore_snapshot(snapshot);
1662 Ok(())
1663 }
1664
1665 pub(super) fn maybe_recover_from_bad_type_plus(&mut self, ty: &Ty) -> PResult<'a, ()> {
1666 if !self.token.is_like_plus() {
1668 return Ok(());
1669 }
1670
1671 self.bump(); let _bounds = self.parse_generic_bounds()?;
1673 let sub = match &ty.kind {
1674 TyKind::Ref(_lifetime, mut_ty) => {
1675 let lo = mut_ty.ty.span.shrink_to_lo();
1676 let hi = self.prev_token.span.shrink_to_hi();
1677 BadTypePlusSub::AddParen { suggestion: AddParen { lo, hi } }
1678 }
1679 TyKind::Ptr(..) | TyKind::FnPtr(..) => {
1680 BadTypePlusSub::ForgotParen { span: ty.span.to(self.prev_token.span) }
1681 }
1682 _ => BadTypePlusSub::ExpectPath { span: ty.span },
1683 };
1684
1685 self.dcx().emit_err(BadTypePlus { span: ty.span, sub });
1686
1687 Ok(())
1688 }
1689
1690 pub(super) fn recover_from_prefix_increment(
1691 &mut self,
1692 operand_expr: Box<Expr>,
1693 op_span: Span,
1694 start_stmt: bool,
1695 ) -> PResult<'a, Box<Expr>> {
1696 let standalone = if start_stmt { IsStandalone::Standalone } else { IsStandalone::Subexpr };
1697 let kind = IncDecRecovery { standalone, op: IncOrDec::Inc, fixity: UnaryFixity::Pre };
1698 self.recover_from_inc_dec(operand_expr, kind, op_span)
1699 }
1700
1701 pub(super) fn recover_from_postfix_increment(
1702 &mut self,
1703 operand_expr: Box<Expr>,
1704 op_span: Span,
1705 start_stmt: bool,
1706 ) -> PResult<'a, Box<Expr>> {
1707 let kind = IncDecRecovery {
1708 standalone: if start_stmt { IsStandalone::Standalone } else { IsStandalone::Subexpr },
1709 op: IncOrDec::Inc,
1710 fixity: UnaryFixity::Post,
1711 };
1712 self.recover_from_inc_dec(operand_expr, kind, op_span)
1713 }
1714
1715 pub(super) fn recover_from_postfix_decrement(
1716 &mut self,
1717 operand_expr: Box<Expr>,
1718 op_span: Span,
1719 start_stmt: bool,
1720 ) -> PResult<'a, Box<Expr>> {
1721 let kind = IncDecRecovery {
1722 standalone: if start_stmt { IsStandalone::Standalone } else { IsStandalone::Subexpr },
1723 op: IncOrDec::Dec,
1724 fixity: UnaryFixity::Post,
1725 };
1726 self.recover_from_inc_dec(operand_expr, kind, op_span)
1727 }
1728
1729 fn recover_from_inc_dec(
1730 &mut self,
1731 base: Box<Expr>,
1732 kind: IncDecRecovery,
1733 op_span: Span,
1734 ) -> PResult<'a, Box<Expr>> {
1735 let mut err = self.dcx().struct_span_err(
1736 op_span,
1737 format!("Rust has no {} {} operator", kind.fixity, kind.op.name()),
1738 );
1739 err.span_label(op_span, format!("not a valid {} operator", kind.fixity));
1740
1741 let help_base_case = |mut err: Diag<'_, _>, base| {
1742 err.help(format!("use `{}= 1` instead", kind.op.chr()));
1743 err.emit();
1744 Ok(base)
1745 };
1746
1747 let spans = match kind.fixity {
1749 UnaryFixity::Pre => (op_span, base.span.shrink_to_hi()),
1750 UnaryFixity::Post => (base.span.shrink_to_lo(), op_span),
1751 };
1752
1753 match kind.standalone {
1754 IsStandalone::Standalone => {
1755 self.inc_dec_standalone_suggest(kind, spans).emit_verbose(&mut err)
1756 }
1757 IsStandalone::Subexpr => {
1758 let Ok(base_src) = self.span_to_snippet(base.span) else {
1759 return help_base_case(err, base);
1760 };
1761 match kind.fixity {
1762 UnaryFixity::Pre => {
1763 self.prefix_inc_dec_suggest(base_src, kind, spans).emit(&mut err)
1764 }
1765 UnaryFixity::Post => {
1766 if !matches!(base.kind, ExprKind::Binary(_, _, _)) {
1769 self.postfix_inc_dec_suggest(base_src, kind, spans).emit(&mut err)
1770 }
1771 }
1772 }
1773 }
1774 }
1775 Err(err)
1776 }
1777
1778 fn prefix_inc_dec_suggest(
1779 &mut self,
1780 base_src: String,
1781 kind: IncDecRecovery,
1782 (pre_span, post_span): (Span, Span),
1783 ) -> MultiSugg {
1784 MultiSugg {
1785 msg: format!("use `{}= 1` instead", kind.op.chr()),
1786 patches: vec![
1787 (pre_span, "{ ".to_string()),
1788 (post_span, format!(" {}= 1; {} }}", kind.op.chr(), base_src)),
1789 ],
1790 applicability: Applicability::MachineApplicable,
1791 }
1792 }
1793
1794 fn postfix_inc_dec_suggest(
1795 &mut self,
1796 base_src: String,
1797 kind: IncDecRecovery,
1798 (pre_span, post_span): (Span, Span),
1799 ) -> MultiSugg {
1800 let tmp_var = if base_src.trim() == "tmp" { "tmp_" } else { "tmp" };
1801 MultiSugg {
1802 msg: format!("use `{}= 1` instead", kind.op.chr()),
1803 patches: vec![
1804 (pre_span, format!("{{ let {tmp_var} = ")),
1805 (post_span, format!("; {} {}= 1; {} }}", base_src, kind.op.chr(), tmp_var)),
1806 ],
1807 applicability: Applicability::HasPlaceholders,
1808 }
1809 }
1810
1811 fn inc_dec_standalone_suggest(
1812 &mut self,
1813 kind: IncDecRecovery,
1814 (pre_span, post_span): (Span, Span),
1815 ) -> MultiSugg {
1816 let mut patches = Vec::new();
1817
1818 if !pre_span.is_empty() {
1819 patches.push((pre_span, String::new()));
1820 }
1821
1822 patches.push((post_span, format!(" {}= 1", kind.op.chr())));
1823 MultiSugg {
1824 msg: format!("use `{}= 1` instead", kind.op.chr()),
1825 patches,
1826 applicability: Applicability::MachineApplicable,
1827 }
1828 }
1829
1830 pub(super) fn maybe_recover_from_bad_qpath<T: RecoverQPath>(
1834 &mut self,
1835 base: Box<T>,
1836 ) -> PResult<'a, Box<T>> {
1837 if !self.may_recover() {
1838 return Ok(base);
1839 }
1840
1841 if self.token == token::PathSep {
1843 if let Some(ty) = base.to_ty() {
1844 return self.maybe_recover_from_bad_qpath_stage_2(ty.span, ty);
1845 }
1846 }
1847 Ok(base)
1848 }
1849
1850 pub(super) fn maybe_recover_from_bad_qpath_stage_2<T: RecoverQPath>(
1853 &mut self,
1854 ty_span: Span,
1855 ty: Box<Ty>,
1856 ) -> PResult<'a, Box<T>> {
1857 self.expect(exp!(PathSep))?;
1858
1859 let mut path = ast::Path { segments: ThinVec::new(), span: DUMMY_SP, tokens: None };
1860 self.parse_path_segments(&mut path.segments, T::PATH_STYLE, None)?;
1861 path.span = ty_span.to(self.prev_token.span);
1862
1863 self.dcx().emit_err(BadQPathStage2 {
1864 span: ty_span,
1865 wrap: WrapType { lo: ty_span.shrink_to_lo(), hi: ty_span.shrink_to_hi() },
1866 });
1867
1868 let path_span = ty_span.shrink_to_hi(); Ok(Box::new(T::recovered(Some(Box::new(QSelf { ty, path_span, position: 0 })), path)))
1870 }
1871
1872 pub fn maybe_consume_incorrect_semicolon(&mut self, previous_item: Option<&Item>) -> bool {
1875 if self.token != TokenKind::Semi {
1876 return false;
1877 }
1878
1879 let err = match previous_item {
1882 Some(previous_item) => {
1883 let name = match previous_item.kind {
1884 ItemKind::Struct(..) => "braced struct",
1887 _ => previous_item.kind.descr(),
1888 };
1889 IncorrectSemicolon { span: self.token.span, name, show_help: true }
1890 }
1891 None => IncorrectSemicolon { span: self.token.span, name: "", show_help: false },
1892 };
1893 self.dcx().emit_err(err);
1894
1895 self.bump();
1896 true
1897 }
1898
1899 pub(super) fn unexpected_try_recover(&mut self, t: &TokenKind) -> PResult<'a, Recovered> {
1902 let token_str = pprust::token_kind_to_string(t);
1903 let this_token_str = super::token_descr(&self.token);
1904 let (prev_sp, sp) = match (&self.token.kind, self.subparser_name) {
1905 (token::Eof, Some(_)) => {
1907 let sp = self.prev_token.span.shrink_to_hi();
1908 (sp, sp)
1909 }
1910 _ if self.prev_token.span == DUMMY_SP => (self.token.span, self.token.span),
1913 (token::Eof, None) => (self.prev_token.span, self.token.span),
1915 _ => (self.prev_token.span.shrink_to_hi(), self.token.span),
1916 };
1917 let msg = format!(
1918 "expected `{}`, found {}",
1919 token_str,
1920 match (&self.token.kind, self.subparser_name) {
1921 (token::Eof, Some(origin)) => format!("end of {origin}"),
1922 _ => this_token_str,
1923 },
1924 );
1925 let mut err = self.dcx().struct_span_err(sp, msg);
1926 let label_exp = format!("expected `{token_str}`");
1927 let sm = self.psess.source_map();
1928 if !sm.is_multiline(prev_sp.until(sp)) {
1929 err.span_label(sp, label_exp);
1932 } else {
1933 err.span_label(prev_sp, label_exp);
1934 err.span_label(sp, "unexpected token");
1935 }
1936 Err(err)
1937 }
1938
1939 pub(super) fn expect_semi(&mut self) -> PResult<'a, ()> {
1940 if self.eat(exp!(Semi)) || self.recover_colon_as_semi() {
1941 return Ok(());
1942 }
1943 self.expect(exp!(Semi)).map(drop) }
1945
1946 pub(super) fn recover_colon_as_semi(&mut self) -> bool {
1947 let line_idx = |span: Span| {
1948 self.psess
1949 .source_map()
1950 .span_to_lines(span)
1951 .ok()
1952 .and_then(|lines| Some(lines.lines.get(0)?.line_index))
1953 };
1954
1955 if self.may_recover()
1956 && self.token == token::Colon
1957 && self.look_ahead(1, |next| line_idx(self.token.span) < line_idx(next.span))
1958 {
1959 self.dcx().emit_err(ColonAsSemi { span: self.token.span });
1960 self.bump();
1961 return true;
1962 }
1963
1964 false
1965 }
1966
1967 pub(super) fn recover_incorrect_await_syntax(
1970 &mut self,
1971 await_sp: Span,
1972 ) -> PResult<'a, Box<Expr>> {
1973 let (hi, expr, is_question) = if self.token == token::Bang {
1974 self.recover_await_macro()?
1976 } else {
1977 self.recover_await_prefix(await_sp)?
1978 };
1979 let (sp, guar) = self.error_on_incorrect_await(await_sp, hi, &expr, is_question);
1980 let expr = self.mk_expr_err(await_sp.to(sp), guar);
1981 self.maybe_recover_from_bad_qpath(expr)
1982 }
1983
1984 fn recover_await_macro(&mut self) -> PResult<'a, (Span, Box<Expr>, bool)> {
1985 self.expect(exp!(Bang))?;
1986 self.expect(exp!(OpenParen))?;
1987 let expr = self.parse_expr()?;
1988 self.expect(exp!(CloseParen))?;
1989 Ok((self.prev_token.span, expr, false))
1990 }
1991
1992 fn recover_await_prefix(&mut self, await_sp: Span) -> PResult<'a, (Span, Box<Expr>, bool)> {
1993 let is_question = self.eat(exp!(Question)); let expr = if self.token == token::OpenBrace {
1995 self.parse_expr_block(None, self.token.span, BlockCheckMode::Default)
1999 } else {
2000 self.parse_expr()
2001 }
2002 .map_err(|mut err| {
2003 err.span_label(await_sp, format!("while parsing this incorrect await expression"));
2004 err
2005 })?;
2006 Ok((expr.span, expr, is_question))
2007 }
2008
2009 fn error_on_incorrect_await(
2010 &self,
2011 lo: Span,
2012 hi: Span,
2013 expr: &Expr,
2014 is_question: bool,
2015 ) -> (Span, ErrorGuaranteed) {
2016 let span = lo.to(hi);
2017 let guar = self.dcx().emit_err(IncorrectAwait {
2018 span,
2019 suggestion: AwaitSuggestion {
2020 removal: lo.until(expr.span),
2021 dot_await: expr.span.shrink_to_hi(),
2022 question_mark: if is_question { "?" } else { "" },
2023 },
2024 });
2025 (span, guar)
2026 }
2027
2028 pub(super) fn recover_from_await_method_call(&mut self) {
2030 if self.token == token::OpenParen && self.look_ahead(1, |t| t == &token::CloseParen) {
2031 let lo = self.token.span;
2033 self.bump(); let span = lo.to(self.token.span);
2035 self.bump(); self.dcx().emit_err(IncorrectUseOfAwait { span });
2038 }
2039 }
2040 pub(super) fn recover_from_use(&mut self) {
2043 if self.token == token::OpenParen && self.look_ahead(1, |t| t == &token::CloseParen) {
2044 let lo = self.token.span;
2046 self.bump(); let span = lo.to(self.token.span);
2048 self.bump(); self.dcx().emit_err(IncorrectUseOfUse { span });
2051 }
2052 }
2053
2054 pub(super) fn try_macro_suggestion(&mut self) -> PResult<'a, Box<Expr>> {
2055 let is_try = self.token.is_keyword(kw::Try);
2056 let is_questionmark = self.look_ahead(1, |t| t == &token::Bang); let is_open = self.look_ahead(2, |t| t == &token::OpenParen); if is_try && is_questionmark && is_open {
2060 let lo = self.token.span;
2061 self.bump(); self.bump(); let try_span = lo.to(self.token.span); self.bump(); let is_empty = self.token == token::CloseParen; self.consume_block(exp!(OpenParen), exp!(CloseParen), ConsumeClosingDelim::No); let hi = self.token.span;
2068 self.bump(); let mut err = self.dcx().struct_span_err(lo.to(hi), "use of deprecated `try` macro");
2070 err.note("in the 2018 edition `try` is a reserved keyword, and the `try!()` macro is deprecated");
2071 let prefix = if is_empty { "" } else { "alternatively, " };
2072 if !is_empty {
2073 err.multipart_suggestion(
2074 "you can use the `?` operator instead",
2075 vec![(try_span, "".to_owned()), (hi, "?".to_owned())],
2076 Applicability::MachineApplicable,
2077 );
2078 }
2079 err.span_suggestion(lo.shrink_to_lo(), format!("{prefix}you can still access the deprecated `try!()` macro using the \"raw identifier\" syntax"), "r#", Applicability::MachineApplicable);
2080 let guar = err.emit();
2081 Ok(self.mk_expr_err(lo.to(hi), guar))
2082 } else {
2083 Err(self.expected_expression_found()) }
2085 }
2086
2087 pub(super) fn expect_gt_or_maybe_suggest_closing_generics(
2094 &mut self,
2095 params: &[ast::GenericParam],
2096 ) -> PResult<'a, ()> {
2097 let Err(mut err) = self.expect_gt() else {
2098 return Ok(());
2099 };
2100 if let [.., ast::GenericParam { bounds, .. }] = params
2102 && let Some(poly) = bounds
2103 .iter()
2104 .filter_map(|bound| match bound {
2105 ast::GenericBound::Trait(poly) => Some(poly),
2106 _ => None,
2107 })
2108 .next_back()
2109 {
2110 err.span_suggestion_verbose(
2111 poly.span.shrink_to_hi(),
2112 "you might have meant to end the type parameters here",
2113 ">",
2114 Applicability::MaybeIncorrect,
2115 );
2116 }
2117 Err(err)
2118 }
2119
2120 pub(super) fn recover_seq_parse_error(
2121 &mut self,
2122 open: ExpTokenPair<'_>,
2123 close: ExpTokenPair<'_>,
2124 lo: Span,
2125 err: Diag<'a>,
2126 ) -> Box<Expr> {
2127 let guar = err.emit();
2128 self.consume_block(open, close, ConsumeClosingDelim::Yes);
2130 self.mk_expr(lo.to(self.prev_token.span), ExprKind::Err(guar))
2131 }
2132
2133 pub(super) fn recover_stmt(&mut self) {
2138 self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore)
2139 }
2140
2141 pub(super) fn recover_stmt_(
2149 &mut self,
2150 break_on_semi: SemiColonMode,
2151 break_on_block: BlockMode,
2152 ) {
2153 let mut brace_depth = 0;
2154 let mut bracket_depth = 0;
2155 let mut in_block = false;
2156 debug!("recover_stmt_ enter loop (semi={:?}, block={:?})", break_on_semi, break_on_block);
2157 loop {
2158 debug!("recover_stmt_ loop {:?}", self.token);
2159 match self.token.kind {
2160 token::OpenBrace => {
2161 brace_depth += 1;
2162 self.bump();
2163 if break_on_block == BlockMode::Break && brace_depth == 1 && bracket_depth == 0
2164 {
2165 in_block = true;
2166 }
2167 }
2168 token::OpenBracket => {
2169 bracket_depth += 1;
2170 self.bump();
2171 }
2172 token::CloseBrace => {
2173 if brace_depth == 0 {
2174 debug!("recover_stmt_ return - close delim {:?}", self.token);
2175 break;
2176 }
2177 brace_depth -= 1;
2178 self.bump();
2179 if in_block && bracket_depth == 0 && brace_depth == 0 {
2180 debug!("recover_stmt_ return - block end {:?}", self.token);
2181 break;
2182 }
2183 }
2184 token::CloseBracket => {
2185 bracket_depth -= 1;
2186 if bracket_depth < 0 {
2187 bracket_depth = 0;
2188 }
2189 self.bump();
2190 }
2191 token::Eof => {
2192 debug!("recover_stmt_ return - Eof");
2193 break;
2194 }
2195 token::Semi => {
2196 self.bump();
2197 if break_on_semi == SemiColonMode::Break
2198 && brace_depth == 0
2199 && bracket_depth == 0
2200 {
2201 debug!("recover_stmt_ return - Semi");
2202 break;
2203 }
2204 }
2205 token::Comma
2206 if break_on_semi == SemiColonMode::Comma
2207 && brace_depth == 0
2208 && bracket_depth == 0 =>
2209 {
2210 break;
2211 }
2212 _ => self.bump(),
2213 }
2214 }
2215 }
2216
2217 pub(super) fn check_for_for_in_in_typo(&mut self, in_span: Span) {
2218 if self.eat_keyword(exp!(In)) {
2219 self.dcx().emit_err(InInTypo {
2221 span: self.prev_token.span,
2222 sugg_span: in_span.until(self.prev_token.span),
2223 });
2224 }
2225 }
2226
2227 pub(super) fn eat_incorrect_doc_comment_for_param_type(&mut self) {
2228 if let token::DocComment(..) = self.token.kind {
2229 self.dcx().emit_err(DocCommentOnParamType { span: self.token.span });
2230 self.bump();
2231 } else if self.token == token::Pound && self.look_ahead(1, |t| *t == token::OpenBracket) {
2232 let lo = self.token.span;
2233 while self.token != token::CloseBracket {
2235 self.bump();
2236 }
2237 let sp = lo.to(self.token.span);
2238 self.bump();
2239 self.dcx().emit_err(AttributeOnParamType { span: sp });
2240 }
2241 }
2242
2243 pub(super) fn parameter_without_type(
2244 &mut self,
2245 err: &mut Diag<'_>,
2246 pat: Box<ast::Pat>,
2247 require_name: bool,
2248 first_param: bool,
2249 ) -> Option<Ident> {
2250 if self.check_ident()
2253 && self.look_ahead(1, |t| *t == token::Comma || *t == token::CloseParen)
2254 {
2255 let ident = self.parse_ident().unwrap();
2257 let span = pat.span.with_hi(ident.span.hi());
2258
2259 err.span_suggestion(
2260 span,
2261 "declare the type after the parameter binding",
2262 "<identifier>: <type>",
2263 Applicability::HasPlaceholders,
2264 );
2265 return Some(ident);
2266 } else if require_name
2267 && (self.token == token::Comma
2268 || self.token == token::Lt
2269 || self.token == token::CloseParen)
2270 {
2271 let rfc_note = "anonymous parameters are removed in the 2018 edition (see RFC 1685)";
2272
2273 let (ident, self_sugg, param_sugg, type_sugg, self_span, param_span, type_span) =
2274 match pat.kind {
2275 PatKind::Ident(_, ident, _) => (
2276 ident,
2277 "self: ",
2278 ": TypeName".to_string(),
2279 "_: ",
2280 pat.span.shrink_to_lo(),
2281 pat.span.shrink_to_hi(),
2282 pat.span.shrink_to_lo(),
2283 ),
2284 PatKind::Ref(ref inner_pat, mutab)
2286 if let PatKind::Ident(_, ident, _) = inner_pat.clone().kind =>
2287 {
2288 let mutab = mutab.prefix_str();
2289 (
2290 ident,
2291 "self: ",
2292 format!("{ident}: &{mutab}TypeName"),
2293 "_: ",
2294 pat.span.shrink_to_lo(),
2295 pat.span,
2296 pat.span.shrink_to_lo(),
2297 )
2298 }
2299 _ => {
2300 if let Some(_) = pat.to_ty() {
2302 err.span_suggestion_verbose(
2303 pat.span.shrink_to_lo(),
2304 "explicitly ignore the parameter name",
2305 "_: ".to_string(),
2306 Applicability::MachineApplicable,
2307 );
2308 err.note(rfc_note);
2309 }
2310
2311 return None;
2312 }
2313 };
2314
2315 if first_param {
2317 err.span_suggestion_verbose(
2318 self_span,
2319 "if this is a `self` type, give it a parameter name",
2320 self_sugg,
2321 Applicability::MaybeIncorrect,
2322 );
2323 }
2324 if self.token != token::Lt {
2327 err.span_suggestion_verbose(
2328 param_span,
2329 "if this is a parameter name, give it a type",
2330 param_sugg,
2331 Applicability::HasPlaceholders,
2332 );
2333 }
2334 err.span_suggestion_verbose(
2335 type_span,
2336 "if this is a type, explicitly ignore the parameter name",
2337 type_sugg,
2338 Applicability::MachineApplicable,
2339 );
2340 err.note(rfc_note);
2341
2342 return if self.token == token::Lt { None } else { Some(ident) };
2344 }
2345 None
2346 }
2347
2348 pub(super) fn recover_arg_parse(&mut self) -> PResult<'a, (Box<ast::Pat>, Box<ast::Ty>)> {
2349 let pat = self.parse_pat_no_top_alt(Some(Expected::ArgumentName), None)?;
2350 self.expect(exp!(Colon))?;
2351 let ty = self.parse_ty()?;
2352
2353 self.dcx().emit_err(PatternMethodParamWithoutBody { span: pat.span });
2354
2355 let pat = Box::new(Pat {
2357 kind: PatKind::Wild,
2358 span: pat.span,
2359 id: ast::DUMMY_NODE_ID,
2360 tokens: None,
2361 });
2362 Ok((pat, ty))
2363 }
2364
2365 pub(super) fn recover_bad_self_param(&mut self, mut param: Param) -> PResult<'a, Param> {
2366 let span = param.pat.span;
2367 let guar = self.dcx().emit_err(SelfParamNotFirst { span });
2368 param.ty.kind = TyKind::Err(guar);
2369 Ok(param)
2370 }
2371
2372 pub(super) fn consume_block(
2373 &mut self,
2374 open: ExpTokenPair<'_>,
2375 close: ExpTokenPair<'_>,
2376 consume_close: ConsumeClosingDelim,
2377 ) {
2378 let mut brace_depth = 0;
2379 loop {
2380 if self.eat(open) {
2381 brace_depth += 1;
2382 } else if self.check(close) {
2383 if brace_depth == 0 {
2384 if let ConsumeClosingDelim::Yes = consume_close {
2385 self.bump();
2389 }
2390 return;
2391 } else {
2392 self.bump();
2393 brace_depth -= 1;
2394 continue;
2395 }
2396 } else if self.token == token::Eof {
2397 return;
2398 } else {
2399 self.bump();
2400 }
2401 }
2402 }
2403
2404 pub(super) fn expected_expression_found(&self) -> Diag<'a> {
2405 let (span, msg) = match (&self.token.kind, self.subparser_name) {
2406 (&token::Eof, Some(origin)) => {
2407 let sp = self.prev_token.span.shrink_to_hi();
2408 (sp, format!("expected expression, found end of {origin}"))
2409 }
2410 _ => (
2411 self.token.span,
2412 format!("expected expression, found {}", super::token_descr(&self.token)),
2413 ),
2414 };
2415 let mut err = self.dcx().struct_span_err(span, msg);
2416 let sp = self.psess.source_map().start_point(self.token.span);
2417 if let Some(sp) = self.psess.ambiguous_block_expr_parse.borrow().get(&sp) {
2418 err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp));
2419 }
2420 err.span_label(span, "expected expression");
2421 err
2422 }
2423
2424 fn consume_tts(
2425 &mut self,
2426 mut acc: i64, modifier: &[(token::TokenKind, i64)],
2429 ) {
2430 while acc > 0 {
2431 if let Some((_, val)) = modifier.iter().find(|(t, _)| self.token == *t) {
2432 acc += *val;
2433 }
2434 if self.token == token::Eof {
2435 break;
2436 }
2437 self.bump();
2438 }
2439 }
2440
2441 pub(super) fn deduplicate_recovered_params_names(&self, fn_inputs: &mut ThinVec<Param>) {
2450 let mut seen_inputs = FxHashSet::default();
2451 for input in fn_inputs.iter_mut() {
2452 let opt_ident = if let (PatKind::Ident(_, ident, _), TyKind::Err(_)) =
2453 (&input.pat.kind, &input.ty.kind)
2454 {
2455 Some(*ident)
2456 } else {
2457 None
2458 };
2459 if let Some(ident) = opt_ident {
2460 if seen_inputs.contains(&ident) {
2461 input.pat.kind = PatKind::Wild;
2462 }
2463 seen_inputs.insert(ident);
2464 }
2465 }
2466 }
2467
2468 pub(super) fn handle_ambiguous_unbraced_const_arg(
2472 &mut self,
2473 args: &mut ThinVec<AngleBracketedArg>,
2474 ) -> PResult<'a, bool> {
2475 let arg = args.pop().unwrap();
2479 let mut err = self.dcx().struct_span_err(
2485 self.token.span,
2486 format!("expected one of `,` or `>`, found {}", super::token_descr(&self.token)),
2487 );
2488 err.span_label(self.token.span, "expected one of `,` or `>`");
2489 match self.recover_const_arg(arg.span(), err) {
2490 Ok(arg) => {
2491 args.push(AngleBracketedArg::Arg(arg));
2492 if self.eat(exp!(Comma)) {
2493 return Ok(true); }
2495 }
2496 Err(err) => {
2497 args.push(arg);
2498 err.delay_as_bug();
2500 }
2501 }
2502 Ok(false) }
2504
2505 pub(super) fn handle_unambiguous_unbraced_const_arg(&mut self) -> PResult<'a, Box<Expr>> {
2513 let start = self.token.span;
2514 let attrs = self.parse_outer_attributes()?;
2515 let (expr, _) =
2516 self.parse_expr_res(Restrictions::CONST_EXPR, attrs).map_err(|mut err| {
2517 err.span_label(
2518 start.shrink_to_lo(),
2519 "while parsing a const generic argument starting here",
2520 );
2521 err
2522 })?;
2523 if !self.expr_is_valid_const_arg(&expr) {
2524 self.dcx().emit_err(ConstGenericWithoutBraces {
2525 span: expr.span,
2526 sugg: ConstGenericWithoutBracesSugg {
2527 left: expr.span.shrink_to_lo(),
2528 right: expr.span.shrink_to_hi(),
2529 },
2530 });
2531 }
2532 Ok(expr)
2533 }
2534
2535 fn recover_const_param_decl(&mut self, ty_generics: Option<&Generics>) -> Option<GenericArg> {
2536 let snapshot = self.create_snapshot_for_diagnostic();
2537 let param = match self.parse_const_param(AttrVec::new()) {
2538 Ok(param) => param,
2539 Err(err) => {
2540 err.cancel();
2541 self.restore_snapshot(snapshot);
2542 return None;
2543 }
2544 };
2545
2546 let ident = param.ident.to_string();
2547 let sugg = match (ty_generics, self.psess.source_map().span_to_snippet(param.span())) {
2548 (Some(Generics { params, span: impl_generics, .. }), Ok(snippet)) => {
2549 Some(match ¶ms[..] {
2550 [] => UnexpectedConstParamDeclarationSugg::AddParam {
2551 impl_generics: *impl_generics,
2552 incorrect_decl: param.span(),
2553 snippet,
2554 ident,
2555 },
2556 [.., generic] => UnexpectedConstParamDeclarationSugg::AppendParam {
2557 impl_generics_end: generic.span().shrink_to_hi(),
2558 incorrect_decl: param.span(),
2559 snippet,
2560 ident,
2561 },
2562 })
2563 }
2564 _ => None,
2565 };
2566 let guar =
2567 self.dcx().emit_err(UnexpectedConstParamDeclaration { span: param.span(), sugg });
2568
2569 let value = self.mk_expr_err(param.span(), guar);
2570 Some(GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value }))
2571 }
2572
2573 pub(super) fn recover_const_param_declaration(
2574 &mut self,
2575 ty_generics: Option<&Generics>,
2576 ) -> PResult<'a, Option<GenericArg>> {
2577 if let Some(arg) = self.recover_const_param_decl(ty_generics) {
2579 return Ok(Some(arg));
2580 }
2581
2582 let start = self.token.span;
2584 self.bump(); let mut err = UnexpectedConstInGenericParam { span: start, to_remove: None };
2588 if self.check_const_arg() {
2589 err.to_remove = Some(start.until(self.token.span));
2590 self.dcx().emit_err(err);
2591 Ok(Some(GenericArg::Const(self.parse_const_arg()?)))
2592 } else {
2593 let after_kw_const = self.token.span;
2594 self.recover_const_arg(after_kw_const, self.dcx().create_err(err)).map(Some)
2595 }
2596 }
2597
2598 pub(super) fn recover_const_arg(
2604 &mut self,
2605 start: Span,
2606 mut err: Diag<'a>,
2607 ) -> PResult<'a, GenericArg> {
2608 let is_op_or_dot = AssocOp::from_token(&self.token)
2609 .and_then(|op| {
2610 if let AssocOp::Binary(
2611 BinOpKind::Gt
2612 | BinOpKind::Lt
2613 | BinOpKind::Shr
2614 | BinOpKind::Ge
2615 )
2616 | AssocOp::Assign
2619 | AssocOp::AssignOp(_) = op
2620 {
2621 None
2622 } else {
2623 Some(op)
2624 }
2625 })
2626 .is_some()
2627 || self.token == TokenKind::Dot;
2628 let was_op = matches!(self.prev_token.kind, token::Plus | token::Shr | token::Gt);
2631 if !is_op_or_dot && !was_op {
2632 return Err(err);
2634 }
2635 let snapshot = self.create_snapshot_for_diagnostic();
2636 if is_op_or_dot {
2637 self.bump();
2638 }
2639 match (|| {
2640 let attrs = self.parse_outer_attributes()?;
2641 self.parse_expr_res(Restrictions::CONST_EXPR, attrs)
2642 })() {
2643 Ok((expr, _)) => {
2644 if snapshot.token == token::EqEq {
2646 err.span_suggestion(
2647 snapshot.token.span,
2648 "if you meant to use an associated type binding, replace `==` with `=`",
2649 "=",
2650 Applicability::MaybeIncorrect,
2651 );
2652 let guar = err.emit();
2653 let value = self.mk_expr_err(start.to(expr.span), guar);
2654 return Ok(GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value }));
2655 } else if snapshot.token == token::Colon
2656 && expr.span.lo() == snapshot.token.span.hi()
2657 && matches!(expr.kind, ExprKind::Path(..))
2658 {
2659 err.span_suggestion(
2661 snapshot.token.span,
2662 "write a path separator here",
2663 "::",
2664 Applicability::MaybeIncorrect,
2665 );
2666 let guar = err.emit();
2667 return Ok(GenericArg::Type(
2668 self.mk_ty(start.to(expr.span), TyKind::Err(guar)),
2669 ));
2670 } else if self.token == token::Comma || self.token.kind.should_end_const_arg() {
2671 return Ok(self.dummy_const_arg_needs_braces(err, start.to(expr.span)));
2678 }
2679 }
2680 Err(err) => {
2681 err.cancel();
2682 }
2683 }
2684 self.restore_snapshot(snapshot);
2685 Err(err)
2686 }
2687
2688 pub(crate) fn recover_unbraced_const_arg_that_can_begin_ty(
2692 &mut self,
2693 mut snapshot: SnapshotParser<'a>,
2694 ) -> Option<Box<ast::Expr>> {
2695 match (|| {
2696 let attrs = self.parse_outer_attributes()?;
2697 snapshot.parse_expr_res(Restrictions::CONST_EXPR, attrs)
2698 })() {
2699 Ok((expr, _)) if let token::Comma | token::Gt = snapshot.token.kind => {
2702 self.restore_snapshot(snapshot);
2703 Some(expr)
2704 }
2705 Ok(_) => None,
2706 Err(err) => {
2707 err.cancel();
2708 None
2709 }
2710 }
2711 }
2712
2713 pub(super) fn dummy_const_arg_needs_braces(&self, mut err: Diag<'a>, span: Span) -> GenericArg {
2715 err.multipart_suggestion(
2716 "expressions must be enclosed in braces to be used as const generic \
2717 arguments",
2718 vec![(span.shrink_to_lo(), "{ ".to_string()), (span.shrink_to_hi(), " }".to_string())],
2719 Applicability::MaybeIncorrect,
2720 );
2721 let guar = err.emit();
2722 let value = self.mk_expr_err(span, guar);
2723 GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value })
2724 }
2725
2726 pub(crate) fn maybe_recover_colon_colon_in_pat_typo(
2729 &mut self,
2730 mut first_pat: Box<Pat>,
2731 expected: Option<Expected>,
2732 ) -> Box<Pat> {
2733 if token::Colon != self.token.kind {
2734 return first_pat;
2735 }
2736 if !matches!(first_pat.kind, PatKind::Ident(_, _, None) | PatKind::Path(..))
2737 || !self.look_ahead(1, |token| token.is_non_reserved_ident())
2738 {
2739 let mut snapshot_type = self.create_snapshot_for_diagnostic();
2740 snapshot_type.bump(); match snapshot_type.parse_ty() {
2742 Err(inner_err) => {
2743 inner_err.cancel();
2744 }
2745 Ok(ty) => {
2746 let Err(mut err) = self.expected_one_of_not_found(&[], &[]) else {
2747 return first_pat;
2748 };
2749 err.span_label(ty.span, "specifying the type of a pattern isn't supported");
2750 self.restore_snapshot(snapshot_type);
2751 let span = first_pat.span.to(ty.span);
2752 first_pat = self.mk_pat(span, PatKind::Wild);
2753 err.emit();
2754 }
2755 }
2756 return first_pat;
2757 }
2758 let colon_span = self.token.span;
2761 let mut snapshot_pat = self.create_snapshot_for_diagnostic();
2764 let mut snapshot_type = self.create_snapshot_for_diagnostic();
2765
2766 match self.expected_one_of_not_found(&[], &[]) {
2768 Err(mut err) => {
2769 snapshot_pat.bump();
2771 snapshot_type.bump();
2772 match snapshot_pat.parse_pat_no_top_alt(expected, None) {
2773 Err(inner_err) => {
2774 inner_err.cancel();
2775 }
2776 Ok(mut pat) => {
2777 let new_span = first_pat.span.to(pat.span);
2779 let mut show_sugg = false;
2780 match &mut pat.kind {
2782 PatKind::Struct(qself @ None, path, ..)
2783 | PatKind::TupleStruct(qself @ None, path, _)
2784 | PatKind::Path(qself @ None, path) => match &first_pat.kind {
2785 PatKind::Ident(_, ident, _) => {
2786 path.segments.insert(0, PathSegment::from_ident(*ident));
2787 path.span = new_span;
2788 show_sugg = true;
2789 first_pat = pat;
2790 }
2791 PatKind::Path(old_qself, old_path) => {
2792 path.segments = old_path
2793 .segments
2794 .iter()
2795 .cloned()
2796 .chain(take(&mut path.segments))
2797 .collect();
2798 path.span = new_span;
2799 *qself = old_qself.clone();
2800 first_pat = pat;
2801 show_sugg = true;
2802 }
2803 _ => {}
2804 },
2805 PatKind::Ident(BindingMode::NONE, ident, None) => {
2806 match &first_pat.kind {
2807 PatKind::Ident(_, old_ident, _) => {
2808 let path = PatKind::Path(
2809 None,
2810 Path {
2811 span: new_span,
2812 segments: thin_vec![
2813 PathSegment::from_ident(*old_ident),
2814 PathSegment::from_ident(*ident),
2815 ],
2816 tokens: None,
2817 },
2818 );
2819 first_pat = self.mk_pat(new_span, path);
2820 show_sugg = true;
2821 }
2822 PatKind::Path(old_qself, old_path) => {
2823 let mut segments = old_path.segments.clone();
2824 segments.push(PathSegment::from_ident(*ident));
2825 let path = PatKind::Path(
2826 old_qself.clone(),
2827 Path { span: new_span, segments, tokens: None },
2828 );
2829 first_pat = self.mk_pat(new_span, path);
2830 show_sugg = true;
2831 }
2832 _ => {}
2833 }
2834 }
2835 _ => {}
2836 }
2837 if show_sugg {
2838 err.span_suggestion_verbose(
2839 colon_span.until(self.look_ahead(1, |t| t.span)),
2840 "maybe write a path separator here",
2841 "::",
2842 Applicability::MaybeIncorrect,
2843 );
2844 } else {
2845 first_pat = self.mk_pat(new_span, PatKind::Wild);
2846 }
2847 self.restore_snapshot(snapshot_pat);
2848 }
2849 }
2850 match snapshot_type.parse_ty() {
2851 Err(inner_err) => {
2852 inner_err.cancel();
2853 }
2854 Ok(ty) => {
2855 err.span_label(ty.span, "specifying the type of a pattern isn't supported");
2856 self.restore_snapshot(snapshot_type);
2857 let new_span = first_pat.span.to(ty.span);
2858 first_pat = self.mk_pat(new_span, PatKind::Wild);
2859 }
2860 }
2861 err.emit();
2862 }
2863 _ => {
2864 }
2866 };
2867 first_pat
2868 }
2869
2870 pub(crate) fn maybe_recover_unexpected_block_label(
2873 &mut self,
2874 loop_header: Option<Span>,
2875 ) -> bool {
2876 if !(self.check_lifetime()
2878 && self.look_ahead(1, |t| *t == token::Colon)
2879 && self.look_ahead(2, |t| *t == token::OpenBrace))
2880 {
2881 return false;
2882 }
2883 let label = self.eat_label().expect("just checked if a label exists");
2884 self.bump(); let span = label.ident.span.to(self.prev_token.span);
2886 let mut diag = self
2887 .dcx()
2888 .struct_span_err(span, "block label not supported here")
2889 .with_span_label(span, "not supported here");
2890 if let Some(loop_header) = loop_header {
2891 diag.multipart_suggestion(
2892 "if you meant to label the loop, move this label before the loop",
2893 vec![
2894 (label.ident.span.until(self.token.span), String::from("")),
2895 (loop_header.shrink_to_lo(), format!("{}: ", label.ident)),
2896 ],
2897 Applicability::MachineApplicable,
2898 );
2899 } else {
2900 diag.tool_only_span_suggestion(
2901 label.ident.span.until(self.token.span),
2902 "remove this block label",
2903 "",
2904 Applicability::MachineApplicable,
2905 );
2906 }
2907 diag.emit();
2908 true
2909 }
2910
2911 pub(crate) fn maybe_recover_unexpected_comma(
2914 &mut self,
2915 lo: Span,
2916 rt: CommaRecoveryMode,
2917 ) -> PResult<'a, ()> {
2918 if self.token != token::Comma {
2919 return Ok(());
2920 }
2921
2922 let comma_span = self.token.span;
2927 self.bump();
2928 if let Err(err) = self.skip_pat_list() {
2929 err.cancel();
2932 }
2933 let seq_span = lo.to(self.prev_token.span);
2934 let mut err = self.dcx().struct_span_err(comma_span, "unexpected `,` in pattern");
2935 if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
2936 err.multipart_suggestion(
2937 format!(
2938 "try adding parentheses to match on a tuple{}",
2939 if let CommaRecoveryMode::LikelyTuple = rt { "" } else { "..." },
2940 ),
2941 vec![
2942 (seq_span.shrink_to_lo(), "(".to_string()),
2943 (seq_span.shrink_to_hi(), ")".to_string()),
2944 ],
2945 Applicability::MachineApplicable,
2946 );
2947 if let CommaRecoveryMode::EitherTupleOrPipe = rt {
2948 err.span_suggestion(
2949 seq_span,
2950 "...or a vertical bar to match on multiple alternatives",
2951 seq_snippet.replace(',', " |"),
2952 Applicability::MachineApplicable,
2953 );
2954 }
2955 }
2956 Err(err)
2957 }
2958
2959 pub(crate) fn maybe_recover_bounds_doubled_colon(&mut self, ty: &Ty) -> PResult<'a, ()> {
2960 let TyKind::Path(qself, path) = &ty.kind else { return Ok(()) };
2961 let qself_position = qself.as_ref().map(|qself| qself.position);
2962 for (i, segments) in path.segments.windows(2).enumerate() {
2963 if qself_position.is_some_and(|pos| i < pos) {
2964 continue;
2965 }
2966 if let [a, b] = segments {
2967 let (a_span, b_span) = (a.span(), b.span());
2968 let between_span = a_span.shrink_to_hi().to(b_span.shrink_to_lo());
2969 if self.span_to_snippet(between_span).as_deref() == Ok(":: ") {
2970 return Err(self.dcx().create_err(DoubleColonInBound {
2971 span: path.span.shrink_to_hi(),
2972 between: between_span,
2973 }));
2974 }
2975 }
2976 }
2977 Ok(())
2978 }
2979
2980 pub(crate) fn maybe_err_dotdotlt_syntax(&self, maybe_lt: Token, mut err: Diag<'a>) -> Diag<'a> {
2982 if maybe_lt == token::Lt
2983 && (self.expected_token_types.contains(TokenType::Gt)
2984 || matches!(self.token.kind, token::Literal(..)))
2985 {
2986 err.span_suggestion(
2987 maybe_lt.span,
2988 "remove the `<` to write an exclusive range",
2989 "",
2990 Applicability::MachineApplicable,
2991 );
2992 }
2993 err
2994 }
2995
2996 pub(super) fn is_vcs_conflict_marker(
3004 &mut self,
3005 long_kind: &TokenKind,
3006 short_kind: &TokenKind,
3007 ) -> bool {
3008 (0..3).all(|i| self.look_ahead(i, |tok| tok == long_kind))
3009 && self.look_ahead(3, |tok| tok == short_kind)
3010 }
3011
3012 fn conflict_marker(&mut self, long_kind: &TokenKind, short_kind: &TokenKind) -> Option<Span> {
3013 if self.is_vcs_conflict_marker(long_kind, short_kind) {
3014 let lo = self.token.span;
3015 for _ in 0..4 {
3016 self.bump();
3017 }
3018 return Some(lo.to(self.prev_token.span));
3019 }
3020 None
3021 }
3022
3023 pub(super) fn recover_vcs_conflict_marker(&mut self) {
3024 let Some(start) = self.conflict_marker(&TokenKind::Shl, &TokenKind::Lt) else {
3026 return;
3027 };
3028 let mut spans = Vec::with_capacity(3);
3029 spans.push(start);
3030 let mut middlediff3 = None;
3032 let mut middle = None;
3034 let mut end = None;
3036 loop {
3037 if self.token == TokenKind::Eof {
3038 break;
3039 }
3040 if let Some(span) = self.conflict_marker(&TokenKind::OrOr, &TokenKind::Or) {
3041 middlediff3 = Some(span);
3042 }
3043 if let Some(span) = self.conflict_marker(&TokenKind::EqEq, &TokenKind::Eq) {
3044 middle = Some(span);
3045 }
3046 if let Some(span) = self.conflict_marker(&TokenKind::Shr, &TokenKind::Gt) {
3047 spans.push(span);
3048 end = Some(span);
3049 break;
3050 }
3051 self.bump();
3052 }
3053
3054 let mut err = self.dcx().struct_span_fatal(spans, "encountered diff marker");
3055 match middlediff3 {
3056 Some(middlediff3) => {
3058 err.span_label(
3059 start,
3060 "between this marker and `|||||||` is the code that we're merging into",
3061 );
3062 err.span_label(middlediff3, "between this marker and `=======` is the base code (what the two refs diverged from)");
3063 }
3064 None => {
3065 err.span_label(
3066 start,
3067 "between this marker and `=======` is the code that we're merging into",
3068 );
3069 }
3070 };
3071
3072 if let Some(middle) = middle {
3073 err.span_label(middle, "between this marker and `>>>>>>>` is the incoming code");
3074 }
3075 if let Some(end) = end {
3076 err.span_label(end, "this marker concludes the conflict region");
3077 }
3078 err.note(
3079 "conflict markers indicate that a merge was started but could not be completed due \
3080 to merge conflicts\n\
3081 to resolve a conflict, keep only the code you want and then delete the lines \
3082 containing conflict markers",
3083 );
3084 err.help(
3085 "if you're having merge conflicts after pulling new code:\n\
3086 the top section is the code you already had and the bottom section is the remote code\n\
3087 if you're in the middle of a rebase:\n\
3088 the top section is the code being rebased onto and the bottom section is the code \
3089 coming from the current commit being rebased",
3090 );
3091
3092 err.note(
3093 "for an explanation on these markers from the `git` documentation:\n\
3094 visit <https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_checking_out_conflicts>",
3095 );
3096
3097 err.emit();
3098 }
3099
3100 fn skip_pat_list(&mut self) -> PResult<'a, ()> {
3103 while !self.check(exp!(CloseParen)) {
3104 self.parse_pat_no_top_alt(None, None)?;
3105 if !self.eat(exp!(Comma)) {
3106 return Ok(());
3107 }
3108 }
3109 Ok(())
3110 }
3111}