Skip to content

Suggest a code fix when an attribute macro is incorrectly used as a derive macro #109589

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

Closed
LukeMathWalker opened this issue Mar 25, 2023 · 2 comments · Fixed by #109638
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@LukeMathWalker
Copy link
Contributor

LukeMathWalker commented Mar 25, 2023

Code

#[proc_macro_attribute]
pub fn attribute_macro(_metadata: TokenStream, input: TokenStream) -> TokenStream {
    input
}

Current output

error: expected derive macro, found attribute macro `my_macro::attribute_macro`
  |
1 | #[derive(my_macro::attribute_macro)]
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^ not a derive macro

Desired output

(The actual desired output should have colours and minuses on the removed derive, but to give the idea)

error: expected derive macro, found attribute macro `my_macro::attribute_macro`
  |
1 | #[derive(my_macro::attribute_macro)]
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^ not a derive macro
  = help: remove the "derive":

  #[my_macro::attribute_macro]

Rationale and extra context

Many users are not familiar with the terminology being used by the compiler error (i.e. the different flavours of procedural macro, derive vs attribute) and might be confused as to what they need to do in order to fix the error.
Since the fix is straight-forward (i.e. remove the derive), the compiler should show a code snippet that fixes the issue.

Other cases

No response

Anything else?

No response

@LukeMathWalker LukeMathWalker added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 25, 2023
@jyn514 jyn514 added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Mar 25, 2023
@NotStirred
Copy link
Contributor

@rustbot claim

@NotStirred
Copy link
Contributor

NotStirred commented Mar 26, 2023

I have a partially functional implementation of this, though I've run into a bit of trouble, some guidance would be appreciated.

Currently I'm implementing this next to the existing error in compiler/rustc_resolve/src/macros.rs in Resolver#smart_resolve_macro_path.

My trouble comes with knowing what the macro is invoked on, (function, struct), and sending the help message selectively based on that. Is there a way to get this information in smart_resolve_macro_path, or is my approach wrong and the error should be elsewhere?

As far as I can tell I am trying to implement this in a place before the AST is fully parsed, so this may be unviable

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 10, 2023
…avidtwco

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

Adds to the existing `expected derive macro, found {}` error message:
```
help: remove the surrounding "derive()":
  --> $DIR/macro-path-prelude-fail-4.rs:1:3
   |
LL | #[derive(inline)]
   |   ^^^^^^^      ^
```

This suggestion will either fix the issue, in the case that the macro was valid, or provide a better error message if not

Not ready for merge yet, as the highlighted span is only valid for trivial formatting. Is there a nice way to get the parent span of the macro path within `smart_resolve_macro_path`?

Closes rust-lang#109589
@bors bors closed this as completed in 194a0bb Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants