Skip to content

Ambiguous macro call is expanded anyway #145196

@theemathas

Description

@theemathas

I tried this code:

macro_rules! foo {
    () => {}
}

macro_rules! make_foo {
    () => {
        macro_rules! foo {
            () => {
                compile_error!("why is this expanded?");
            }
        }
    }
}

make_foo!();

foo!();

I got the following compiler output:

error: why is this expanded?
  --> src/lib.rs:9:17
   |
 9 |                 compile_error!("why is this expanded?");
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
17 | foo!();
   | ------ in this macro invocation
   |
   = note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0659]: `foo` is ambiguous
  --> src/lib.rs:17:1
   |
17 | foo!();
   | ^^^ ambiguous name
   |
   = note: ambiguous because of a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution
note: `foo` could refer to the macro defined here
  --> src/lib.rs:7:9
   |
 7 | /         macro_rules! foo {
 8 | |             () => {
 9 | |                 compile_error!("why is this expanded?");
10 | |             }
11 | |         }
   | |_________^
...
15 |   make_foo!();
   |   ----------- in this macro invocation
note: `foo` could also refer to the macro defined here
  --> src/lib.rs:1:1
   |
 1 | / macro_rules! foo {
 2 | |     () => {}
 3 | | }
   | |_^
   = note: this error originates in the macro `make_foo` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: unused macro definition: `foo`
 --> src/lib.rs:1:14
  |
1 | macro_rules! foo {
  |              ^^^
  |
  = note: `#[warn(unused_macros)]` on by default

For more information about this error, try `rustc --explain E0659`.
warning: `playground` (lib) generated 1 warning
error: could not compile `playground` (lib) due to 2 previous errors; 1 warning emitted

It seems incorrect that foo!() would be expanded, since the macro call is ambiguous, so we don't know which macro call is the right one.

@rustbot labels +A-macros

Meta

Reproducible on the playground with version 1.91.0-nightly (2025-08-09 ca77504943887037504c)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions