Skip to content

Commit 42f2be8

Browse files
committed
Add suggestion to remove derive() if invoked macro is non-derive
1 parent 89c2e3d commit 42f2be8

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

compiler/rustc_resolve/src/macros.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,23 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
544544
if let Some((article, expected)) = unexpected_res {
545545
let path_str = pprust::path_to_string(path);
546546
let msg = format!("expected {}, found {} `{}`", expected, res.descr(), path_str);
547-
self.tcx
548-
.sess
549-
.struct_span_err(path.span, &msg)
550-
.span_label(path.span, format!("not {} {}", article, expected))
551-
.emit();
547+
let mut err = self.tcx.sess.struct_span_err(path.span, &msg);
548+
549+
err.span_label(path.span, format!("not {} {}", article, expected));
550+
551+
if kind == MacroKind::Derive && ext.macro_kind() != MacroKind::Derive {
552+
// Suggest removing the derive() as the macro isn't Derive
553+
let opening_span =
554+
path.span.shrink_to_lo().with_lo(path.span.lo() - rustc_span::BytePos(7));
555+
let closing_span =
556+
path.span.shrink_to_hi().with_hi(path.span.hi() + rustc_span::BytePos(1));
557+
err.span_help(
558+
vec![opening_span, closing_span],
559+
"remove the surrounding \"derive()\":",
560+
);
561+
}
562+
563+
err.emit();
552564
return Ok((self.dummy_ext(kind), Res::Err));
553565
}
554566

tests/ui/macros/macro-path-prelude-fail-4.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error: expected derive macro, found built-in attribute `inline`
33
|
44
LL | #[derive(inline)]
55
| ^^^^^^ not a derive macro
6+
|
7+
help: remove the surrounding "derive()":
8+
--> $DIR/macro-path-prelude-fail-4.rs:1:3
9+
|
10+
LL | #[derive(inline)]
11+
| ^^^^^^^ ^
612

713
error: aborting due to previous error
814

tests/ui/proc-macro/macro-namespace-reserved-2.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ error: expected derive macro, found attribute macro `my_macro_attr`
5757
|
5858
LL | #[derive(my_macro_attr)]
5959
| ^^^^^^^^^^^^^ not a derive macro
60+
|
61+
help: remove the surrounding "derive()":
62+
--> $DIR/macro-namespace-reserved-2.rs:53:3
63+
|
64+
LL | #[derive(my_macro_attr)]
65+
| ^^^^^^^ ^
6066

6167
error: can't use a procedural macro from the same crate that defines it
6268
--> $DIR/macro-namespace-reserved-2.rs:56:10
@@ -87,6 +93,12 @@ error: expected derive macro, found macro `crate::my_macro`
8793
|
8894
LL | #[derive(crate::my_macro)]
8995
| ^^^^^^^^^^^^^^^ not a derive macro
96+
|
97+
help: remove the surrounding "derive()":
98+
--> $DIR/macro-namespace-reserved-2.rs:50:3
99+
|
100+
LL | #[derive(crate::my_macro)]
101+
| ^^^^^^^ ^
90102

91103
error: cannot find macro `my_macro_attr` in this scope
92104
--> $DIR/macro-namespace-reserved-2.rs:28:5

tests/ui/tool-attributes/tool-attributes-misplaced-2.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error: expected derive macro, found tool attribute `rustfmt::skip`
33
|
44
LL | #[derive(rustfmt::skip)]
55
| ^^^^^^^^^^^^^ not a derive macro
6+
|
7+
help: remove the surrounding "derive()":
8+
--> $DIR/tool-attributes-misplaced-2.rs:1:3
9+
|
10+
LL | #[derive(rustfmt::skip)]
11+
| ^^^^^^^ ^
612

713
error: expected macro, found tool attribute `rustfmt::skip`
814
--> $DIR/tool-attributes-misplaced-2.rs:5:5

0 commit comments

Comments
 (0)