rust-lang / rust-lang/reference

Document macro_use shadow restriction

Open
#2,207 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros
Dominant language
Rust
Stars
1.6k
Forks
607
PR merge metrics
PR metrics pending

Description

There seems to be a restriction that macro_use cannot shadow a macro_use macro when expanded from within a macro_rules macro.

#[macro_use(foo)] extern crate m;

macro_rules! m1 { () => {
    #[macro_use(foo)] //~ ERROR `foo` is already in scope
    extern crate m as _;
}}
m1!();

emits:

error: `foo` is already in scope
  ╭▸ src/lib.rs:4:17
  │
4 │     #[macro_use(foo)] //~ ERROR `foo` is already in scope
  │                 ━━━
  ‡
7 │ m1!();
  │ ───── in this macro invocation
  │
  ├ note: macro-expanded `#[macro_use]`s may not shadow existing macros (see RFC 1560)
  ╰ note: this error originates in the macro `m1` (in Nightly builds, run with -Z macro-backtrace for more info)

I couldn't find a rule (or combination of rules) that would cover this situation.

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 provided macro_rules!/macro_use example and the diagnostic's reference to RFC 1560. Locate the Rust Reference section covering macro_use and document the applicable shadowing restriction, including whether macro-expanded attributes are covered; done when the example's behavior and rule are clearly explained.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.