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