about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/parser
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2021-09-13 21:20:42 +0200
committerGitHub <noreply@github.com>2021-09-13 21:20:42 +0200
commit84d65fee0e57830153601c798f4c946c6df72040 (patch)
tree183ecb07a0fee3fc4ccf16f5901718ba31f9eeae /compiler/rustc_parse/src/parser
parentf10fc2152b4c661e3b59504abff28d22d7345ee0 (diff)
parent3f0e69591934bdc90577f2af41c0981ea2d1651b (diff)
downloadrust-84d65fee0e57830153601c798f4c946c6df72040.tar.gz
rust-84d65fee0e57830153601c798f4c946c6df72040.zip
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 { }
  |
```
Diffstat (limited to 'compiler/rustc_parse/src/parser')
-rw-r--r--compiler/rustc_parse/src/parser/item.rs15
1 files changed, 14 insertions, 1 deletions
diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs
index 10c73fd64bc..04a7948e8c9 100644
--- a/compiler/rustc_parse/src/parser/item.rs
+++ b/compiler/rustc_parse/src/parser/item.rs
@@ -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,