Skip to content

Better help text for incorrect macro_rules! usage #150899

@carols10cents

Description

@carols10cents

Code

macro_rules!(i_think_the_name_should_go_here) {
    () => {}
}

Current output

error: macros that expand to items must be delimited with braces or followed by a semicolon
 --> src/lib.rs:1:13
  |
1 | macro_rules!(i_think_the_name_should_go_here) {
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
help: change the delimiters to curly braces
  |
1 - macro_rules!(i_think_the_name_should_go_here) {
1 + macro_rules!{i_think_the_name_should_go_here} {
  |
help: add a semicolon
  |
1 | macro_rules!(i_think_the_name_should_go_here); {
  |                                              +

error: expected item, found `{`
 --> src/lib.rs:1:47
  |
1 | macro_rules!(i_think_the_name_should_go_here) {
  |                                               ^ expected item
  |
  = note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>

error: could not compile `playground` (lib) due to 2 previous errors

Desired output

help: to define a macro, remove the parentheses around the macro name
  |
1 - macro_rules!(i_think_the_name_should_go_here) {
1 + macro_rules! i_think_the_name_should_go_here {
  |

Rationale and extra context

I was trying to define a macro from memory, and my memory failed me 😅 My brain thought the syntax was macro_rules!(macro_name) {}, but the parentheses can't go there.

I have no idea how common of a mistake this is and if it's worth special casing; I'm happy with whatever judgment call yinz make :)

Other cases

Rust Version

$ rustc --version --verbose
rustc 1.91.1 (ed61e7d7e 2025-11-07)
binary: rustc
commit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb
commit-date: 2025-11-07
host: aarch64-apple-darwin
release: 1.91.1
LLVM version: 21.1.2

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)T-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