Skip to content

Commit de21c3d

Browse files
Create E0777 error code for "invalid literal in derive"
1 parent 7820135 commit de21c3d

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

compiler/rustc_error_codes/src/error_codes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ E0773: include_str!("./error_codes/E0773.md"),
459459
E0774: include_str!("./error_codes/E0774.md"),
460460
E0775: include_str!("./error_codes/E0775.md"),
461461
E0776: include_str!("./error_codes/E0776.md"),
462+
E0777: include_str!("./error_codes/E0777.md"),
462463
;
463464
// E0006, // merged with E0005
464465
// E0008, // cannot bind by-move into a pattern guard
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
A literal value was used inside `#[derive]`.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0777
6+
#[derive("Clone")] // error!
7+
struct Foo;
8+
```
9+
10+
Only paths to traits are allowed as argument inside `#[derive]`. You can find
11+
more information about the `#[derive]` attribute in the [Rust Book].
12+
13+
14+
```
15+
#[derive(Clone)] // ok!
16+
struct Foo;
17+
```
18+
19+
[Rust Book]: https://doc.rust-lang.org/book/appendix-03-derivable-traits.html

compiler/rustc_expand/src/proc_macro.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast::token;
55
use rustc_ast::tokenstream::{TokenStream, TokenTree};
66
use rustc_ast::{self as ast, *};
77
use rustc_data_structures::sync::Lrc;
8-
use rustc_errors::{Applicability, ErrorReported};
8+
use rustc_errors::{struct_span_err, Applicability, ErrorReported};
99
use rustc_parse::nt_to_tokenstream;
1010
use rustc_span::symbol::sym;
1111
use rustc_span::{Span, DUMMY_SP};
@@ -182,9 +182,14 @@ crate fn collect_derives(cx: &mut ExtCtxt<'_>, attrs: &mut Vec<ast::Attribute>)
182182
.filter_map(|nmi| match nmi {
183183
NestedMetaItem::Literal(lit) => {
184184
error_reported_filter_map = true;
185-
cx.struct_span_err(lit.span, "expected path to a trait, found literal")
186-
.help("for example, write `#[derive(Debug)]` for `Debug`")
187-
.emit();
185+
struct_span_err!(
186+
cx.sess,
187+
lit.span,
188+
E0777,
189+
"expected path to a trait, found literal",
190+
)
191+
.help("for example, write `#[derive(Debug)]` for `Debug`")
192+
.emit();
188193
None
189194
}
190195
NestedMetaItem::MetaItem(mi) => Some(mi),

src/test/ui/error-codes/E0777.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[derive("Clone")] //~ ERROR E0777
2+
struct Foo;
3+
4+
fn main() {}

src/test/ui/error-codes/E0777.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0777]: expected path to a trait, found literal
2+
--> $DIR/E0777.rs:1:10
3+
|
4+
LL | #[derive("Clone")]
5+
| ^^^^^^^
6+
|
7+
= help: for example, write `#[derive(Debug)]` for `Debug`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0777`.

0 commit comments

Comments
 (0)