Skip to content

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

Closed
@kpreid

Description

@kpreid

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions