Skip to content

Commit e0df2f8

Browse files
Create new E0774 code error
1 parent 02fe309 commit e0df2f8

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

compiler/rustc_error_codes/src/error_codes.rs

Lines changed: 1 addition & 0 deletions
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
Lines changed: 24 additions & 0 deletions
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

Lines changed: 6 additions & 3 deletions
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(", "));

0 commit comments

Comments
 (0)