Rollup merge of #69522 - Centril:fix-69341, r=petrochenkov

error_derive_forbidden_on_non_adt: be more graceful

Fixes #69341 which was injected by #67052.

r? @petrochenkov
This commit is contained in:
Mazdak Farrokhzad 2020-02-28 17:17:31 +01:00 committed by GitHub
commit 87cc521b02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 4 deletions

View File

@ -503,13 +503,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
}
fn error_derive_forbidden_on_non_adt(&self, derives: &[Path], item: &Annotatable) {
let attr =
attr::find_by_name(item.attrs(), sym::derive).expect("`derive` attribute should exist");
let span = attr.span;
let attr = attr::find_by_name(item.attrs(), sym::derive);
let span = attr.map_or(item.span(), |attr| attr.span);
let mut err = self
.cx
.struct_span_err(span, "`derive` may only be applied to structs, enums and unions");
if let ast::AttrStyle::Inner = attr.style {
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 suggestion = format!("#[derive({})]", trait_list.join(", "));
err.span_suggestion(

View File

@ -0,0 +1,10 @@
fn main() {}
struct CLI {
#[derive(parse())]
//~^ ERROR traits in `#[derive(...)]` don't accept arguments
//~| ERROR cannot find derive macro `parse` in this scope
//~| ERROR cannot find derive macro `parse` in this scope
path: (),
//~^ ERROR `derive` may only be applied to structs, enums and unions
}

View File

@ -0,0 +1,26 @@
error: traits in `#[derive(...)]` don't accept arguments
--> $DIR/issue-69341-malformed-derive-inert.rs:4:19
|
LL | #[derive(parse())]
| ^^ help: remove the arguments
error: `derive` may only be applied to structs, enums and unions
--> $DIR/issue-69341-malformed-derive-inert.rs:8:5
|
LL | path: (),
| ^^^^^^^^
error: cannot find derive macro `parse` in this scope
--> $DIR/issue-69341-malformed-derive-inert.rs:4:14
|
LL | #[derive(parse())]
| ^^^^^
error: cannot find derive macro `parse` in this scope
--> $DIR/issue-69341-malformed-derive-inert.rs:4:14
|
LL | #[derive(parse())]
| ^^^^^
error: aborting due to 4 previous errors