Create new E0774 code error
This commit is contained in:
parent
02fe30971e
commit
e0df2f87b6
3 changed files with 31 additions and 3 deletions
|
@ -455,6 +455,7 @@ E0769: include_str!("./error_codes/E0769.md"),
|
||||||
E0770: include_str!("./error_codes/E0770.md"),
|
E0770: include_str!("./error_codes/E0770.md"),
|
||||||
E0771: include_str!("./error_codes/E0771.md"),
|
E0771: include_str!("./error_codes/E0771.md"),
|
||||||
E0773: include_str!("./error_codes/E0773.md"),
|
E0773: include_str!("./error_codes/E0773.md"),
|
||||||
|
E0774: include_str!("./error_codes/E0774.md"),
|
||||||
;
|
;
|
||||||
// E0006, // merged with E0005
|
// E0006, // merged with E0005
|
||||||
// E0008, // cannot bind by-move into a pattern guard
|
// E0008, // cannot bind by-move into a pattern guard
|
||||||
|
|
24
compiler/rustc_error_codes/src/error_codes/E0774.md
Normal file
24
compiler/rustc_error_codes/src/error_codes/E0774.md
Normal file
|
@ -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
|
|
@ -529,9 +529,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
||||||
fn error_derive_forbidden_on_non_adt(&self, derives: &[Path], item: &Annotatable) {
|
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 attr = self.cx.sess.find_by_name(item.attrs(), sym::derive);
|
||||||
let span = attr.map_or(item.span(), |attr| attr.span);
|
let span = attr.map_or(item.span(), |attr| attr.span);
|
||||||
let mut err = self
|
let mut err = rustc_errors::struct_span_err!(
|
||||||
.cx
|
self.cx.sess,
|
||||||
.struct_span_err(span, "`derive` may only be applied to structs, enums and unions");
|
span,
|
||||||
|
E0774,
|
||||||
|
"`derive` may only be applied to structs, enums and unions",
|
||||||
|
);
|
||||||
if let Some(ast::Attribute { style: ast::AttrStyle::Inner, .. }) = attr {
|
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 trait_list = derives.iter().map(|t| pprust::path_to_string(t)).collect::<Vec<_>>();
|
||||||
let suggestion = format!("#[derive({})]", trait_list.join(", "));
|
let suggestion = format!("#[derive({})]", trait_list.join(", "));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue