Skip to content

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

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
N-Maas opened this issue Jun 19, 2024 · 1 comment · Fixed by #127557
Closed

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

N-Maas opened this issue Jun 19, 2024 · 1 comment · Fixed by #127557
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

@N-Maas
Copy link

N-Maas commented Jun 19, 2024

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

@N-Maas N-Maas 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 Jun 19, 2024
@linyihai
Copy link
Contributor

linyihai commented Jul 8, 2024

This issue seems to have been improved here, but the added tips do not have the conspicuous annotation information to enhance the tips.

As mentioned in #118295

error: `macro_rules` needs to be given a name for the macro you are defining
 --> src/main.rs:1:14
  |
1 | macro_rules! {
  |             ^ put a macro name here

@bors bors closed this as completed in c5788d6 Jul 26, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jul 26, 2024
Rollup merge of rust-lang#127557 - linyihai:issue-126694, r=compiler-errors

Add a label to point to the lacking macro name definition

Fixes rust-lang#126694, but adopts the suggestion from rust-lang#118295

```
 --> src/main.rs:1:14
  |
1 | macro_rules! {
  |            ^ put a macro name here
```
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