about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2020-03-09 13:20:00 +0100
committerPietro Albini <pietro@pietroalbini.org>2020-03-09 15:11:57 +0100
commitc12687b1a12b893ec8352861ec9fed773b8027a6 (patch)
treef6c39c4c763bb401119822defec79df65360233c
parent153e0029f519616ff9c7fb5c0ef92b5c48a56f66 (diff)
downloadrust-c12687b1a12b893ec8352861ec9fed773b8027a6.tar.gz
rust-c12687b1a12b893ec8352861ec9fed773b8027a6.zip
error_derive_forbidden_on_non_adt: be more graceful
-rw-r--r--src/librustc_expand/expand.rs7
-rw-r--r--src/test/ui/malformed/issue-69341-malformed-derive-inert.rs10
-rw-r--r--src/test/ui/malformed/issue-69341-malformed-derive-inert.stderr26
3 files changed, 39 insertions, 4 deletions
diff --git a/src/librustc_expand/expand.rs b/src/librustc_expand/expand.rs
index f915f44c17a..a65ba8903be 100644
--- a/src/librustc_expand/expand.rs
+++ b/src/librustc_expand/expand.rs
@@ -435,14 +435,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
                         _ => unreachable!(),
                     };
                     if !item.derive_allowed() {
-                        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))
diff --git a/src/test/ui/malformed/issue-69341-malformed-derive-inert.rs b/src/test/ui/malformed/issue-69341-malformed-derive-inert.rs
new file mode 100644
index 00000000000..24692f7cf52
--- /dev/null
+++ b/src/test/ui/malformed/issue-69341-malformed-derive-inert.rs
@@ -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
+}
diff --git a/src/test/ui/malformed/issue-69341-malformed-derive-inert.stderr b/src/test/ui/malformed/issue-69341-malformed-derive-inert.stderr
new file mode 100644
index 00000000000..e8f96178d10
--- /dev/null
+++ b/src/test/ui/malformed/issue-69341-malformed-derive-inert.stderr
@@ -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
+