Skip to content

Commit

Permalink
Rollup merge of rust-lang#58872 - rep-nop:diagnostic-fix-56031, r=pet…
Browse files Browse the repository at this point in the history
…rochenkov

Adds help message in error for invalid `impl for T` syntax

Fixes rust-lang#56031.
  • Loading branch information
Centril authored Mar 10, 2019
2 parents cd755cf + e19b228 commit f847d3d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6743,7 +6743,15 @@ impl<'a> Parser<'a> {
};

// Parse both types and traits as a type, then reinterpret if necessary.
let ty_first = self.parse_ty()?;
let err_path = |span| ast::Path::from_ident(Ident::new(keywords::Invalid.name(), span));
let ty_first = if self.token.is_keyword(keywords::For) &&
self.look_ahead(1, |t| t != &token::Lt) {
let span = self.prev_span.between(self.span);
self.struct_span_err(span, "missing trait in a trait impl").emit();
P(Ty { node: TyKind::Path(None, err_path(span)), span, id: ast::DUMMY_NODE_ID })
} else {
self.parse_ty()?
};

// If `for` is missing we try to recover.
let has_for = self.eat_keyword(keywords::For);
Expand All @@ -6752,7 +6760,7 @@ impl<'a> Parser<'a> {
let ty_second = if self.token == token::DotDot {
// We need to report this error after `cfg` expansion for compatibility reasons
self.bump(); // `..`, do not add it to expected tokens
Some(P(Ty { node: TyKind::Err, span: self.prev_span, id: ast::DUMMY_NODE_ID }))
Some(DummyResult::raw_ty(self.prev_span, true))
} else if has_for || self.token.can_begin_type() {
Some(self.parse_ty()?)
} else {
Expand Down Expand Up @@ -6782,7 +6790,7 @@ impl<'a> Parser<'a> {
TyKind::Path(None, path) => path,
_ => {
self.span_err(ty_first.span, "expected a trait, found type");
ast::Path::from_ident(Ident::new(keywords::Invalid.name(), ty_first.span))
err_path(ty_first.span)
}
};
let trait_ref = TraitRef { path, ref_id: ty_first.id };
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/issues/issue-56031.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
struct T;

impl for T {}
//~^ ERROR missing trait in a trait impl

fn main() {}
8 changes: 8 additions & 0 deletions src/test/ui/issues/issue-56031.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: missing trait in a trait impl
--> $DIR/issue-56031.rs:3:5
|
LL | impl for T {}
| ^

error: aborting due to previous error

0 comments on commit f847d3d

Please sign in to comment.