Rollup merge of #88894 - FabianWolff:issue-88818, r=estebank

Improve error message for missing trait in trait impl

Fixes #88818. For the following example:
```rust
struct S { }
impl for S { }
```
the current output is:
```
error: missing trait in a trait impl
 --> t1.rs:2:5
  |
2 | impl for S { }
  |     ^
```
With my changes, I get:
```
error: missing trait in a trait impl
 --> t1.rs:2:5
  |
2 | impl for S { }
  |     ^
  |
help: add a trait here
  |
2 | impl Trait for S { }
  |      +++++
help: for an inherent impl, drop this `for`
  |
2 - impl for S { }
2 + impl S { }
  |
```
This commit is contained in:
Guillaume Gomez 2021-09-13 21:20:42 +02:00 committed by GitHub
commit 84d65fee0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 1 deletions

View File

@ -493,7 +493,20 @@ impl<'a> Parser<'a> {
let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
{
let span = self.prev_token.span.between(self.token.span);
self.struct_span_err(span, "missing trait in a trait impl").emit();
self.struct_span_err(span, "missing trait in a trait impl")
.span_suggestion(
span,
"add a trait here",
" Trait ".into(),
Applicability::HasPlaceholders,
)
.span_suggestion(
span.to(self.token.span),
"for an inherent impl, drop this `for`",
"".into(),
Applicability::MaybeIncorrect,
)
.emit();
P(Ty {
kind: TyKind::Path(None, err_path(span)),
span,

View File

@ -3,6 +3,16 @@ error: missing trait in a trait impl
|
LL | impl for T {}
| ^
|
help: add a trait here
|
LL | impl Trait for T {}
| +++++
help: for an inherent impl, drop this `for`
|
LL - impl for T {}
LL + impl T {}
|
error: aborting due to previous error

View File

@ -0,0 +1,10 @@
// Regression test for #88818 (improve error message for missing trait
// in `impl for X`).
struct S { }
impl for S { }
//~^ ERROR: missing trait in a trait impl
//~| HELP: add a trait here
//~| HELP: for an inherent impl, drop this `for`
fn main() {}

View File

@ -0,0 +1,18 @@
error: missing trait in a trait impl
--> $DIR/issue-88818.rs:5:5
|
LL | impl for S { }
| ^
|
help: add a trait here
|
LL | impl Trait for S { }
| +++++
help: for an inherent impl, drop this `for`
|
LL - impl for S { }
LL + impl S { }
|
error: aborting due to previous error