rust-lang / rust-lang/rust

Confusing diagnostic when trying to invoke a macro in a module

Open
#141,613 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-decl-macros-1-2 A-diagnostics D-lack-of-suggestion D-terse T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Behold the following:

mod inner {
    macro_rules! a {
        () => {}
    }
}

fn main() {
    inner::a!();
}

Attempting to compile this results in the compiler simultaneously telling you that the macro is unused (implying that it exists) and that it could not be found (implying that it does not exist):

error[E0433]: failed to resolve: could not find `a` in `inner`
 --> src/main.rs:8:12
  |
8 |     inner::a!();
  |            ^ could not find `a` in `inner`

warning: unused macro definition: `a`
 --> src/main.rs:2:18
  |
2 |     macro_rules! a {
  |                  ^
  |
  = note: `#[warn(unused_macros)]` on by default

Additionally, no suggested fix is provided, despite the fact that an extremely simple fix does exist (stackoverflow):

mod inner {
    macro_rules! a {
        () => {}
    }
    
+    pub(crate) use a;
}

fn main() {
    inner::a!();
}

This compiles and runs. Ideally, the compiler would directly suggest adding the use statement:

help: consider adding a public re-export for this macro
   |
6 +     pub(crate) use a;
   |

Changing the visibility of the use statement (e.g. to pub use a; or use a;) already results in much more helpful errors, depending on the context. The issue is specifically when no #[macro_export] or use statement exists.

Rust Version

rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

Potentially Related Issues

https://github.com/rust-lang/rust/issues/57966

  • Pertains to the case when the use statement is outside the macro's module, rather than in it.

https://github.com/rust-lang/rust/issues/136140

  • Pertains to the case when the #[macro_export] attribute is added, rather than a use statement.

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

Reproduce the supplied module-macro example with rustc 1.87.0 and compare the behavior described in related issues #57966 and #136140. Done means the unresolved macro diagnostic suggests a suitable pub(crate) use a; re-export while retaining the existing successful behavior.

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.