Skip to content

Commit c12687b

Browse files
Centrilpietroalbini
authored andcommitted
error_derive_forbidden_on_non_adt: be more graceful
1 parent 153e002 commit c12687b

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/librustc_expand/expand.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
435435
_ => unreachable!(),
436436
};
437437
if !item.derive_allowed() {
438-
let attr = attr::find_by_name(item.attrs(), sym::derive)
439-
.expect("`derive` attribute should exist");
440-
let span = attr.span;
438+
let attr = attr::find_by_name(item.attrs(), sym::derive);
439+
let span = attr.map_or(item.span(), |attr| attr.span);
441440
let mut err = self.cx.struct_span_err(
442441
span,
443442
"`derive` may only be applied to structs, enums and unions",
444443
);
445-
if let ast::AttrStyle::Inner = attr.style {
444+
if let Some(ast::Attribute { style: ast::AttrStyle::Inner, .. }) = attr {
446445
let trait_list = derives
447446
.iter()
448447
.map(|t| pprust::path_to_string(t))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() {}
2+
3+
struct CLI {
4+
#[derive(parse())]
5+
//~^ ERROR traits in `#[derive(...)]` don't accept arguments
6+
//~| ERROR cannot find derive macro `parse` in this scope
7+
//~| ERROR cannot find derive macro `parse` in this scope
8+
path: (),
9+
//~^ ERROR `derive` may only be applied to structs, enums and unions
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: traits in `#[derive(...)]` don't accept arguments
2+
--> $DIR/issue-69341-malformed-derive-inert.rs:4:19
3+
|
4+
LL | #[derive(parse())]
5+
| ^^ help: remove the arguments
6+
7+
error: `derive` may only be applied to structs, enums and unions
8+
--> $DIR/issue-69341-malformed-derive-inert.rs:8:5
9+
|
10+
LL | path: (),
11+
| ^^^^^^^^
12+
13+
error: cannot find derive macro `parse` in this scope
14+
--> $DIR/issue-69341-malformed-derive-inert.rs:4:14
15+
|
16+
LL | #[derive(parse())]
17+
| ^^^^^
18+
19+
error: cannot find derive macro `parse` in this scope
20+
--> $DIR/issue-69341-malformed-derive-inert.rs:4:14
21+
|
22+
LL | #[derive(parse())]
23+
| ^^^^^
24+
25+
error: aborting due to 4 previous errors
26+

0 commit comments

Comments
 (0)