mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 15:23:46 +00:00
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:
commit
84d65fee0e
@ -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,
|
||||
|
@ -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
|
||||
|
||||
|
10
src/test/ui/parser/issue-88818.rs
Normal file
10
src/test/ui/parser/issue-88818.rs
Normal 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() {}
|
18
src/test/ui/parser/issue-88818.stderr
Normal file
18
src/test/ui/parser/issue-88818.stderr
Normal 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
|
||||
|
Loading…
Reference in New Issue
Block a user