Skip to content

Unhelpful error when macro_rules macro name is missing #118295

Closed
@dtolnay

Description

@dtolnay

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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