]> git.lizzy.rs Git - rust.git/blob - src/librustc_parse/parser/diagnostics.rs
4f259d314fbf19273d2c5ec8a54fa2e51476e8be
[rust.git] / src / librustc_parse / parser / diagnostics.rs
1 use super::{BlockMode, Parser, PathStyle, SemiColonMode, SeqSep, TokenExpectType, TokenType};
2
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_errors::{pluralize, struct_span_err};
5 use rustc_errors::{Applicability, DiagnosticBuilder, Handler, PResult};
6 use rustc_span::source_map::Spanned;
7 use rustc_span::symbol::kw;
8 use rustc_span::{MultiSpan, Span, SpanSnippetError, DUMMY_SP};
9 use syntax::ast::{
10     self, BinOpKind, BindingMode, BlockCheckMode, Expr, ExprKind, Ident, Item, Param,
11 };
12 use syntax::ast::{AttrVec, ItemKind, Mutability, Pat, PatKind, PathSegment, QSelf, Ty, TyKind};
13 use syntax::print::pprust;
14 use syntax::ptr::P;
15 use syntax::token::{self, token_can_begin_expr, TokenKind};
16 use syntax::util::parser::AssocOp;
17
18 use log::{debug, trace};
19 use std::mem;
20
21 const TURBOFISH: &'static str = "use `::<...>` instead of `<...>` to specify type arguments";
22
23 /// Creates a placeholder argument.
24 pub(super) fn dummy_arg(ident: Ident) -> Param {
25     let pat = P(Pat {
26         id: ast::DUMMY_NODE_ID,
27         kind: PatKind::Ident(BindingMode::ByValue(Mutability::Not), ident, None),
28         span: ident.span,
29     });
30     let ty = Ty { kind: TyKind::Err, span: ident.span, id: ast::DUMMY_NODE_ID };
31     Param {
32         attrs: AttrVec::default(),
33         id: ast::DUMMY_NODE_ID,
34         pat,
35         span: ident.span,
36         ty: P(ty),
37         is_placeholder: false,
38     }
39 }
40
41 pub enum Error {
42     FileNotFoundForModule {
43         mod_name: String,
44         default_path: String,
45         secondary_path: String,
46         dir_path: String,
47     },
48     DuplicatePaths {
49         mod_name: String,
50         default_path: String,
51         secondary_path: String,
52     },
53     UselessDocComment,
54 }
55
56 impl Error {
57     fn span_err(self, sp: impl Into<MultiSpan>, handler: &Handler) -> DiagnosticBuilder<'_> {
58         match self {
59             Error::FileNotFoundForModule {
60                 ref mod_name,
61                 ref default_path,
62                 ref secondary_path,
63                 ref dir_path,
64             } => {
65                 let mut err = struct_span_err!(
66                     handler,
67                     sp,
68                     E0583,
69                     "file not found for module `{}`",
70                     mod_name,
71                 );
72                 err.help(&format!(
73                     "name the file either {} or {} inside the directory \"{}\"",
74                     default_path, secondary_path, dir_path,
75                 ));
76                 err
77             }
78             Error::DuplicatePaths { ref mod_name, ref default_path, ref secondary_path } => {
79                 let mut err = struct_span_err!(
80                     handler,
81                     sp,
82                     E0584,
83                     "file for module `{}` found at both {} and {}",
84                     mod_name,
85                     default_path,
86                     secondary_path,
87                 );
88                 err.help("delete or rename one of them to remove the ambiguity");
89                 err
90             }
91             Error::UselessDocComment => {
92                 let mut err = struct_span_err!(
93                     handler,
94                     sp,
95                     E0585,
96                     "found a documentation comment that doesn't document anything",
97                 );
98                 err.help(
99                     "doc comments must come before what they document, maybe a comment was \
100                           intended with `//`?",
101                 );
102                 err
103             }
104         }
105     }
106 }
107
108 pub(super) trait RecoverQPath: Sized + 'static {
109     const PATH_STYLE: PathStyle = PathStyle::Expr;
110     fn to_ty(&self) -> Option<P<Ty>>;
111     fn recovered(qself: Option<QSelf>, path: ast::Path) -> Self;
112 }
113
114 impl RecoverQPath for Ty {
115     const PATH_STYLE: PathStyle = PathStyle::Type;
116     fn to_ty(&self) -> Option<P<Ty>> {
117         Some(P(self.clone()))
118     }
119     fn recovered(qself: Option<QSelf>, path: ast::Path) -> Self {
120         Self { span: path.span, kind: TyKind::Path(qself, path), id: ast::DUMMY_NODE_ID }
121     }
122 }
123
124 impl RecoverQPath for Pat {
125     fn to_ty(&self) -> Option<P<Ty>> {
126         self.to_ty()
127     }
128     fn recovered(qself: Option<QSelf>, path: ast::Path) -> Self {
129         Self { span: path.span, kind: PatKind::Path(qself, path), id: ast::DUMMY_NODE_ID }
130     }
131 }
132
133 impl RecoverQPath for Expr {
134     fn to_ty(&self) -> Option<P<Ty>> {
135         self.to_ty()
136     }
137     fn recovered(qself: Option<QSelf>, path: ast::Path) -> Self {
138         Self {
139             span: path.span,
140             kind: ExprKind::Path(qself, path),
141             attrs: AttrVec::new(),
142             id: ast::DUMMY_NODE_ID,
143         }
144     }
145 }
146
147 /// Control whether the closing delimiter should be consumed when calling `Parser::consume_block`.
148 crate enum ConsumeClosingDelim {
149     Yes,
150     No,
151 }
152
153 impl<'a> Parser<'a> {
154     pub(super) fn span_fatal_err<S: Into<MultiSpan>>(
155         &self,
156         sp: S,
157         err: Error,
158     ) -> DiagnosticBuilder<'a> {
159         err.span_err(sp, self.diagnostic())
160     }
161
162     pub fn struct_span_err<S: Into<MultiSpan>>(&self, sp: S, m: &str) -> DiagnosticBuilder<'a> {
163         self.sess.span_diagnostic.struct_span_err(sp, m)
164     }
165
166     pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, m: &str) -> ! {
167         self.sess.span_diagnostic.span_bug(sp, m)
168     }
169
170     pub(super) fn diagnostic(&self) -> &'a Handler {
171         &self.sess.span_diagnostic
172     }
173
174     pub(super) fn span_to_snippet(&self, span: Span) -> Result<String, SpanSnippetError> {
175         self.sess.source_map().span_to_snippet(span)
176     }
177
178     pub(super) fn expected_ident_found(&self) -> DiagnosticBuilder<'a> {
179         let mut err = self.struct_span_err(
180             self.token.span,
181             &format!("expected identifier, found {}", super::token_descr(&self.token)),
182         );
183         let valid_follow = &[
184             TokenKind::Eq,
185             TokenKind::Colon,
186             TokenKind::Comma,
187             TokenKind::Semi,
188             TokenKind::ModSep,
189             TokenKind::OpenDelim(token::DelimToken::Brace),
190             TokenKind::OpenDelim(token::DelimToken::Paren),
191             TokenKind::CloseDelim(token::DelimToken::Brace),
192             TokenKind::CloseDelim(token::DelimToken::Paren),
193         ];
194         if let token::Ident(name, false) = self.token.kind {
195             if Ident::new(name, self.token.span).is_raw_guess()
196                 && self.look_ahead(1, |t| valid_follow.contains(&t.kind))
197             {
198                 err.span_suggestion(
199                     self.token.span,
200                     "you can escape reserved keywords to use them as identifiers",
201                     format!("r#{}", name),
202                     Applicability::MaybeIncorrect,
203                 );
204             }
205         }
206         if let Some(token_descr) = super::token_descr_opt(&self.token) {
207             err.span_label(self.token.span, format!("expected identifier, found {}", token_descr));
208         } else {
209             err.span_label(self.token.span, "expected identifier");
210             if self.token == token::Comma && self.look_ahead(1, |t| t.is_ident()) {
211                 err.span_suggestion(
212                     self.token.span,
213                     "remove this comma",
214                     String::new(),
215                     Applicability::MachineApplicable,
216                 );
217             }
218         }
219         err
220     }
221
222     pub(super) fn expected_one_of_not_found(
223         &mut self,
224         edible: &[TokenKind],
225         inedible: &[TokenKind],
226     ) -> PResult<'a, bool /* recovered */> {
227         fn tokens_to_string(tokens: &[TokenType]) -> String {
228             let mut i = tokens.iter();
229             // This might be a sign we need a connect method on `Iterator`.
230             let b = i.next().map_or(String::new(), |t| t.to_string());
231             i.enumerate().fold(b, |mut b, (i, a)| {
232                 if tokens.len() > 2 && i == tokens.len() - 2 {
233                     b.push_str(", or ");
234                 } else if tokens.len() == 2 && i == tokens.len() - 2 {
235                     b.push_str(" or ");
236                 } else {
237                     b.push_str(", ");
238                 }
239                 b.push_str(&a.to_string());
240                 b
241             })
242         }
243
244         let mut expected = edible
245             .iter()
246             .map(|x| TokenType::Token(x.clone()))
247             .chain(inedible.iter().map(|x| TokenType::Token(x.clone())))
248             .chain(self.expected_tokens.iter().cloned())
249             .collect::<Vec<_>>();
250         expected.sort_by_cached_key(|x| x.to_string());
251         expected.dedup();
252         let expect = tokens_to_string(&expected[..]);
253         let actual = super::token_descr(&self.token);
254         let (msg_exp, (label_sp, label_exp)) = if expected.len() > 1 {
255             let short_expect = if expected.len() > 6 {
256                 format!("{} possible tokens", expected.len())
257             } else {
258                 expect.clone()
259             };
260             (
261                 format!("expected one of {}, found {}", expect, actual),
262                 (self.prev_span.shrink_to_hi(), format!("expected one of {}", short_expect)),
263             )
264         } else if expected.is_empty() {
265             (
266                 format!("unexpected token: {}", actual),
267                 (self.prev_span, "unexpected token after this".to_string()),
268             )
269         } else {
270             (
271                 format!("expected {}, found {}", expect, actual),
272                 (self.prev_span.shrink_to_hi(), format!("expected {}", expect)),
273             )
274         };
275         self.last_unexpected_token_span = Some(self.token.span);
276         let mut err = self.struct_span_err(self.token.span, &msg_exp);
277         let sp = if self.token == token::Eof {
278             // This is EOF; don't want to point at the following char, but rather the last token.
279             self.prev_span
280         } else {
281             label_sp
282         };
283         match self.recover_closing_delimiter(
284             &expected
285                 .iter()
286                 .filter_map(|tt| match tt {
287                     TokenType::Token(t) => Some(t.clone()),
288                     _ => None,
289                 })
290                 .collect::<Vec<_>>(),
291             err,
292         ) {
293             Err(e) => err = e,
294             Ok(recovered) => {
295                 return Ok(recovered);
296             }
297         }
298
299         let sm = self.sess.source_map();
300         if self.prev_span == DUMMY_SP {
301             // Account for macro context where the previous span might not be
302             // available to avoid incorrect output (#54841).
303             err.span_label(self.token.span, label_exp);
304         } else if !sm.is_multiline(self.token.span.shrink_to_hi().until(sp.shrink_to_lo())) {
305             // When the spans are in the same line, it means that the only content between
306             // them is whitespace, point at the found token in that case:
307             //
308             // X |     () => { syntax error };
309             //   |                    ^^^^^ expected one of 8 possible tokens here
310             //
311             // instead of having:
312             //
313             // X |     () => { syntax error };
314             //   |                   -^^^^^ unexpected token
315             //   |                   |
316             //   |                   expected one of 8 possible tokens here
317             err.span_label(self.token.span, label_exp);
318         } else {
319             err.span_label(sp, label_exp);
320             err.span_label(self.token.span, "unexpected token");
321         }
322         self.maybe_annotate_with_ascription(&mut err, false);
323         Err(err)
324     }
325
326     pub fn maybe_annotate_with_ascription(
327         &mut self,
328         err: &mut DiagnosticBuilder<'_>,
329         maybe_expected_semicolon: bool,
330     ) {
331         if let Some((sp, likely_path)) = self.last_type_ascription.take() {
332             let sm = self.sess.source_map();
333             let next_pos = sm.lookup_char_pos(self.token.span.lo());
334             let op_pos = sm.lookup_char_pos(sp.hi());
335
336             let allow_unstable = self.sess.unstable_features.is_nightly_build();
337
338             if likely_path {
339                 err.span_suggestion(
340                     sp,
341                     "maybe write a path separator here",
342                     "::".to_string(),
343                     if allow_unstable {
344                         Applicability::MaybeIncorrect
345                     } else {
346                         Applicability::MachineApplicable
347                     },
348                 );
349             } else if op_pos.line != next_pos.line && maybe_expected_semicolon {
350                 err.span_suggestion(
351                     sp,
352                     "try using a semicolon",
353                     ";".to_string(),
354                     Applicability::MaybeIncorrect,
355                 );
356             } else if allow_unstable {
357                 err.span_label(sp, "tried to parse a type due to this type ascription");
358             } else {
359                 err.span_label(sp, "tried to parse a type due to this");
360             }
361             if allow_unstable {
362                 // Give extra information about type ascription only if it's a nightly compiler.
363                 err.note(
364                     "`#![feature(type_ascription)]` lets you annotate an expression with a \
365                           type: `<expr>: <type>`",
366                 );
367                 err.note(
368                     "for more information, see \
369                           https://github.com/rust-lang/rust/issues/23416",
370                 );
371             }
372         }
373     }
374
375     /// Eats and discards tokens until one of `kets` is encountered. Respects token trees,
376     /// passes through any errors encountered. Used for error recovery.
377     pub(super) fn eat_to_tokens(&mut self, kets: &[&TokenKind]) {
378         if let Err(ref mut err) =
379             self.parse_seq_to_before_tokens(kets, SeqSep::none(), TokenExpectType::Expect, |p| {
380                 Ok(p.parse_token_tree())
381             })
382         {
383             err.cancel();
384         }
385     }
386
387     /// This function checks if there are trailing angle brackets and produces
388     /// a diagnostic to suggest removing them.
389     ///
390     /// ```ignore (diagnostic)
391     /// let _ = vec![1, 2, 3].into_iter().collect::<Vec<usize>>>>();
392     ///                                                        ^^ help: remove extra angle brackets
393     /// ```
394     pub(super) fn check_trailing_angle_brackets(&mut self, segment: &PathSegment, end: TokenKind) {
395         // This function is intended to be invoked after parsing a path segment where there are two
396         // cases:
397         //
398         // 1. A specific token is expected after the path segment.
399         //    eg. `x.foo(`, `x.foo::<u32>(` (parenthesis - method call),
400         //        `Foo::`, or `Foo::<Bar>::` (mod sep - continued path).
401         // 2. No specific token is expected after the path segment.
402         //    eg. `x.foo` (field access)
403         //
404         // This function is called after parsing `.foo` and before parsing the token `end` (if
405         // present). This includes any angle bracket arguments, such as `.foo::<u32>` or
406         // `Foo::<Bar>`.
407
408         // We only care about trailing angle brackets if we previously parsed angle bracket
409         // arguments. This helps stop us incorrectly suggesting that extra angle brackets be
410         // removed in this case:
411         //
412         // `x.foo >> (3)` (where `x.foo` is a `u32` for example)
413         //
414         // This case is particularly tricky as we won't notice it just looking at the tokens -
415         // it will appear the same (in terms of upcoming tokens) as below (since the `::<u32>` will
416         // have already been parsed):
417         //
418         // `x.foo::<u32>>>(3)`
419         let parsed_angle_bracket_args =
420             segment.args.as_ref().map(|args| args.is_angle_bracketed()).unwrap_or(false);
421
422         debug!(
423             "check_trailing_angle_brackets: parsed_angle_bracket_args={:?}",
424             parsed_angle_bracket_args,
425         );
426         if !parsed_angle_bracket_args {
427             return;
428         }
429
430         // Keep the span at the start so we can highlight the sequence of `>` characters to be
431         // removed.
432         let lo = self.token.span;
433
434         // We need to look-ahead to see if we have `>` characters without moving the cursor forward
435         // (since we might have the field access case and the characters we're eating are
436         // actual operators and not trailing characters - ie `x.foo >> 3`).
437         let mut position = 0;
438
439         // We can encounter `>` or `>>` tokens in any order, so we need to keep track of how
440         // many of each (so we can correctly pluralize our error messages) and continue to
441         // advance.
442         let mut number_of_shr = 0;
443         let mut number_of_gt = 0;
444         while self.look_ahead(position, |t| {
445             trace!("check_trailing_angle_brackets: t={:?}", t);
446             if *t == token::BinOp(token::BinOpToken::Shr) {
447                 number_of_shr += 1;
448                 true
449             } else if *t == token::Gt {
450                 number_of_gt += 1;
451                 true
452             } else {
453                 false
454             }
455         }) {
456             position += 1;
457         }
458
459         // If we didn't find any trailing `>` characters, then we have nothing to error about.
460         debug!(
461             "check_trailing_angle_brackets: number_of_gt={:?} number_of_shr={:?}",
462             number_of_gt, number_of_shr,
463         );
464         if number_of_gt < 1 && number_of_shr < 1 {
465             return;
466         }
467
468         // Finally, double check that we have our end token as otherwise this is the
469         // second case.
470         if self.look_ahead(position, |t| {
471             trace!("check_trailing_angle_brackets: t={:?}", t);
472             *t == end
473         }) {
474             // Eat from where we started until the end token so that parsing can continue
475             // as if we didn't have those extra angle brackets.
476             self.eat_to_tokens(&[&end]);
477             let span = lo.until(self.token.span);
478
479             let total_num_of_gt = number_of_gt + number_of_shr * 2;
480             self.struct_span_err(
481                 span,
482                 &format!("unmatched angle bracket{}", pluralize!(total_num_of_gt)),
483             )
484             .span_suggestion(
485                 span,
486                 &format!("remove extra angle bracket{}", pluralize!(total_num_of_gt)),
487                 String::new(),
488                 Applicability::MachineApplicable,
489             )
490             .emit();
491         }
492     }
493
494     /// Check to see if a pair of chained operators looks like an attempt at chained comparison,
495     /// e.g. `1 < x <= 3`. If so, suggest either splitting the comparison into two, or
496     /// parenthesising the leftmost comparison.
497     fn attempt_chained_comparison_suggestion(
498         &mut self,
499         err: &mut DiagnosticBuilder<'_>,
500         inner_op: &Expr,
501         outer_op: &Spanned<AssocOp>,
502     ) {
503         if let ExprKind::Binary(op, ref l1, ref r1) = inner_op.kind {
504             match (op.node, &outer_op.node) {
505                 // `x < y < z` and friends.
506                 (BinOpKind::Lt, AssocOp::Less) | (BinOpKind::Lt, AssocOp::LessEqual) |
507                 (BinOpKind::Le, AssocOp::LessEqual) | (BinOpKind::Le, AssocOp::Less) |
508                 // `x > y > z` and friends.
509                 (BinOpKind::Gt, AssocOp::Greater) | (BinOpKind::Gt, AssocOp::GreaterEqual) |
510                 (BinOpKind::Ge, AssocOp::GreaterEqual) | (BinOpKind::Ge, AssocOp::Greater) => {
511                     let expr_to_str = |e: &Expr| {
512                         self.span_to_snippet(e.span)
513                             .unwrap_or_else(|_| pprust::expr_to_string(&e))
514                     };
515                     err.span_suggestion(
516                         inner_op.span.to(outer_op.span),
517                         "split the comparison into two...",
518                         format!(
519                             "{} {} {} && {} {}",
520                             expr_to_str(&l1),
521                             op.node.to_string(),
522                             expr_to_str(&r1),
523                             expr_to_str(&r1),
524                             outer_op.node.to_ast_binop().unwrap().to_string(),
525                         ),
526                         Applicability::MaybeIncorrect,
527                     );
528                     err.span_suggestion(
529                         inner_op.span.to(outer_op.span),
530                         "...or parenthesize one of the comparisons",
531                         format!(
532                             "({} {} {}) {}",
533                             expr_to_str(&l1),
534                             op.node.to_string(),
535                             expr_to_str(&r1),
536                             outer_op.node.to_ast_binop().unwrap().to_string(),
537                         ),
538                         Applicability::MaybeIncorrect,
539                     );
540                 }
541                 _ => {}
542             }
543         }
544     }
545
546     /// Produces an error if comparison operators are chained (RFC #558).
547     /// We only need to check the LHS, not the RHS, because all comparison ops have same
548     /// precedence (see `fn precedence`) and are left-associative (see `fn fixity`).
549     ///
550     /// This can also be hit if someone incorrectly writes `foo<bar>()` when they should have used
551     /// the turbofish (`foo::<bar>()`) syntax. We attempt some heuristic recovery if that is the
552     /// case.
553     ///
554     /// Keep in mind that given that `outer_op.is_comparison()` holds and comparison ops are left
555     /// associative we can infer that we have:
556     ///
557     ///           outer_op
558     ///           /   \
559     ///     inner_op   r2
560     ///        /  \
561     ///      l1    r1
562     pub(super) fn check_no_chained_comparison(
563         &mut self,
564         inner_op: &Expr,
565         outer_op: &Spanned<AssocOp>,
566     ) -> PResult<'a, Option<P<Expr>>> {
567         debug_assert!(
568             outer_op.node.is_comparison(),
569             "check_no_chained_comparison: {:?} is not comparison",
570             outer_op.node,
571         );
572
573         let mk_err_expr =
574             |this: &Self, span| Ok(Some(this.mk_expr(span, ExprKind::Err, AttrVec::new())));
575
576         match inner_op.kind {
577             ExprKind::Binary(op, _, _) if op.node.is_comparison() => {
578                 // Respan to include both operators.
579                 let op_span = op.span.to(self.prev_span);
580                 let mut err =
581                     self.struct_span_err(op_span, "comparison operators cannot be chained");
582
583                 // If it looks like a genuine attempt to chain operators (as opposed to a
584                 // misformatted turbofish, for instance), suggest a correct form.
585                 self.attempt_chained_comparison_suggestion(&mut err, inner_op, outer_op);
586
587                 let suggest = |err: &mut DiagnosticBuilder<'_>| {
588                     err.span_suggestion_verbose(
589                         op_span.shrink_to_lo(),
590                         TURBOFISH,
591                         "::".to_string(),
592                         Applicability::MaybeIncorrect,
593                     );
594                 };
595
596                 if op.node == BinOpKind::Lt &&
597                     outer_op.node == AssocOp::Less ||  // Include `<` to provide this recommendation
598                     outer_op.node == AssocOp::Greater
599                 // even in a case like the following:
600                 {
601                     //     Foo<Bar<Baz<Qux, ()>>>
602                     if outer_op.node == AssocOp::Less {
603                         let snapshot = self.clone();
604                         self.bump();
605                         // So far we have parsed `foo<bar<`, consume the rest of the type args.
606                         let modifiers =
607                             [(token::Lt, 1), (token::Gt, -1), (token::BinOp(token::Shr), -2)];
608                         self.consume_tts(1, &modifiers[..]);
609
610                         if !&[token::OpenDelim(token::Paren), token::ModSep]
611                             .contains(&self.token.kind)
612                         {
613                             // We don't have `foo< bar >(` or `foo< bar >::`, so we rewind the
614                             // parser and bail out.
615                             mem::replace(self, snapshot.clone());
616                         }
617                     }
618                     return if token::ModSep == self.token.kind {
619                         // We have some certainty that this was a bad turbofish at this point.
620                         // `foo< bar >::`
621                         suggest(&mut err);
622
623                         let snapshot = self.clone();
624                         self.bump(); // `::`
625
626                         // Consume the rest of the likely `foo<bar>::new()` or return at `foo<bar>`.
627                         match self.parse_expr() {
628                             Ok(_) => {
629                                 // 99% certain that the suggestion is correct, continue parsing.
630                                 err.emit();
631                                 // FIXME: actually check that the two expressions in the binop are
632                                 // paths and resynthesize new fn call expression instead of using
633                                 // `ExprKind::Err` placeholder.
634                                 mk_err_expr(self, inner_op.span.to(self.prev_span))
635                             }
636                             Err(mut expr_err) => {
637                                 expr_err.cancel();
638                                 // Not entirely sure now, but we bubble the error up with the
639                                 // suggestion.
640                                 mem::replace(self, snapshot);
641                                 Err(err)
642                             }
643                         }
644                     } else if token::OpenDelim(token::Paren) == self.token.kind {
645                         // We have high certainty that this was a bad turbofish at this point.
646                         // `foo< bar >(`
647                         suggest(&mut err);
648                         // Consume the fn call arguments.
649                         match self.consume_fn_args() {
650                             Err(()) => Err(err),
651                             Ok(()) => {
652                                 err.emit();
653                                 // FIXME: actually check that the two expressions in the binop are
654                                 // paths and resynthesize new fn call expression instead of using
655                                 // `ExprKind::Err` placeholder.
656                                 mk_err_expr(self, inner_op.span.to(self.prev_span))
657                             }
658                         }
659                     } else {
660                         // All we know is that this is `foo < bar >` and *nothing* else. Try to
661                         // be helpful, but don't attempt to recover.
662                         err.help(TURBOFISH);
663                         err.help("or use `(...)` if you meant to specify fn arguments");
664                         // These cases cause too many knock-down errors, bail out (#61329).
665                         Err(err)
666                     };
667                 }
668                 err.emit();
669             }
670             _ => {}
671         }
672         Ok(None)
673     }
674
675     fn consume_fn_args(&mut self) -> Result<(), ()> {
676         let snapshot = self.clone();
677         self.bump(); // `(`
678
679         // Consume the fn call arguments.
680         let modifiers =
681             [(token::OpenDelim(token::Paren), 1), (token::CloseDelim(token::Paren), -1)];
682         self.consume_tts(1, &modifiers[..]);
683
684         if self.token.kind == token::Eof {
685             // Not entirely sure that what we consumed were fn arguments, rollback.
686             mem::replace(self, snapshot);
687             Err(())
688         } else {
689             // 99% certain that the suggestion is correct, continue parsing.
690             Ok(())
691         }
692     }
693
694     pub(super) fn maybe_report_ambiguous_plus(
695         &mut self,
696         allow_plus: bool,
697         impl_dyn_multi: bool,
698         ty: &Ty,
699     ) {
700         if !allow_plus && impl_dyn_multi {
701             let sum_with_parens = format!("({})", pprust::ty_to_string(&ty));
702             self.struct_span_err(ty.span, "ambiguous `+` in a type")
703                 .span_suggestion(
704                     ty.span,
705                     "use parentheses to disambiguate",
706                     sum_with_parens,
707                     Applicability::MachineApplicable,
708                 )
709                 .emit();
710         }
711     }
712
713     pub(super) fn maybe_recover_from_bad_type_plus(
714         &mut self,
715         allow_plus: bool,
716         ty: &Ty,
717     ) -> PResult<'a, ()> {
718         // Do not add `+` to expected tokens.
719         if !allow_plus || !self.token.is_like_plus() {
720             return Ok(());
721         }
722
723         self.bump(); // `+`
724         let bounds = self.parse_generic_bounds(None)?;
725         let sum_span = ty.span.to(self.prev_span);
726
727         let mut err = struct_span_err!(
728             self.sess.span_diagnostic,
729             sum_span,
730             E0178,
731             "expected a path on the left-hand side of `+`, not `{}`",
732             pprust::ty_to_string(ty)
733         );
734
735         match ty.kind {
736             TyKind::Rptr(ref lifetime, ref mut_ty) => {
737                 let sum_with_parens = pprust::to_string(|s| {
738                     s.s.word("&");
739                     s.print_opt_lifetime(lifetime);
740                     s.print_mutability(mut_ty.mutbl, false);
741                     s.popen();
742                     s.print_type(&mut_ty.ty);
743                     s.print_type_bounds(" +", &bounds);
744                     s.pclose()
745                 });
746                 err.span_suggestion(
747                     sum_span,
748                     "try adding parentheses",
749                     sum_with_parens,
750                     Applicability::MachineApplicable,
751                 );
752             }
753             TyKind::Ptr(..) | TyKind::BareFn(..) => {
754                 err.span_label(sum_span, "perhaps you forgot parentheses?");
755             }
756             _ => {
757                 err.span_label(sum_span, "expected a path");
758             }
759         }
760         err.emit();
761         Ok(())
762     }
763
764     /// Tries to recover from associated item paths like `[T]::AssocItem` / `(T, U)::AssocItem`.
765     /// Attempts to convert the base expression/pattern/type into a type, parses the `::AssocItem`
766     /// tail, and combines them into a `<Ty>::AssocItem` expression/pattern/type.
767     pub(super) fn maybe_recover_from_bad_qpath<T: RecoverQPath>(
768         &mut self,
769         base: P<T>,
770         allow_recovery: bool,
771     ) -> PResult<'a, P<T>> {
772         // Do not add `::` to expected tokens.
773         if allow_recovery && self.token == token::ModSep {
774             if let Some(ty) = base.to_ty() {
775                 return self.maybe_recover_from_bad_qpath_stage_2(ty.span, ty);
776             }
777         }
778         Ok(base)
779     }
780
781     /// Given an already parsed `Ty`, parses the `::AssocItem` tail and
782     /// combines them into a `<Ty>::AssocItem` expression/pattern/type.
783     pub(super) fn maybe_recover_from_bad_qpath_stage_2<T: RecoverQPath>(
784         &mut self,
785         ty_span: Span,
786         ty: P<Ty>,
787     ) -> PResult<'a, P<T>> {
788         self.expect(&token::ModSep)?;
789
790         let mut path = ast::Path { segments: Vec::new(), span: DUMMY_SP };
791         self.parse_path_segments(&mut path.segments, T::PATH_STYLE)?;
792         path.span = ty_span.to(self.prev_span);
793
794         let ty_str = self.span_to_snippet(ty_span).unwrap_or_else(|_| pprust::ty_to_string(&ty));
795         self.struct_span_err(path.span, "missing angle brackets in associated item path")
796             .span_suggestion(
797                 // This is a best-effort recovery.
798                 path.span,
799                 "try",
800                 format!("<{}>::{}", ty_str, pprust::path_to_string(&path)),
801                 Applicability::MaybeIncorrect,
802             )
803             .emit();
804
805         let path_span = ty_span.shrink_to_hi(); // Use an empty path since `position == 0`.
806         Ok(P(T::recovered(Some(QSelf { ty, path_span, position: 0 }), path)))
807     }
808
809     pub(super) fn maybe_consume_incorrect_semicolon(&mut self, items: &[P<Item>]) -> bool {
810         if self.eat(&token::Semi) {
811             let mut err = self.struct_span_err(self.prev_span, "expected item, found `;`");
812             err.span_suggestion_short(
813                 self.prev_span,
814                 "remove this semicolon",
815                 String::new(),
816                 Applicability::MachineApplicable,
817             );
818             if !items.is_empty() {
819                 let previous_item = &items[items.len() - 1];
820                 let previous_item_kind_name = match previous_item.kind {
821                     // Say "braced struct" because tuple-structs and
822                     // braceless-empty-struct declarations do take a semicolon.
823                     ItemKind::Struct(..) => Some("braced struct"),
824                     ItemKind::Enum(..) => Some("enum"),
825                     ItemKind::Trait(..) => Some("trait"),
826                     ItemKind::Union(..) => Some("union"),
827                     _ => None,
828                 };
829                 if let Some(name) = previous_item_kind_name {
830                     err.help(&format!("{} declarations are not followed by a semicolon", name));
831                 }
832             }
833             err.emit();
834             true
835         } else {
836             false
837         }
838     }
839
840     /// Creates a `DiagnosticBuilder` for an unexpected token `t` and tries to recover if it is a
841     /// closing delimiter.
842     pub(super) fn unexpected_try_recover(
843         &mut self,
844         t: &TokenKind,
845     ) -> PResult<'a, bool /* recovered */> {
846         let token_str = pprust::token_kind_to_string(t);
847         let this_token_str = super::token_descr(&self.token);
848         let (prev_sp, sp) = match (&self.token.kind, self.subparser_name) {
849             // Point at the end of the macro call when reaching end of macro arguments.
850             (token::Eof, Some(_)) => {
851                 let sp = self.sess.source_map().next_point(self.token.span);
852                 (sp, sp)
853             }
854             // We don't want to point at the following span after DUMMY_SP.
855             // This happens when the parser finds an empty TokenStream.
856             _ if self.prev_span == DUMMY_SP => (self.token.span, self.token.span),
857             // EOF, don't want to point at the following char, but rather the last token.
858             (token::Eof, None) => (self.prev_span, self.token.span),
859             _ => (self.prev_span.shrink_to_hi(), self.token.span),
860         };
861         let msg = format!(
862             "expected `{}`, found {}",
863             token_str,
864             match (&self.token.kind, self.subparser_name) {
865                 (token::Eof, Some(origin)) => format!("end of {}", origin),
866                 _ => this_token_str,
867             },
868         );
869         let mut err = self.struct_span_err(sp, &msg);
870         let label_exp = format!("expected `{}`", token_str);
871         match self.recover_closing_delimiter(&[t.clone()], err) {
872             Err(e) => err = e,
873             Ok(recovered) => {
874                 return Ok(recovered);
875             }
876         }
877         let sm = self.sess.source_map();
878         if !sm.is_multiline(prev_sp.until(sp)) {
879             // When the spans are in the same line, it means that the only content
880             // between them is whitespace, point only at the found token.
881             err.span_label(sp, label_exp);
882         } else {
883             err.span_label(prev_sp, label_exp);
884             err.span_label(sp, "unexpected token");
885         }
886         Err(err)
887     }
888
889     pub(super) fn expect_semi(&mut self) -> PResult<'a, ()> {
890         if self.eat(&token::Semi) {
891             return Ok(());
892         }
893         let sm = self.sess.source_map();
894         let msg = format!("expected `;`, found `{}`", super::token_descr(&self.token));
895         let appl = Applicability::MachineApplicable;
896         if self.token.span == DUMMY_SP || self.prev_span == DUMMY_SP {
897             // Likely inside a macro, can't provide meaninful suggestions.
898             return self.expect(&token::Semi).map(drop);
899         } else if !sm.is_multiline(self.prev_span.until(self.token.span)) {
900             // The current token is in the same line as the prior token, not recoverable.
901         } else if self.look_ahead(1, |t| {
902             t == &token::CloseDelim(token::Brace)
903                 || token_can_begin_expr(t) && t.kind != token::Colon
904         }) && [token::Comma, token::Colon].contains(&self.token.kind)
905         {
906             // Likely typo: `,` â†’ `;` or `:` â†’ `;`. This is triggered if the current token is
907             // either `,` or `:`, and the next token could either start a new statement or is a
908             // block close. For example:
909             //
910             //   let x = 32:
911             //   let y = 42;
912             self.bump();
913             let sp = self.prev_span;
914             self.struct_span_err(sp, &msg)
915                 .span_suggestion(sp, "change this to `;`", ";".to_string(), appl)
916                 .emit();
917             return Ok(());
918         } else if self.look_ahead(0, |t| {
919             t == &token::CloseDelim(token::Brace)
920                 || (
921                     token_can_begin_expr(t) && t != &token::Semi && t != &token::Pound
922                     // Avoid triggering with too many trailing `#` in raw string.
923                 )
924         }) {
925             // Missing semicolon typo. This is triggered if the next token could either start a
926             // new statement or is a block close. For example:
927             //
928             //   let x = 32
929             //   let y = 42;
930             let sp = self.prev_span.shrink_to_hi();
931             self.struct_span_err(sp, &msg)
932                 .span_label(self.token.span, "unexpected token")
933                 .span_suggestion_short(sp, "add `;` here", ";".to_string(), appl)
934                 .emit();
935             return Ok(());
936         }
937         self.expect(&token::Semi).map(drop) // Error unconditionally
938     }
939
940     pub(super) fn parse_semi_or_incorrect_foreign_fn_body(
941         &mut self,
942         ident: &Ident,
943         extern_sp: Span,
944     ) -> PResult<'a, ()> {
945         if self.token != token::Semi {
946             // This might be an incorrect fn definition (#62109).
947             let parser_snapshot = self.clone();
948             match self.parse_inner_attrs_and_block() {
949                 Ok((_, body)) => {
950                     self.struct_span_err(ident.span, "incorrect `fn` inside `extern` block")
951                         .span_label(ident.span, "can't have a body")
952                         .span_label(body.span, "this body is invalid here")
953                         .span_label(
954                             extern_sp,
955                             "`extern` blocks define existing foreign functions and `fn`s \
956                              inside of them cannot have a body",
957                         )
958                         .help(
959                             "you might have meant to write a function accessible through ffi, \
960                                which can be done by writing `extern fn` outside of the \
961                                `extern` block",
962                         )
963                         .note(
964                             "for more information, visit \
965                                https://doc.rust-lang.org/std/keyword.extern.html",
966                         )
967                         .emit();
968                 }
969                 Err(mut err) => {
970                     err.cancel();
971                     mem::replace(self, parser_snapshot);
972                     self.expect_semi()?;
973                 }
974             }
975         } else {
976             self.bump();
977         }
978         Ok(())
979     }
980
981     /// Consumes alternative await syntaxes like `await!(<expr>)`, `await <expr>`,
982     /// `await? <expr>`, `await(<expr>)`, and `await { <expr> }`.
983     pub(super) fn recover_incorrect_await_syntax(
984         &mut self,
985         lo: Span,
986         await_sp: Span,
987         attrs: AttrVec,
988     ) -> PResult<'a, P<Expr>> {
989         let (hi, expr, is_question) = if self.token == token::Not {
990             // Handle `await!(<expr>)`.
991             self.recover_await_macro()?
992         } else {
993             self.recover_await_prefix(await_sp)?
994         };
995         let sp = self.error_on_incorrect_await(lo, hi, &expr, is_question);
996         let expr = self.mk_expr(lo.to(sp), ExprKind::Await(expr), attrs);
997         self.maybe_recover_from_bad_qpath(expr, true)
998     }
999
1000     fn recover_await_macro(&mut self) -> PResult<'a, (Span, P<Expr>, bool)> {
1001         self.expect(&token::Not)?;
1002         self.expect(&token::OpenDelim(token::Paren))?;
1003         let expr = self.parse_expr()?;
1004         self.expect(&token::CloseDelim(token::Paren))?;
1005         Ok((self.prev_span, expr, false))
1006     }
1007
1008     fn recover_await_prefix(&mut self, await_sp: Span) -> PResult<'a, (Span, P<Expr>, bool)> {
1009         let is_question = self.eat(&token::Question); // Handle `await? <expr>`.
1010         let expr = if self.token == token::OpenDelim(token::Brace) {
1011             // Handle `await { <expr> }`.
1012             // This needs to be handled separatedly from the next arm to avoid
1013             // interpreting `await { <expr> }?` as `<expr>?.await`.
1014             self.parse_block_expr(None, self.token.span, BlockCheckMode::Default, AttrVec::new())
1015         } else {
1016             self.parse_expr()
1017         }
1018         .map_err(|mut err| {
1019             err.span_label(await_sp, "while parsing this incorrect await expression");
1020             err
1021         })?;
1022         Ok((expr.span, expr, is_question))
1023     }
1024
1025     fn error_on_incorrect_await(&self, lo: Span, hi: Span, expr: &Expr, is_question: bool) -> Span {
1026         let expr_str =
1027             self.span_to_snippet(expr.span).unwrap_or_else(|_| pprust::expr_to_string(&expr));
1028         let suggestion = format!("{}.await{}", expr_str, if is_question { "?" } else { "" });
1029         let sp = lo.to(hi);
1030         let app = match expr.kind {
1031             ExprKind::Try(_) => Applicability::MaybeIncorrect, // `await <expr>?`
1032             _ => Applicability::MachineApplicable,
1033         };
1034         self.struct_span_err(sp, "incorrect use of `await`")
1035             .span_suggestion(sp, "`await` is a postfix operation", suggestion, app)
1036             .emit();
1037         sp
1038     }
1039
1040     /// If encountering `future.await()`, consumes and emits an error.
1041     pub(super) fn recover_from_await_method_call(&mut self) {
1042         if self.token == token::OpenDelim(token::Paren)
1043             && self.look_ahead(1, |t| t == &token::CloseDelim(token::Paren))
1044         {
1045             // future.await()
1046             let lo = self.token.span;
1047             self.bump(); // (
1048             let sp = lo.to(self.token.span);
1049             self.bump(); // )
1050             self.struct_span_err(sp, "incorrect use of `await`")
1051                 .span_suggestion(
1052                     sp,
1053                     "`await` is not a method call, remove the parentheses",
1054                     String::new(),
1055                     Applicability::MachineApplicable,
1056                 )
1057                 .emit()
1058         }
1059     }
1060
1061     /// Recovers a situation like `for ( $pat in $expr )`
1062     /// and suggest writing `for $pat in $expr` instead.
1063     ///
1064     /// This should be called before parsing the `$block`.
1065     pub(super) fn recover_parens_around_for_head(
1066         &mut self,
1067         pat: P<Pat>,
1068         expr: &Expr,
1069         begin_paren: Option<Span>,
1070     ) -> P<Pat> {
1071         match (&self.token.kind, begin_paren) {
1072             (token::CloseDelim(token::Paren), Some(begin_par_sp)) => {
1073                 self.bump();
1074
1075                 let pat_str = self
1076                     // Remove the `(` from the span of the pattern:
1077                     .span_to_snippet(pat.span.trim_start(begin_par_sp).unwrap())
1078                     .unwrap_or_else(|_| pprust::pat_to_string(&pat));
1079
1080                 self.struct_span_err(self.prev_span, "unexpected closing `)`")
1081                     .span_label(begin_par_sp, "opening `(`")
1082                     .span_suggestion(
1083                         begin_par_sp.to(self.prev_span),
1084                         "remove parenthesis in `for` loop",
1085                         format!("{} in {}", pat_str, pprust::expr_to_string(&expr)),
1086                         // With e.g. `for (x) in y)` this would replace `(x) in y)`
1087                         // with `x) in y)` which is syntactically invalid.
1088                         // However, this is prevented before we get here.
1089                         Applicability::MachineApplicable,
1090                     )
1091                     .emit();
1092
1093                 // Unwrap `(pat)` into `pat` to avoid the `unused_parens` lint.
1094                 pat.and_then(|pat| match pat.kind {
1095                     PatKind::Paren(pat) => pat,
1096                     _ => P(pat),
1097                 })
1098             }
1099             _ => pat,
1100         }
1101     }
1102
1103     pub(super) fn could_ascription_be_path(&self, node: &ast::ExprKind) -> bool {
1104         (self.token == token::Lt && // `foo:<bar`, likely a typoed turbofish.
1105             self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident()))
1106             || self.token.is_ident() &&
1107             match node {
1108                 // `foo::` â†’ `foo:` or `foo.bar::` â†’ `foo.bar:`
1109                 ast::ExprKind::Path(..) | ast::ExprKind::Field(..) => true,
1110                 _ => false,
1111             } &&
1112             !self.token.is_reserved_ident() &&           // v `foo:bar(baz)`
1113             self.look_ahead(1, |t| t == &token::OpenDelim(token::Paren))
1114             || self.look_ahead(1, |t| t == &token::Lt) &&     // `foo:bar<baz`
1115             self.look_ahead(2, |t| t.is_ident())
1116             || self.look_ahead(1, |t| t == &token::Colon) &&  // `foo:bar:baz`
1117             self.look_ahead(2, |t| t.is_ident())
1118             || self.look_ahead(1, |t| t == &token::ModSep)
1119                 && (self.look_ahead(2, |t| t.is_ident()) ||   // `foo:bar::baz`
1120              self.look_ahead(2, |t| t == &token::Lt)) // `foo:bar::<baz>`
1121     }
1122
1123     pub(super) fn recover_seq_parse_error(
1124         &mut self,
1125         delim: token::DelimToken,
1126         lo: Span,
1127         result: PResult<'a, P<Expr>>,
1128     ) -> P<Expr> {
1129         match result {
1130             Ok(x) => x,
1131             Err(mut err) => {
1132                 err.emit();
1133                 // Recover from parse error, callers expect the closing delim to be consumed.
1134                 self.consume_block(delim, ConsumeClosingDelim::Yes);
1135                 self.mk_expr(lo.to(self.prev_span), ExprKind::Err, AttrVec::new())
1136             }
1137         }
1138     }
1139
1140     pub(super) fn recover_closing_delimiter(
1141         &mut self,
1142         tokens: &[TokenKind],
1143         mut err: DiagnosticBuilder<'a>,
1144     ) -> PResult<'a, bool> {
1145         let mut pos = None;
1146         // We want to use the last closing delim that would apply.
1147         for (i, unmatched) in self.unclosed_delims.iter().enumerate().rev() {
1148             if tokens.contains(&token::CloseDelim(unmatched.expected_delim))
1149                 && Some(self.token.span) > unmatched.unclosed_span
1150             {
1151                 pos = Some(i);
1152             }
1153         }
1154         match pos {
1155             Some(pos) => {
1156                 // Recover and assume that the detected unclosed delimiter was meant for
1157                 // this location. Emit the diagnostic and act as if the delimiter was
1158                 // present for the parser's sake.
1159
1160                 // Don't attempt to recover from this unclosed delimiter more than once.
1161                 let unmatched = self.unclosed_delims.remove(pos);
1162                 let delim = TokenType::Token(token::CloseDelim(unmatched.expected_delim));
1163                 if unmatched.found_delim.is_none() {
1164                     // We encountered `Eof`, set this fact here to avoid complaining about missing
1165                     // `fn main()` when we found place to suggest the closing brace.
1166                     *self.sess.reached_eof.borrow_mut() = true;
1167                 }
1168
1169                 // We want to suggest the inclusion of the closing delimiter where it makes
1170                 // the most sense, which is immediately after the last token:
1171                 //
1172                 //  {foo(bar {}}
1173                 //      -      ^
1174                 //      |      |
1175                 //      |      help: `)` may belong here
1176                 //      |
1177                 //      unclosed delimiter
1178                 if let Some(sp) = unmatched.unclosed_span {
1179                     err.span_label(sp, "unclosed delimiter");
1180                 }
1181                 err.span_suggestion_short(
1182                     self.prev_span.shrink_to_hi(),
1183                     &format!("{} may belong here", delim.to_string()),
1184                     delim.to_string(),
1185                     Applicability::MaybeIncorrect,
1186                 );
1187                 if unmatched.found_delim.is_none() {
1188                     // Encountered `Eof` when lexing blocks. Do not recover here to avoid knockdown
1189                     // errors which would be emitted elsewhere in the parser and let other error
1190                     // recovery consume the rest of the file.
1191                     Err(err)
1192                 } else {
1193                     err.emit();
1194                     self.expected_tokens.clear(); // Reduce the number of errors.
1195                     Ok(true)
1196                 }
1197             }
1198             _ => Err(err),
1199         }
1200     }
1201
1202     /// Eats tokens until we can be relatively sure we reached the end of the
1203     /// statement. This is something of a best-effort heuristic.
1204     ///
1205     /// We terminate when we find an unmatched `}` (without consuming it).
1206     pub(super) fn recover_stmt(&mut self) {
1207         self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore)
1208     }
1209
1210     /// If `break_on_semi` is `Break`, then we will stop consuming tokens after
1211     /// finding (and consuming) a `;` outside of `{}` or `[]` (note that this is
1212     /// approximate -- it can mean we break too early due to macros, but that
1213     /// should only lead to sub-optimal recovery, not inaccurate parsing).
1214     ///
1215     /// If `break_on_block` is `Break`, then we will stop consuming tokens
1216     /// after finding (and consuming) a brace-delimited block.
1217     pub(super) fn recover_stmt_(
1218         &mut self,
1219         break_on_semi: SemiColonMode,
1220         break_on_block: BlockMode,
1221     ) {
1222         let mut brace_depth = 0;
1223         let mut bracket_depth = 0;
1224         let mut in_block = false;
1225         debug!("recover_stmt_ enter loop (semi={:?}, block={:?})", break_on_semi, break_on_block);
1226         loop {
1227             debug!("recover_stmt_ loop {:?}", self.token);
1228             match self.token.kind {
1229                 token::OpenDelim(token::DelimToken::Brace) => {
1230                     brace_depth += 1;
1231                     self.bump();
1232                     if break_on_block == BlockMode::Break && brace_depth == 1 && bracket_depth == 0
1233                     {
1234                         in_block = true;
1235                     }
1236                 }
1237                 token::OpenDelim(token::DelimToken::Bracket) => {
1238                     bracket_depth += 1;
1239                     self.bump();
1240                 }
1241                 token::CloseDelim(token::DelimToken::Brace) => {
1242                     if brace_depth == 0 {
1243                         debug!("recover_stmt_ return - close delim {:?}", self.token);
1244                         break;
1245                     }
1246                     brace_depth -= 1;
1247                     self.bump();
1248                     if in_block && bracket_depth == 0 && brace_depth == 0 {
1249                         debug!("recover_stmt_ return - block end {:?}", self.token);
1250                         break;
1251                     }
1252                 }
1253                 token::CloseDelim(token::DelimToken::Bracket) => {
1254                     bracket_depth -= 1;
1255                     if bracket_depth < 0 {
1256                         bracket_depth = 0;
1257                     }
1258                     self.bump();
1259                 }
1260                 token::Eof => {
1261                     debug!("recover_stmt_ return - Eof");
1262                     break;
1263                 }
1264                 token::Semi => {
1265                     self.bump();
1266                     if break_on_semi == SemiColonMode::Break
1267                         && brace_depth == 0
1268                         && bracket_depth == 0
1269                     {
1270                         debug!("recover_stmt_ return - Semi");
1271                         break;
1272                     }
1273                 }
1274                 token::Comma
1275                     if break_on_semi == SemiColonMode::Comma
1276                         && brace_depth == 0
1277                         && bracket_depth == 0 =>
1278                 {
1279                     debug!("recover_stmt_ return - Semi");
1280                     break;
1281                 }
1282                 _ => self.bump(),
1283             }
1284         }
1285     }
1286
1287     pub(super) fn check_for_for_in_in_typo(&mut self, in_span: Span) {
1288         if self.eat_keyword(kw::In) {
1289             // a common typo: `for _ in in bar {}`
1290             self.struct_span_err(self.prev_span, "expected iterable, found keyword `in`")
1291                 .span_suggestion_short(
1292                     in_span.until(self.prev_span),
1293                     "remove the duplicated `in`",
1294                     String::new(),
1295                     Applicability::MachineApplicable,
1296                 )
1297                 .emit();
1298         }
1299     }
1300
1301     pub(super) fn expected_semi_or_open_brace<T>(&mut self) -> PResult<'a, T> {
1302         let token_str = super::token_descr(&self.token);
1303         let msg = &format!("expected `;` or `{{`, found {}", token_str);
1304         let mut err = self.struct_span_err(self.token.span, msg);
1305         err.span_label(self.token.span, "expected `;` or `{`");
1306         Err(err)
1307     }
1308
1309     pub(super) fn eat_incorrect_doc_comment_for_param_type(&mut self) {
1310         if let token::DocComment(_) = self.token.kind {
1311             self.struct_span_err(
1312                 self.token.span,
1313                 "documentation comments cannot be applied to a function parameter's type",
1314             )
1315             .span_label(self.token.span, "doc comments are not allowed here")
1316             .emit();
1317             self.bump();
1318         } else if self.token == token::Pound
1319             && self.look_ahead(1, |t| *t == token::OpenDelim(token::Bracket))
1320         {
1321             let lo = self.token.span;
1322             // Skip every token until next possible arg.
1323             while self.token != token::CloseDelim(token::Bracket) {
1324                 self.bump();
1325             }
1326             let sp = lo.to(self.token.span);
1327             self.bump();
1328             self.struct_span_err(sp, "attributes cannot be applied to a function parameter's type")
1329                 .span_label(sp, "attributes are not allowed here")
1330                 .emit();
1331         }
1332     }
1333
1334     pub(super) fn parameter_without_type(
1335         &mut self,
1336         err: &mut DiagnosticBuilder<'_>,
1337         pat: P<ast::Pat>,
1338         require_name: bool,
1339         first_param: bool,
1340     ) -> Option<Ident> {
1341         // If we find a pattern followed by an identifier, it could be an (incorrect)
1342         // C-style parameter declaration.
1343         if self.check_ident()
1344             && self.look_ahead(1, |t| *t == token::Comma || *t == token::CloseDelim(token::Paren))
1345         {
1346             // `fn foo(String s) {}`
1347             let ident = self.parse_ident().unwrap();
1348             let span = pat.span.with_hi(ident.span.hi());
1349
1350             err.span_suggestion(
1351                 span,
1352                 "declare the type after the parameter binding",
1353                 String::from("<identifier>: <type>"),
1354                 Applicability::HasPlaceholders,
1355             );
1356             return Some(ident);
1357         } else if let PatKind::Ident(_, ident, _) = pat.kind {
1358             if require_name
1359                 && (self.token == token::Comma
1360                     || self.token == token::Lt
1361                     || self.token == token::CloseDelim(token::Paren))
1362             {
1363                 // `fn foo(a, b) {}`, `fn foo(a<x>, b<y>) {}` or `fn foo(usize, usize) {}`
1364                 if first_param {
1365                     err.span_suggestion(
1366                         pat.span,
1367                         "if this is a `self` type, give it a parameter name",
1368                         format!("self: {}", ident),
1369                         Applicability::MaybeIncorrect,
1370                     );
1371                 }
1372                 // Avoid suggesting that `fn foo(HashMap<u32>)` is fixed with a change to
1373                 // `fn foo(HashMap: TypeName<u32>)`.
1374                 if self.token != token::Lt {
1375                     err.span_suggestion(
1376                         pat.span,
1377                         "if this was a parameter name, give it a type",
1378                         format!("{}: TypeName", ident),
1379                         Applicability::HasPlaceholders,
1380                     );
1381                 }
1382                 err.span_suggestion(
1383                     pat.span,
1384                     "if this is a type, explicitly ignore the parameter name",
1385                     format!("_: {}", ident),
1386                     Applicability::MachineApplicable,
1387                 );
1388                 err.note("anonymous parameters are removed in the 2018 edition (see RFC 1685)");
1389
1390                 // Don't attempt to recover by using the `X` in `X<Y>` as the parameter name.
1391                 return if self.token == token::Lt { None } else { Some(ident) };
1392             }
1393         }
1394         None
1395     }
1396
1397     pub(super) fn recover_arg_parse(&mut self) -> PResult<'a, (P<ast::Pat>, P<ast::Ty>)> {
1398         let pat = self.parse_pat(Some("argument name"))?;
1399         self.expect(&token::Colon)?;
1400         let ty = self.parse_ty()?;
1401
1402         struct_span_err!(
1403             self.diagnostic(),
1404             pat.span,
1405             E0642,
1406             "patterns aren't allowed in methods without bodies",
1407         )
1408         .span_suggestion_short(
1409             pat.span,
1410             "give this argument a name or use an underscore to ignore it",
1411             "_".to_owned(),
1412             Applicability::MachineApplicable,
1413         )
1414         .emit();
1415
1416         // Pretend the pattern is `_`, to avoid duplicate errors from AST validation.
1417         let pat = P(Pat { kind: PatKind::Wild, span: pat.span, id: ast::DUMMY_NODE_ID });
1418         Ok((pat, ty))
1419     }
1420
1421     pub(super) fn recover_bad_self_param(&mut self, mut param: Param) -> PResult<'a, Param> {
1422         let sp = param.pat.span;
1423         param.ty.kind = TyKind::Err;
1424         self.struct_span_err(sp, "unexpected `self` parameter in function")
1425             .span_label(sp, "must be the first parameter of an associated function")
1426             .emit();
1427         Ok(param)
1428     }
1429
1430     pub(super) fn consume_block(
1431         &mut self,
1432         delim: token::DelimToken,
1433         consume_close: ConsumeClosingDelim,
1434     ) {
1435         let mut brace_depth = 0;
1436         loop {
1437             if self.eat(&token::OpenDelim(delim)) {
1438                 brace_depth += 1;
1439             } else if self.check(&token::CloseDelim(delim)) {
1440                 if brace_depth == 0 {
1441                     if let ConsumeClosingDelim::Yes = consume_close {
1442                         // Some of the callers of this method expect to be able to parse the
1443                         // closing delimiter themselves, so we leave it alone. Otherwise we advance
1444                         // the parser.
1445                         self.bump();
1446                     }
1447                     return;
1448                 } else {
1449                     self.bump();
1450                     brace_depth -= 1;
1451                     continue;
1452                 }
1453             } else if self.token == token::Eof || self.eat(&token::CloseDelim(token::NoDelim)) {
1454                 return;
1455             } else {
1456                 self.bump();
1457             }
1458         }
1459     }
1460
1461     pub(super) fn expected_expression_found(&self) -> DiagnosticBuilder<'a> {
1462         let (span, msg) = match (&self.token.kind, self.subparser_name) {
1463             (&token::Eof, Some(origin)) => {
1464                 let sp = self.sess.source_map().next_point(self.token.span);
1465                 (sp, format!("expected expression, found end of {}", origin))
1466             }
1467             _ => (
1468                 self.token.span,
1469                 format!("expected expression, found {}", super::token_descr(&self.token),),
1470             ),
1471         };
1472         let mut err = self.struct_span_err(span, &msg);
1473         let sp = self.sess.source_map().start_point(self.token.span);
1474         if let Some(sp) = self.sess.ambiguous_block_expr_parse.borrow().get(&sp) {
1475             self.sess.expr_parentheses_needed(&mut err, *sp, None);
1476         }
1477         err.span_label(span, "expected expression");
1478         err
1479     }
1480
1481     fn consume_tts(
1482         &mut self,
1483         mut acc: i64, // `i64` because malformed code can have more closing delims than opening.
1484         // Not using `FxHashMap` due to `token::TokenKind: !Eq + !Hash`.
1485         modifier: &[(token::TokenKind, i64)],
1486     ) {
1487         while acc > 0 {
1488             if let Some((_, val)) = modifier.iter().find(|(t, _)| *t == self.token.kind) {
1489                 acc += *val;
1490             }
1491             if self.token.kind == token::Eof {
1492                 break;
1493             }
1494             self.bump();
1495         }
1496     }
1497
1498     /// Replace duplicated recovered parameters with `_` pattern to avoid unnecessary errors.
1499     ///
1500     /// This is necessary because at this point we don't know whether we parsed a function with
1501     /// anonymous parameters or a function with names but no types. In order to minimize
1502     /// unnecessary errors, we assume the parameters are in the shape of `fn foo(a, b, c)` where
1503     /// the parameters are *names* (so we don't emit errors about not being able to find `b` in
1504     /// the local scope), but if we find the same name multiple times, like in `fn foo(i8, i8)`,
1505     /// we deduplicate them to not complain about duplicated parameter names.
1506     pub(super) fn deduplicate_recovered_params_names(&self, fn_inputs: &mut Vec<Param>) {
1507         let mut seen_inputs = FxHashSet::default();
1508         for input in fn_inputs.iter_mut() {
1509             let opt_ident = if let (PatKind::Ident(_, ident, _), TyKind::Err) =
1510                 (&input.pat.kind, &input.ty.kind)
1511             {
1512                 Some(*ident)
1513             } else {
1514                 None
1515             };
1516             if let Some(ident) = opt_ident {
1517                 if seen_inputs.contains(&ident) {
1518                     input.pat.kind = PatKind::Wild;
1519                 }
1520                 seen_inputs.insert(ident);
1521             }
1522         }
1523     }
1524 }