Skip to content

Confusing error when name for macro definition (macro_rules!) is missing #126694

Closed
@N-Maas

Description

@N-Maas

Code

macro_rules! {
    () => {};
}

Current output

error: cannot find macro `macro_rules` in this scope
 --> src/lib.rs:1:1
  |
1 | macro_rules! {
  | ^^^^^^^^^^^
  |
note: maybe you have forgotten to define a name for this `macro_rules!`
 --> src/lib.rs:1:1
  |
1 | macro_rules! {
  | ^^^^^^^^^^^

Desired output

error: missing name for `macro_rules!`
 --> src/lib.rs:1:1
  |
1 | macro_rules! {
  | ^^^^^^^^^^^
  |
note: maybe you have forgotten the name for this macro definition
 --> src/lib.rs:1:1
  |
1 | macro_rules! name_of_macro {
  |              ^^^^^^^^^^^^^

Rationale and extra context

The error is confusing since it looks like some kind of import is needed to use macro_rules.
While reading the note carefully could clear this up, it is not optimal since the wording of the note is IMO still a bit confusing (I couldn't directly map "name for this macro_rules!" to "you forgot to give your macro a name"), especially since the error seems to state something completely different. Furthermore, the suggested fix is not optimal since you need to remember the syntax even if you understand the error. This could be easily fixed, though (see desired output).

I'm aware that the error itself might be harder to change if macro_rules! could actually refer to a different macro in theory (?). In this case, I think adjusting the note similar to the suggestion should be sufficient.

Note: I ran into this since it is the snippet provided by rust analyzer for macro_rules!. Since the snippet directly caused the error and I didn't have the correct syntax remembered, I needed a bit of time to find the fix.

Other cases

No response

Rust Version

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7

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