Skip to content

Leading underscore in path makes rust suggest to add a crate named _ with edition >= 2018 #142662

Closed
@Veykril

Description

@Veykril
Member

Code

use _::a;
use _::*;

fn main() {
    use _::a;
    use _::*;
}

Current output

⣿
Errors

Exited with status 101

Standard Error

   Compiling playground v0.0.1 (/playground)
error: expected identifier, found reserved identifier `_`
 --> src/main.rs:1:5
  |
1 | use _::a;
  |     ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
 --> src/main.rs:2:5
  |
2 | use _::*;
  |     ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
 --> src/main.rs:5:9
  |
5 |     use _::a;
  |         ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
 --> src/main.rs:6:9
  |
6 |     use _::*;
  |         ^ expected identifier, found reserved identifier

error[E0432]: unresolved import `_`
 --> src/main.rs:2:5
  |
2 | use _::*;
  |     ^ use of unresolved module or unlinked crate `_`
  |
  = help: if you wanted to use a crate named `_`, use `cargo add _` to add it to your `Cargo.toml`

error[E0432]: unresolved import `_`
 --> src/main.rs:1:5
  |
1 | use _::a;
  |     ^ use of unresolved module or unlinked crate `_`
  |
  = help: if you wanted to use a crate named `_`, use `cargo add _` to add it to your `Cargo.toml`

error[E0432]: unresolved import `_`
 --> src/main.rs:5:9
  |
5 |     use _::a;
  |         ^ use of unresolved module or unlinked crate `_`
  |
  = help: if you wanted to use a crate named `_`, use `cargo add _` to add it to your `Cargo.toml`

error[E0432]: unresolved import `_`
 --> src/main.rs:6:9
  |
6 |     use _::*;
  |         ^ use of unresolved module or unlinked crate `_`
  |
  = help: if you wanted to use a crate named `_`, use `cargo add _` to add it to your `Cargo.toml`

For more information about this error, try `rustc --explain E0432`.
error: could not compile `playground` (bin "playground") due to 8 previous errors

Standard Output

Desired output

⣿
Errors

Exited with status 101

Standard Error

   Compiling playground v0.0.1 (/playground)
error: expected identifier, found reserved identifier `_`
 --> src/main.rs:1:5
  |
1 | use _::a;
  |     ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
 --> src/main.rs:2:5
  |
2 | use _::*;
  |     ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
 --> src/main.rs:5:9
  |
5 |     use _::a;
  |         ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
 --> src/main.rs:6:9
  |
6 |     use _::*;
  |         ^ expected identifier, found reserved identifier

For more information about this error, try `rustc --explain E0432`.
error: could not compile `playground` (bin "playground") due to 8 previous errors

Standard Output

Rationale and extra context

crates cannot be named _ in Rust. Note this only happens with edition 2018 and beyond.

This was discovered by the ferrocene team while running the test suite with edition 2018, specifically with the test tests/ui/underscore-imports/issue-110164.rs output changing and showing this regression.

Other cases

Rust Version

rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

Anything else?

No response

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Jun 18, 2025
JonathanBrouwer

JonathanBrouwer commented on Jun 18, 2025

@JonathanBrouwer
Contributor

From what I'm seeing, crates can be named _ in Rust.
This succeeds:

cargo new _
cd _
cargo check
Veykril

Veykril commented on Jun 18, 2025

@Veykril
MemberAuthor

Oh indeed, you can even specify it as a dependency, but naming that dependency is impossible in Rust. Interesting, well then I am not sure about this, I still think this suggestion is wrong given you cannot name a _ named dependency.

added a commit that references this issue on Jun 24, 2025

Rollup merge of rust-lang#142805 - estebank:underscore-import, r=comp…

e752d09
added a commit that references this issue on Jun 24, 2025

Rollup merge of rust-lang#142805 - estebank:underscore-import, r=comp…

d7e10f0
added a commit that references this issue on Jun 25, 2025
feccc6b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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

      Participants

      @Veykril@JonathanBrouwer

      Issue actions

        Leading underscore in path makes rust suggest to add a crate named `_` with edition >= 2018 · Issue #142662 · rust-lang/rust