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

Unhelpful error when macro_rules macro name is missing #118295

Closed
dtolnay opened this issue Nov 25, 2023 · 0 comments · Fixed by #118317
Closed

Unhelpful error when macro_rules macro name is missing #118295

dtolnay opened this issue Nov 25, 2023 · 0 comments · Fixed by #118317
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name resolution D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Nov 25, 2023

Code

macro_rules! {
    () => {};
}

Current output

error: cannot find macro `macro_rules` in this scope
 --> src/main.rs:1:1
  |
1 | macro_rules! {
  | ^^^^^^^^^^^

Desired output

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

Rationale and extra context

The syntax for how an ident is given to macro_rules! is unlike any other macro invocation. New Rust users would typically have more experience calling macros (println!) before defining their own macros. There may be a period in which the bespoke macro_rules syntax is tricky to build muscle memory for.

Other cases

When name of a function is omitted, this is still a better error than "cannot find 'fn' in this scope".

fn() {
}
error: expected identifier, found `(`
 --> src/lib.rs:1:3
  |
1 | fn() {
  |   ^ expected identifier
@dtolnay dtolnay 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 25, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 25, 2023
@fmease fmease added A-resolve Area: Name resolution A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 26, 2023
@bors bors closed this as completed in aefbbc6 Dec 7, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 7, 2023
Rollup merge of rust-lang#118317 - bvanjoi:fix-118295, r=petrochenkov

tip for define macro name after `macro_rules!`

Fixes rust-lang#118295

~Note that there are some bad case such as `macro_rules![]` or `macro_rules!()`. However, I think these are acceptable as they are likely to be seldom used (feel free to close this if you think its shortcomings outweigh its benefits)~

Edit: this problem was resolved by utilizing the `source_map.span_to_next_source`.

r? `@petrochenkov`
tgross35 added a commit to tgross35/rust that referenced this issue Jul 26, 2024
…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
```
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 A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name resolution D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. 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