Skip to content

Commit 337ca88

Browse files
Rollup merge of rust-lang#88894 - FabianWolff:issue-88818, r=estebank
Improve error message for missing trait in trait impl Fixes rust-lang#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 { } | ```
2 parents 8548ba4 + 3f0e695 commit 337ca88

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

compiler/rustc_parse/src/parser/item.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,20 @@ impl<'a> Parser<'a> {
493493
let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
494494
{
495495
let span = self.prev_token.span.between(self.token.span);
496-
self.struct_span_err(span, "missing trait in a trait impl").emit();
496+
self.struct_span_err(span, "missing trait in a trait impl")
497+
.span_suggestion(
498+
span,
499+
"add a trait here",
500+
" Trait ".into(),
501+
Applicability::HasPlaceholders,
502+
)
503+
.span_suggestion(
504+
span.to(self.token.span),
505+
"for an inherent impl, drop this `for`",
506+
"".into(),
507+
Applicability::MaybeIncorrect,
508+
)
509+
.emit();
497510
P(Ty {
498511
kind: TyKind::Path(None, err_path(span)),
499512
span,

src/test/ui/issues/issue-56031.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ error: missing trait in a trait impl
33
|
44
LL | impl for T {}
55
| ^
6+
|
7+
help: add a trait here
8+
|
9+
LL | impl Trait for T {}
10+
| +++++
11+
help: for an inherent impl, drop this `for`
12+
|
13+
LL - impl for T {}
14+
LL + impl T {}
15+
|
616

717
error: aborting due to previous error
818

src/test/ui/parser/issue-88818.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Regression test for #88818 (improve error message for missing trait
2+
// in `impl for X`).
3+
4+
struct S { }
5+
impl for S { }
6+
//~^ ERROR: missing trait in a trait impl
7+
//~| HELP: add a trait here
8+
//~| HELP: for an inherent impl, drop this `for`
9+
10+
fn main() {}

src/test/ui/parser/issue-88818.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: missing trait in a trait impl
2+
--> $DIR/issue-88818.rs:5:5
3+
|
4+
LL | impl for S { }
5+
| ^
6+
|
7+
help: add a trait here
8+
|
9+
LL | impl Trait for S { }
10+
| +++++
11+
help: for an inherent impl, drop this `for`
12+
|
13+
LL - impl for S { }
14+
LL + impl S { }
15+
|
16+
17+
error: aborting due to previous error
18+

0 commit comments

Comments
 (0)