Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create E0777 error code for invalid argument in derive #77419

Merged
merged 3 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_error_codes/src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0777.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rustc_ast_pretty::pprust;
use rustc_attr::{self as attr, is_builtin_attr, HasAttrs};
use rustc_data_structures::map_in_place::MapInPlace;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::{Applicability, PResult};
use rustc_errors::{struct_span_err, Applicability, PResult};
use rustc_feature::Features;
use rustc_parse::parser::Parser;
use rustc_parse::validate_attr;
Expand Down Expand Up @@ -542,7 +542,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
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 span = attr.map_or(item.span(), |attr| attr.span);
let mut err = rustc_errors::struct_span_err!(
let mut err = struct_span_err!(
self.cx.sess,
span,
E0774,
Expand Down
22 changes: 18 additions & 4 deletions compiler/rustc_expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ 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_lexer::is_ident;
use rustc_parse::nt_to_tokenstream;
use rustc_span::symbol::sym;
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -182,9 +183,22 @@ 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();
let mut err = struct_span_err!(
cx.sess,
lit.span,
E0777,
"expected path to a trait, found literal",
);
let token = lit.token.to_string();
if token.starts_with('"')
&& token.len() > 2
&& is_ident(&token[1..token.len() - 1])
{
err.help(&format!("try using `#[derive({})]`", &token[1..token.len() - 1]));
} else {
err.help("for example, write `#[derive(Debug)]` for `Debug`");
}
err.emit();
None
}
NestedMetaItem::MetaItem(mi) => Some(mi),
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/error-codes/E0777.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[derive("Clone")] //~ ERROR E0777
#[derive("Clone
")]
//~^^ ERROR E0777
struct Foo;

fn main() {}
21 changes: 21 additions & 0 deletions src/test/ui/error-codes/E0777.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0777]: expected path to a trait, found literal
--> $DIR/E0777.rs:1:10
|
LL | #[derive("Clone")]
| ^^^^^^^
|
= help: try using `#[derive(Clone)]`

error[E0777]: expected path to a trait, found literal
--> $DIR/E0777.rs:2:10
|
LL | #[derive("Clone
| __________^
LL | | ")]
| |_^
|
= help: for example, write `#[derive(Debug)]` for `Debug`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0777`.