Skip to content

Add suggestion to remove derive() if invoked macro is non-derive #109638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,19 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if let Some((article, expected)) = unexpected_res {
let path_str = pprust::path_to_string(path);
let msg = format!("expected {}, found {} `{}`", expected, res.descr(), path_str);
self.tcx
.sess
.struct_span_err(path.span, &msg)
.span_label(path.span, format!("not {} {}", article, expected))
.emit();
let mut err = self.tcx.sess.struct_span_err(path.span, &msg);

err.span_label(path.span, format!("not {} {}", article, expected));

if !path.span.from_expansion() {
// Suggest moving the macro out of the derive() as the macro isn't Derive
if kind == MacroKind::Derive && ext.macro_kind() != MacroKind::Derive {
err.span_help(path.span, "Remove from the surrounding `derive()`");
err.help(format!("Add as non-Derive macro\n`#[{}]`", path_str));
}
}

err.emit();
return Ok((self.dummy_ext(kind), Res::Err));
}

Expand Down
8 changes: 8 additions & 0 deletions tests/ui/macros/macro-path-prelude-fail-4.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ error: expected derive macro, found built-in attribute `inline`
|
LL | #[derive(inline)]
| ^^^^^^ not a derive macro
|
help: Remove from the surrounding `derive()`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a new test case that has some of the trickier cases for this change? e.g. two derives, macro (to confirm it doesn't trigger), etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, this isn't valid. Was there something else you meant with macro?

macro_rules! foo {
    () => {
        Debug
    };
}

#[derive(foo!())]
struct V;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macro_rules! foo {
    ($foo:expr) => {
		#[derive($foo)]
		struct V;
    };
}

Something more like this?

--> $DIR/macro-path-prelude-fail-4.rs:1:10
|
LL | #[derive(inline)]
| ^^^^^^
= help: Add as non-Derive macro
`#[inline]`

error: aborting due to previous error

16 changes: 16 additions & 0 deletions tests/ui/proc-macro/macro-namespace-reserved-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ error: expected derive macro, found attribute macro `my_macro_attr`
|
LL | #[derive(my_macro_attr)]
| ^^^^^^^^^^^^^ not a derive macro
|
help: Remove from the surrounding `derive()`
--> $DIR/macro-namespace-reserved-2.rs:53:10
|
LL | #[derive(my_macro_attr)]
| ^^^^^^^^^^^^^
= help: Add as non-Derive macro
`#[my_macro_attr]`

error: can't use a procedural macro from the same crate that defines it
--> $DIR/macro-namespace-reserved-2.rs:56:10
Expand Down Expand Up @@ -87,6 +95,14 @@ error: expected derive macro, found macro `crate::my_macro`
|
LL | #[derive(crate::my_macro)]
| ^^^^^^^^^^^^^^^ not a derive macro
|
help: Remove from the surrounding `derive()`
--> $DIR/macro-namespace-reserved-2.rs:50:10
|
LL | #[derive(crate::my_macro)]
| ^^^^^^^^^^^^^^^
= help: Add as non-Derive macro
`#[crate::my_macro]`

error: cannot find macro `my_macro_attr` in this scope
--> $DIR/macro-namespace-reserved-2.rs:28:5
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/tool-attributes/tool-attributes-misplaced-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ error: expected derive macro, found tool attribute `rustfmt::skip`
|
LL | #[derive(rustfmt::skip)]
| ^^^^^^^^^^^^^ not a derive macro
|
help: Remove from the surrounding `derive()`
--> $DIR/tool-attributes-misplaced-2.rs:1:10
|
LL | #[derive(rustfmt::skip)]
| ^^^^^^^^^^^^^
= help: Add as non-Derive macro
`#[rustfmt::skip]`

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