diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2020-10-01 18:39:47 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2020-10-01 20:41:57 +0200 |
| commit | de21c3df0e17674784fe3691e3e5517c0c45cdd3 (patch) | |
| tree | c6de3affa2b73246fe3aa769854be70399fbf025 | |
| parent | 782013564efc06ef02614ba35a4e67dee4fcb8e7 (diff) | |
| download | rust-de21c3df0e17674784fe3691e3e5517c0c45cdd3.tar.gz rust-de21c3df0e17674784fe3691e3e5517c0c45cdd3.zip | |
Create E0777 error code for "invalid literal in derive"
| -rw-r--r-- | compiler/rustc_error_codes/src/error_codes.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_error_codes/src/error_codes/E0777.md | 19 | ||||
| -rw-r--r-- | compiler/rustc_expand/src/proc_macro.rs | 13 | ||||
| -rw-r--r-- | src/test/ui/error-codes/E0777.rs | 4 | ||||
| -rw-r--r-- | src/test/ui/error-codes/E0777.stderr | 11 |
5 files changed, 44 insertions, 4 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes.rs b/compiler/rustc_error_codes/src/error_codes.rs index 81f65ac8690..981b5bb8ba7 100644 --- a/compiler/rustc_error_codes/src/error_codes.rs +++ b/compiler/rustc_error_codes/src/error_codes.rs @@ -459,6 +459,7 @@ E0773: include_str!("./error_codes/E0773.md"), E0774: include_str!("./error_codes/E0774.md"), E0775: include_str!("./error_codes/E0775.md"), E0776: include_str!("./error_codes/E0776.md"), +E0777: include_str!("./error_codes/E0777.md"), ; // E0006, // merged with E0005 // E0008, // cannot bind by-move into a pattern guard diff --git a/compiler/rustc_error_codes/src/error_codes/E0777.md b/compiler/rustc_error_codes/src/error_codes/E0777.md new file mode 100644 index 00000000000..8c5c6e28b65 --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0777.md @@ -0,0 +1,19 @@ +A literal value was used inside `#[derive]`. + +Erroneous code example: + +```compile_fail,E0777 +#[derive("Clone")] // error! +struct Foo; +``` + +Only paths to traits are allowed as argument inside `#[derive]`. You can find +more information about the `#[derive]` attribute in the [Rust Book]. + + +``` +#[derive(Clone)] // ok! +struct Foo; +``` + +[Rust Book]: https://doc.rust-lang.org/book/appendix-03-derivable-traits.html diff --git a/compiler/rustc_expand/src/proc_macro.rs b/compiler/rustc_expand/src/proc_macro.rs index 94b3fcf2850..320d8fdef4d 100644 --- a/compiler/rustc_expand/src/proc_macro.rs +++ b/compiler/rustc_expand/src/proc_macro.rs @@ -5,7 +5,7 @@ use rustc_ast::token; use rustc_ast::tokenstream::{TokenStream, TokenTree}; use rustc_ast::{self as ast, *}; use rustc_data_structures::sync::Lrc; -use rustc_errors::{Applicability, ErrorReported}; +use rustc_errors::{struct_span_err, Applicability, ErrorReported}; use rustc_parse::nt_to_tokenstream; use rustc_span::symbol::sym; use rustc_span::{Span, DUMMY_SP}; @@ -182,9 +182,14 @@ crate fn collect_derives(cx: &mut ExtCtxt<'_>, attrs: &mut Vec<ast::Attribute>) .filter_map(|nmi| match nmi { NestedMetaItem::Literal(lit) => { error_reported_filter_map = true; - cx.struct_span_err(lit.span, "expected path to a trait, found literal") - .help("for example, write `#[derive(Debug)]` for `Debug`") - .emit(); + struct_span_err!( + cx.sess, + lit.span, + E0777, + "expected path to a trait, found literal", + ) + .help("for example, write `#[derive(Debug)]` for `Debug`") + .emit(); None } NestedMetaItem::MetaItem(mi) => Some(mi), diff --git a/src/test/ui/error-codes/E0777.rs b/src/test/ui/error-codes/E0777.rs new file mode 100644 index 00000000000..ab633e4f535 --- /dev/null +++ b/src/test/ui/error-codes/E0777.rs @@ -0,0 +1,4 @@ +#[derive("Clone")] //~ ERROR E0777 +struct Foo; + +fn main() {} diff --git a/src/test/ui/error-codes/E0777.stderr b/src/test/ui/error-codes/E0777.stderr new file mode 100644 index 00000000000..b356ea508f4 --- /dev/null +++ b/src/test/ui/error-codes/E0777.stderr @@ -0,0 +1,11 @@ +error[E0777]: expected path to a trait, found literal + --> $DIR/E0777.rs:1:10 + | +LL | #[derive("Clone")] + | ^^^^^^^ + | + = help: for example, write `#[derive(Debug)]` for `Debug` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0777`. |
