Skip to content

Commit 87cc521

Browse files
authored
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
2 parents 07d9ed2 + 13d42f4 commit 87cc521

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
@@ -503,13 +503,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
503503
}
504504

505505
fn error_derive_forbidden_on_non_adt(&self, derives: &[Path], item: &Annotatable) {
506-
let attr =
507-
attr::find_by_name(item.attrs(), sym::derive).expect("`derive` attribute should exist");
508-
let span = attr.span;
506+
let attr = attr::find_by_name(item.attrs(), sym::derive);
507+
let span = attr.map_or(item.span(), |attr| attr.span);
509508
let mut err = self
510509
.cx
511510
.struct_span_err(span, "`derive` may only be applied to structs, enums and unions");
512-
if let ast::AttrStyle::Inner = attr.style {
511+
if let Some(ast::Attribute { style: ast::AttrStyle::Inner, .. }) = attr {
513512
let trait_list = derives.iter().map(|t| pprust::path_to_string(t)).collect::<Vec<_>>();
514513
let suggestion = format!("#[derive({})]", trait_list.join(", "));
515514
err.span_suggestion(
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)