Skip to content

Non-found macro_rules! should suggest moving if they actually exist in the same file #121061

Closed
@MultisampledNight

Description

@MultisampledNight

Code

fn main() {
    something_later!();
}

macro_rules! something_later {
    () => {
        println!("successfully expanded!");
    };
}

Current output

error: cannot find macro `something_later` in this scope
 --> src/main.rs:2:5
  |
2 |     something_later!();
  |     ^^^^^^^^^^^^^^^
  |
  = help: have you added the `#[macro_use]` on the module/import?

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

Desired output

error: cannot find macro `something_later` in this scope
 --> src/main.rs:2:5
  |
2 |     something_later!();
  |     ^^^^^^^^^^^^^^^ help: a macro with the same name exists, but it appears later
  |                     help: consider moving the definition of
  |                           `something_later` before this call
  |
  = help: have you added the `#[macro_use]` on the module/import?

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

Rationale and extra context

Often times,

  • the actual error of the attempted call and
  • the warning of the unused macro

can be apart and/or soaked in other errors. Due to this, it might be desirable to couple a help message addressing this specific situation of a later definition.
This might also be useful advice for beginners who are just starting out with Rust and might not be aware of the horrors of declarative macros using macro_rules!, and as such could appreciate a helping hand by the compiler here.

Other cases

No response

Rust Version

= rustc --version --verbose
rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6

Anything else?

I tried to keep the suggested output close to the one for similarly named macros/variables, but that might not be applicable here and instead deserve its own section under the code excerpt. Open for bikeshedding. Like all of this, actually. Hmm.

(The message is the same on nightly b381d3a 2024-02-12.)

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.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