Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create E0774 #76406

Merged
merged 2 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_error_codes/src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ E0769: include_str!("./error_codes/E0769.md"),
E0770: include_str!("./error_codes/E0770.md"),
E0771: include_str!("./error_codes/E0771.md"),
E0773: include_str!("./error_codes/E0773.md"),
E0774: include_str!("./error_codes/E0774.md"),
;
// E0006, // merged with E0005
// E0008, // cannot bind by-move into a pattern guard
Expand Down
24 changes: 24 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0774.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
`derive` was applied on something which is not a struct, a union or an enum.

Erroneous code example:

```compile_fail,E0774
trait Foo {
#[derive(Clone)] // error!
type Bar;
}
```

As said above, the `derive` attribute is only allowed on structs, unions or
enums:

```
#[derive(Clone)] // ok!
struct Bar {
field: u32,
}
```

You can find more information about `derive` in the [Rust Book].

[Rust Book]: https://doc.rust-lang.org/book/appendix-03-derivable-traits.html
9 changes: 6 additions & 3 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
fn error_derive_forbidden_on_non_adt(&self, derives: &[Path], item: &Annotatable) {
let attr = self.cx.sess.find_by_name(item.attrs(), sym::derive);
let span = attr.map_or(item.span(), |attr| attr.span);
let mut err = self
.cx
.struct_span_err(span, "`derive` may only be applied to structs, enums and unions");
let mut err = rustc_errors::struct_span_err!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this is generally imported and used without the rustc_errors prefix

self.cx.sess,
span,
E0774,
"`derive` may only be applied to structs, enums and unions",
);
if let Some(ast::Attribute { style: ast::AttrStyle::Inner, .. }) = attr {
let trait_list = derives.iter().map(|t| pprust::path_to_string(t)).collect::<Vec<_>>();
let suggestion = format!("#[derive({})]", trait_list.join(", "));
Expand Down
5 changes: 3 additions & 2 deletions src/test/ui/derives/derive-on-trait-item-or-impl-item.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/derive-on-trait-item-or-impl-item.rs:2:5
|
LL | #[derive(Clone)]
| ^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/derive-on-trait-item-or-impl-item.rs:10:5
|
LL | #[derive(Clone)]
| ^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0774`.
19 changes: 10 additions & 9 deletions src/test/ui/derives/deriving-non-type.stderr
Original file line number Diff line number Diff line change
@@ -1,56 +1,57 @@
error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/deriving-non-type.rs:5:1
|
LL | #[derive(PartialEq)]
| ^^^^^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/deriving-non-type.rs:8:1
|
LL | #[derive(PartialEq)]
| ^^^^^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/deriving-non-type.rs:11:1
|
LL | #[derive(PartialEq)]
| ^^^^^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/deriving-non-type.rs:14:1
|
LL | #[derive(PartialEq)]
| ^^^^^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/deriving-non-type.rs:17:1
|
LL | #[derive(PartialEq)]
| ^^^^^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/deriving-non-type.rs:20:1
|
LL | #[derive(PartialEq)]
| ^^^^^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/deriving-non-type.rs:23:1
|
LL | #[derive(PartialEq)]
| ^^^^^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/deriving-non-type.rs:26:1
|
LL | #[derive(PartialEq)]
| ^^^^^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/deriving-non-type.rs:29:1
|
LL | #[derive(PartialEq)]
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 9 previous errors

For more information about this error, try `rustc --explain E0774`.
11 changes: 6 additions & 5 deletions src/test/ui/feature-gate/issue-43106-gating-of-derive.stderr
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-43106-gating-of-derive.rs:4:1
|
LL | #[derive(Debug)]
| ^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-43106-gating-of-derive.rs:7:17
|
LL | mod inner { #![derive(Debug)] }
| ^^^^^^^^^^^^^^^^^ help: try an outer attribute: `#[derive(Debug)]`

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-43106-gating-of-derive.rs:10:5
|
LL | #[derive(Debug)]
| ^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-43106-gating-of-derive.rs:23:5
|
LL | #[derive(Debug)]
| ^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-43106-gating-of-derive.rs:27:5
|
LL | #[derive(Debug)]
| ^^^^^^^^^^^^^^^^

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0774`.
3 changes: 2 additions & 1 deletion src/test/ui/issues/issue-36617.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-36617.rs:1:1
|
LL | #![derive(Copy)]
Expand All @@ -22,3 +22,4 @@ LL | #![derive(Copy)]

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0774`.
7 changes: 4 additions & 3 deletions src/test/ui/issues/issue-43023.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-43023.rs:4:5
|
LL | #[derive(Debug)]
| ^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-43023.rs:11:5
|
LL | #[derive(Debug)]
| ^^^^^^^^^^^^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-43023.rs:16:5
|
LL | #[derive(Debug)]
| ^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0774`.
5 changes: 3 additions & 2 deletions src/test/ui/issues/issue-49934-errors.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-49934-errors.rs:1:8
|
LL | fn foo<#[derive(Debug)] T>() {
Expand All @@ -10,7 +10,7 @@ error: expected an inert attribute, found a derive macro
LL | fn foo<#[derive(Debug)] T>() {
| ^^^^^

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-49934-errors.rs:5:9
|
LL | #[derive(Debug)]
Expand All @@ -24,3 +24,4 @@ LL | #[derive(Debug)]

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0774`.
3 changes: 2 additions & 1 deletion src/test/ui/macros/trace_faulty_macros.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ LL | let a = pat_macro!();
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/trace_faulty_macros.rs:42:1
|
LL | #[derive(Debug)]
Expand All @@ -79,3 +79,4 @@ LL | let a = pat_macro!();

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0774`.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: traits in `#[derive(...)]` don't accept arguments
LL | #[derive(parse())]
| ^^ help: remove the arguments

error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-69341-malformed-derive-inert.rs:8:5
|
LL | path: (),
Expand All @@ -24,3 +24,4 @@ LL | #[derive(parse())]

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0774`.
4 changes: 2 additions & 2 deletions src/test/ui/proc-macro/attributes-on-modules-fail.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/attributes-on-modules-fail.rs:16:1
|
LL | #[derive(Copy)]
Expand Down Expand Up @@ -64,5 +64,5 @@ LL | use m::X;

error: aborting due to 7 previous errors

Some errors have detailed explanations: E0412, E0658.
Some errors have detailed explanations: E0412, E0658, E0774.
For more information about an error, try `rustc --explain E0412`.
3 changes: 2 additions & 1 deletion src/test/ui/proc-macro/macros-in-extern-derive.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/macros-in-extern-derive.rs:2:5
|
LL | #[derive(Copy)]
| ^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0774`.
3 changes: 2 additions & 1 deletion src/test/ui/span/issue-43927-non-ADT-derive.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: `derive` may only be applied to structs, enums and unions
error[E0774]: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-43927-non-ADT-derive.rs:3:1
|
LL | #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
Expand Down Expand Up @@ -54,3 +54,4 @@ LL | #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!

error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0774`.