|
1 | 1 | use super::{ForceCollect, Parser, TrailingToken};
|
2 | 2 |
|
3 | 3 | use rustc_ast::token;
|
4 |
| -use rustc_ast::{self as ast, AttrVec, GenericBounds, GenericParam, GenericParamKind, WhereClause}; |
| 4 | +use rustc_ast::{ |
| 5 | + self as ast, AttrVec, GenericBounds, GenericParam, GenericParamKind, TyKind, WhereClause, |
| 6 | +}; |
5 | 7 | use rustc_errors::{Applicability, PResult};
|
6 | 8 | use rustc_span::symbol::kw;
|
7 | 9 |
|
@@ -31,13 +33,43 @@ impl<'a> Parser<'a> {
|
31 | 33 | let mut colon_span = None;
|
32 | 34 | let bounds = if self.eat(&token::Colon) {
|
33 | 35 | colon_span = Some(self.prev_token.span);
|
| 36 | + // recover from `impl Trait` in type param bound |
| 37 | + if self.token.is_keyword(kw::Impl) { |
| 38 | + let impl_span = self.token.span; |
| 39 | + let snapshot = self.create_snapshot_for_diagnostic(); |
| 40 | + match self.parse_ty() { |
| 41 | + Ok(p) => { |
| 42 | + if let TyKind::ImplTrait(_, bounds) = &(*p).kind { |
| 43 | + let span = impl_span.to(self.token.span.shrink_to_lo()); |
| 44 | + let mut err = self.struct_span_err( |
| 45 | + span, |
| 46 | + "expected trait bound, found `impl Trait` type", |
| 47 | + ); |
| 48 | + err.span_label(span, "not a trait"); |
| 49 | + if let [bound, ..] = &bounds[..] { |
| 50 | + err.span_suggestion_verbose( |
| 51 | + impl_span.until(bound.span()), |
| 52 | + "use the trait bounds directly", |
| 53 | + String::new(), |
| 54 | + Applicability::MachineApplicable, |
| 55 | + ); |
| 56 | + } |
| 57 | + err.emit(); |
| 58 | + return Err(err); |
| 59 | + } |
| 60 | + } |
| 61 | + Err(err) => { |
| 62 | + err.cancel(); |
| 63 | + } |
| 64 | + } |
| 65 | + self.restore_snapshot(snapshot); |
| 66 | + } |
34 | 67 | self.parse_generic_bounds(colon_span)?
|
35 | 68 | } else {
|
36 | 69 | Vec::new()
|
37 | 70 | };
|
38 | 71 |
|
39 | 72 | let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
|
40 |
| - |
41 | 73 | Ok(GenericParam {
|
42 | 74 | ident,
|
43 | 75 | id: ast::DUMMY_NODE_ID,
|
|
0 commit comments