rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
99.13k stars 12.8k forks source link

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

Open kanashimia opened 1 day ago

kanashimia commented 1 day ago

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.

fmease commented 1 day ago

Triage: Slightly "better" reproducer (where we don't try to define warn multiple times):

macro_rules! warn { () => {} }
use warn as warning;
fn main() {}