Skip to content

Commit 3f5e617

Browse files
committed
Auto merge of rust-lang#76406 - GuillaumeGomez:create-e0774, r=pickfire,jyn514
Create E0774
2 parents 0855263 + 1d02f4f commit 3f5e617

14 files changed

+69
-31
lines changed

compiler/rustc_error_codes/src/error_codes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ E0769: include_str!("./error_codes/E0769.md"),
455455
E0770: include_str!("./error_codes/E0770.md"),
456456
E0771: include_str!("./error_codes/E0771.md"),
457457
E0773: include_str!("./error_codes/E0773.md"),
458+
E0774: include_str!("./error_codes/E0774.md"),
458459
;
459460
// E0006, // merged with E0005
460461
// E0008, // cannot bind by-move into a pattern guard
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
`derive` was applied on something which is not a struct, a union or an enum.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0774
6+
trait Foo {
7+
#[derive(Clone)] // error!
8+
type Bar;
9+
}
10+
```
11+
12+
As said above, the `derive` attribute is only allowed on structs, unions or
13+
enums:
14+
15+
```
16+
#[derive(Clone)] // ok!
17+
struct Bar {
18+
field: u32,
19+
}
20+
```
21+
22+
You can find more information about `derive` in the [Rust Book].
23+
24+
[Rust Book]: https://doc.rust-lang.org/book/appendix-03-derivable-traits.html

compiler/rustc_expand/src/expand.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
529529
fn error_derive_forbidden_on_non_adt(&self, derives: &[Path], item: &Annotatable) {
530530
let attr = self.cx.sess.find_by_name(item.attrs(), sym::derive);
531531
let span = attr.map_or(item.span(), |attr| attr.span);
532-
let mut err = self
533-
.cx
534-
.struct_span_err(span, "`derive` may only be applied to structs, enums and unions");
532+
let mut err = rustc_errors::struct_span_err!(
533+
self.cx.sess,
534+
span,
535+
E0774,
536+
"`derive` may only be applied to structs, enums and unions",
537+
);
535538
if let Some(ast::Attribute { style: ast::AttrStyle::Inner, .. }) = attr {
536539
let trait_list = derives.iter().map(|t| pprust::path_to_string(t)).collect::<Vec<_>>();
537540
let suggestion = format!("#[derive({})]", trait_list.join(", "));
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
error: `derive` may only be applied to structs, enums and unions
1+
error[E0774]: `derive` may only be applied to structs, enums and unions
22
--> $DIR/derive-on-trait-item-or-impl-item.rs:2:5
33
|
44
LL | #[derive(Clone)]
55
| ^^^^^^^^^^^^^^^^
66

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

1313
error: aborting due to 2 previous errors
1414

15+
For more information about this error, try `rustc --explain E0774`.
+10-9
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,57 @@
1-
error: `derive` may only be applied to structs, enums and unions
1+
error[E0774]: `derive` may only be applied to structs, enums and unions
22
--> $DIR/deriving-non-type.rs:5:1
33
|
44
LL | #[derive(PartialEq)]
55
| ^^^^^^^^^^^^^^^^^^^^
66

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

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

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

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

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

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

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

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

5555
error: aborting due to 9 previous errors
5656

57+
For more information about this error, try `rustc --explain E0774`.
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
error: `derive` may only be applied to structs, enums and unions
1+
error[E0774]: `derive` may only be applied to structs, enums and unions
22
--> $DIR/issue-43106-gating-of-derive.rs:4:1
33
|
44
LL | #[derive(Debug)]
55
| ^^^^^^^^^^^^^^^^
66

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

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

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

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

3131
error: aborting due to 5 previous errors
3232

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `derive` may only be applied to structs, enums and unions
1+
error[E0774]: `derive` may only be applied to structs, enums and unions
22
--> $DIR/issue-36617.rs:1:1
33
|
44
LL | #![derive(Copy)]
@@ -22,3 +22,4 @@ LL | #![derive(Copy)]
2222

2323
error: aborting due to 3 previous errors
2424

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

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
error: `derive` may only be applied to structs, enums and unions
1+
error[E0774]: `derive` may only be applied to structs, enums and unions
22
--> $DIR/issue-43023.rs:4:5
33
|
44
LL | #[derive(Debug)]
55
| ^^^^^^^^^^^^^^^^
66

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

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

1919
error: aborting due to 3 previous errors
2020

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

src/test/ui/issues/issue-49934-errors.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `derive` may only be applied to structs, enums and unions
1+
error[E0774]: `derive` may only be applied to structs, enums and unions
22
--> $DIR/issue-49934-errors.rs:1:8
33
|
44
LL | fn foo<#[derive(Debug)] T>() {
@@ -10,7 +10,7 @@ error: expected an inert attribute, found a derive macro
1010
LL | fn foo<#[derive(Debug)] T>() {
1111
| ^^^^^
1212

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

2525
error: aborting due to 4 previous errors
2626

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

src/test/ui/macros/trace_faulty_macros.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ LL | let a = pat_macro!();
6060
|
6161
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
6262

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

8080
error: aborting due to 4 previous errors
8181

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

src/test/ui/malformed/issue-69341-malformed-derive-inert.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: traits in `#[derive(...)]` don't accept arguments
44
LL | #[derive(parse())]
55
| ^^ help: remove the arguments
66

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

2525
error: aborting due to 4 previous errors
2626

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

src/test/ui/proc-macro/attributes-on-modules-fail.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `derive` may only be applied to structs, enums and unions
1+
error[E0774]: `derive` may only be applied to structs, enums and unions
22
--> $DIR/attributes-on-modules-fail.rs:16:1
33
|
44
LL | #[derive(Copy)]
@@ -64,5 +64,5 @@ LL | use m::X;
6464

6565
error: aborting due to 7 previous errors
6666

67-
Some errors have detailed explanations: E0412, E0658.
67+
Some errors have detailed explanations: E0412, E0658, E0774.
6868
For more information about an error, try `rustc --explain E0412`.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
error: `derive` may only be applied to structs, enums and unions
1+
error[E0774]: `derive` may only be applied to structs, enums and unions
22
--> $DIR/macros-in-extern-derive.rs:2:5
33
|
44
LL | #[derive(Copy)]
55
| ^^^^^^^^^^^^^^^
66

77
error: aborting due to previous error
88

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

src/test/ui/span/issue-43927-non-ADT-derive.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `derive` may only be applied to structs, enums and unions
1+
error[E0774]: `derive` may only be applied to structs, enums and unions
22
--> $DIR/issue-43927-non-ADT-derive.rs:3:1
33
|
44
LL | #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
@@ -54,3 +54,4 @@ LL | #![derive(Debug, PartialEq, Eq)] // should be an outer attribute!
5454

5555
error: aborting due to 7 previous errors
5656

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

0 commit comments

Comments
 (0)