Skip to content

Give a better error when using an undeclared macro variable #95943

Open
@jyn514

Description

@jyn514

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a17456ca7139db13e945cbde87ae6fbd

macro_rules! m {
    () => {
        $x;
    }
}

fn foo() {
    m!()
}

The current output is:

error: expected expression, found `$`
 --> src/lib.rs:3:9
  |
3 |         $x;
  |         ^^ expected expression
...
8 |     m!()
  |     ---- in this macro invocation
  |
  = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)

Ideally the output should look like:

error[E0425]: cannot find macro parameter `$x` in this scope
 --> src/lib.rs:8:5
  |
2 |    () => {
  |    ^^ in this macro matcher
3 |     $x;
  |     ^^ not found in this scope
...
8 |     m!()
  |     ---- in this macro invocation
  |
  = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)

@rustbot label +A-macros

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, ..)E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.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