Improve error message for missing trait in trait impl
This commit is contained in:
parent
c7dbe7a830
commit
3f0e695919
4 changed files with 52 additions and 1 deletions
|
@ -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 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);
|
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 {
|
P(Ty {
|
||||||
kind: TyKind::Path(None, err_path(span)),
|
kind: TyKind::Path(None, err_path(span)),
|
||||||
span,
|
span,
|
||||||
|
|
|
@ -3,6 +3,16 @@ error: missing trait in a trait impl
|
||||||
|
|
|
|
||||||
LL | impl for T {}
|
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
|
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…
Add table
Add a link
Reference in a new issue