rust-lang / rust-lang/rust

`extern crate $crate` causes rust to read `lib$crate.rlib`

Open
#149,018 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-macros T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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 https://github.com/rust-lang/rust/issues/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

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 issue from main.rs with rustc main.rs -L crate=deps, keeping the empty deps/lib$crate.rlib file in place. Start by tracing rustc's handling of extern crate $crate during macro expansion and crate metadata loading. Done means rustc no longer attempts to read lib$crate.rlib or reports invalid metadata for this invalid crate name.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.