Skip to content

extern crate $crate causes rust to read lib$crate.rlib #149018

@theemathas

Description

@theemathas

main.rs:

macro_rules! foo { () => { extern crate $crate; }; } foo!(); fn main() {}

There's also an empty file at deps/lib$crate.rlib.

I run the command: rustc main.rs -L crate=deps. I get the following error:

error: expected identifier, found reserved identifier `$crate` --> main.rs:3:22 | 3 | extern crate $crate; | ^^^^^^ expected identifier, found reserved identifier ... 6 | foo!(); | ------ in this macro invocation | = note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0786]: found invalid metadata files for crate `$crate` --> main.rs:3:9 | 3 | extern crate $crate; | ^^^^^^^^^^^^^^^^^^^^ ... 6 | foo!(); | ------ in this macro invocation | = note: failed to mmap file 'deps/lib$crate.rlib': memory map must have a non-zero length = note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0786`. 

Since $crate is not a valid crate name, rustc probably should not be reading the lib$crate.rlib file at all. And based on the error message, rustc is actually even trying to parse that file.

See also #146967, which features a similar issue with use.

Meta

rustc --version --verbose:

rustc 1.91.1 (ed61e7d7e 2025-11-07) binary: rustc commit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb commit-date: 2025-11-07 host: aarch64-apple-darwin release: 1.91.1 LLVM version: 21.1.2 

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions