Skip to content

use $crate::{self}; should not be accepted #146967

@theemathas

Description

@theemathas

tried this code:

macro_rules! foo { () => { use $crate::{self}; }; } foo!();

I expected a compile error of some sort. Instead, it compiles fine.

Based on the error message when I try to import it twice, I'm guessing that it's importing the current crate with the name $crate.

Importing it twice
macro_rules! foo { () => { use $crate::{self}; }; } foo!(); foo!(); fn main() {}
error[E0252]: the name `$crate` is defined multiple times --> src/main.rs:3:22 | 3 | use $crate::{self}; | -------------^^^^-- | | | | | `$crate` reimported here | | previous import of the module `$crate` here | help: remove unnecessary import ... 7 | foo!(); | ------ in this macro invocation | = note: `$crate` must be defined only once in the type namespace of this module = note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info) 

I am also able to use this import via the name $crate:

Using the import
macro_rules! foo { () => { use $crate::{self}; use crate::$crate as lol; }; } foo!(); fn wtf() { println!("wtf"); } fn main() { lol::wtf(); // this compiles }

Surely, this $crate identifier should not be able to be used as an actual name?

See also #146968 for more nonsense with $crate.

Meta

Reproducible on the playground with version 1.92.0-nightly (2025-09-23 975e6c8fec280816d24f)

Metadata

Metadata

Assignees

Labels

A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions