rust-lang / rust-lang/rust

non-`#[macro_export]`'ed `macro_rules!` macros are impossible to disambiguate from built-in attributes in `use` declarations

Open
#133,708 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-attributes A-macros A-resolve C-bug P-high T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

macro_rules! warn { () => {} }
pub(crate) use warn;
fn main() { }

I expected it to compile.

Instead, it produces an error like this:

error[E0659]: `warn` is ambiguous
 --> src/main.rs:3:16
  |
3 | pub(crate) use warn;
  |                ^^^^ ambiguous name
  |
  = note: ambiguous because of a name conflict with a builtin attribute
  = note: `warn` could refer to a built-in attribute
note: `warn` could also refer to the macro defined here
 --> src/main.rs:1:1
  |
1 | macro_rules! warn { () => {} }
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

macro_rules are impossible to qualify it as self::warn or crate::warn or super::warn.
use r#warn doesn't do anything useful too.

The only thing you can do is something stupid like this:

macro_rules! warn_hack { ($($tt:tt)*) => { warn!($($tt)*) } }
pub(crate) use warn_hack as warn;

This will work, the fact that it works like that is why I consider this a bug.

Meta

Occurs on all versions with --edition 2018 or later.

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 reported macro_rules! and pub(crate) use warn example with the Rust 2018 or later edition, then compare it with the warn_hack workaround. Investigate the compiler's handling of macro imports that conflict with built-in attributes; done should include a supported disambiguation path and regression coverage for the reported cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.