rust-lang / rust-lang/rust

Ambiguous macro call is expanded anyway

Open
#145,196 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal reproduction in src/lib.rs and run it on the reported nightly version. Trace resolution of the final foo!() call and compare the expansion with the E0659 ambiguity diagnostic; done means the ambiguous call is not expanded as one candidate.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.