Skip to content
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

Misleading error when attempting to use macro_rules as an attribute #132928

Closed
kpreid opened this issue Nov 12, 2024 · 1 comment · Fixed by #132949
Closed

Misleading error when attempting to use macro_rules as an attribute #132928

kpreid opened this issue Nov 12, 2024 · 1 comment · Fixed by #132949
Labels
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.

Comments

@kpreid
Copy link
Contributor

kpreid commented Nov 12, 2024

Code

Code provided by Discord user baltdev on the Rust Programming Language Discord server (message link).

macro_rules! sample { () => {} }

#[sample]
pub struct S {}

Current output

error: cannot find attribute `sample` in this scope
 --> src/lib.rs:3:3
  |
3 | #[sample]
  |   ^^^^^^ consider moving the definition of `sample` before this call
  |
note: a macro with the same name exists, but it appears later at here
 --> src/lib.rs:1:14
  |
1 | macro_rules! sample { () => {} }
  |              ^^^^^^

warning: unused macro definition: `sample`
 --> src/lib.rs:1:14
  |
1 | macro_rules! sample { () => {} }
  |              ^^^^^^
  |
  = note: `#[warn(unused_macros)]` on by default

Desired output

error: `sample` is not an attribute macro
help: `macro_rules!` macros cannot be used as attribute macros

Rationale and extra context

The error message is about a missing definition that is not missing, and incorrectly suggests that reordering the code might help. It should instead explain the fundamental problem with what is being attempted.

This issue is moot if rust-lang/rfcs#3697 is implemented.

Other cases

Rust Version

1.82.0

Anything else?

No response

@kpreid kpreid 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 Nov 12, 2024
joshtriplett added a commit to joshtriplett/rfcs that referenced this issue Nov 18, 2024
@joshtriplett
Copy link
Member

@kpreid It's not moot when RFC 3697 gets implemented. The error message is still misleading and should be improved. It'll just be able to say that the macro doesn't have any attr rules, rather than that this isn't possible.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 23, 2024
… r=fmease

Add specific diagnostic for using macro_rules macro as attribute/derive

Fixes rust-lang#132928
@bors bors closed this as completed in f7e3de3 Nov 23, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 23, 2024
Rollup merge of rust-lang#132949 - clubby789:macro-rules-attr-derive, r=fmease

Add specific diagnostic for using macro_rules macro as attribute/derive

Fixes rust-lang#132928
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 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.

2 participants