rust-lang / rust-lang/rust

Incorrect error message when `decl_macro` macro refers to `macro_rules!` macro

Open
#148,414 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Playground link

#![feature(decl_macro)]

macro_rules! inner {
    () => {}
}

macro outer() {
    inner!();
}

outer!();

This code results in this confusing and completely incorrect error message:

error: cannot find macro `inner` in this scope
  --> src/lib.rs:10:5
   |
10 |     inner!();
   |     ^^^^^ consider moving the definition of `inner` before this call
...
15 | outer!();
   | -------- in this macro invocation
   |
note: a macro with the same name exists, but it appears later
  --> src/lib.rs:5:14
   |
 5 | macro_rules! inner {
   |              ^^^^^
   = note: this error originates in the macro `outer` (in Nightly builds, run with -Z macro-backtrace for more info)

Obviously, the definition of inner doesn't "appear later" than the definition of outer. In fact, the problem has nothing to do with definition order. The real fix is to add use inner; anywhere after the definition of inner.

(As a side note, I don't know if this is the intended behavior of macros 2.0, or a bug / current limitation of the nightly feature)

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 by running the linked Rust Playground example on nightly and compare the diagnostic with the reported output. Trace the compiler's diagnostic and name-resolution handling for a decl_macro referring to a macro_rules! macro. Done means the error no longer incorrectly attributes the problem to definition order and instead explains the required import or actual limitation.

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.