Skip to content

Confusing diagnostic when trying to invoke a macro in a module #141613

Open
@BigWingBeat

Description

@BigWingBeat

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

#57966

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

#136140

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-decl-macros-1-2Area: Declarative macros 1.2A-diagnosticsArea: Messages for errors, warnings, and lintsD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.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