Skip to content

Emit a single error when importing a path with _ #142805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}

self.throw_unresolved_import_error(errors, glob_error);
if !errors.is_empty() {
self.throw_unresolved_import_error(errors, glob_error);
}
}

pub(crate) fn check_hidden_glob_reexports(
Expand Down Expand Up @@ -688,14 +690,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
Some(def_id) if self.mods_with_parse_errors.contains(&def_id) => false,
_ => true,
});
errors.retain(|(_import, err)| {
// If we've encountered something like `use _;`, we've already emitted an error stating
// that `_` is not a valid identifier, so we ignore that resolve error.
err.segment != Some(kw::Underscore)
});

if errors.is_empty() {
self.tcx.dcx().delayed_bug("expected a parse or \"`_` can't be an identifier\" error");
return;
}

/// Upper limit on the number of `span_label` messages.
const MAX_LABEL_COUNT: usize = 10;

let span = MultiSpan::from_spans(errors.iter().map(|(_, err)| err.span).collect());

let paths = errors
.iter()
.map(|(import, err)| {
Expand All @@ -715,6 +722,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
diag.note(note.clone());
}

/// Upper limit on the number of `span_label` messages.
const MAX_LABEL_COUNT: usize = 10;

for (import, err) in errors.into_iter().take(MAX_LABEL_COUNT) {
if let Some(label) = err.label {
diag.span_label(err.span, label);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: expected identifier, found reserved identifier `_`
--> $DIR/multiple-extern-by-macro-for-underscore.rs:16:11
--> $DIR/multiple-extern-by-macro-for-underscore.rs:18:11
|
LL | use ::_;
| ^ expected identifier, found reserved identifier
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected identifier, found reserved identifier `_`
--> $DIR/multiple-extern-by-macro-for-underscore.rs:18:11
|
LL | use ::_;
| ^ expected identifier, found reserved identifier

error: aborting due to 1 previous error

4 changes: 3 additions & 1 deletion tests/ui/imports/multiple-extern-by-macro-for-underscore.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//@ edition: 2021
//@ revisions: ed2015 ed2021
//@[ed2015] edition: 2015
//@[ed2021] edition: 2021

// issue#128813

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
error: expected identifier, found reserved identifier `_`
--> $DIR/issue-110164.rs:5:5
--> $DIR/issue-110164.rs:8:5
|
LL | use _::a;
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/issue-110164.rs:8:5
--> $DIR/issue-110164.rs:10:5
|
LL | use _::*;
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/issue-110164.rs:13:9
--> $DIR/issue-110164.rs:14:9
|
LL | use _::a;
| ^ expected identifier, found reserved identifier
Expand All @@ -23,41 +23,17 @@ LL | use _::*;
| ^ expected identifier, found reserved identifier

error[E0432]: unresolved import `self::*`
--> $DIR/issue-110164.rs:1:5
--> $DIR/issue-110164.rs:4:5
|
LL | use self::*;
| ^^^^^^^ cannot glob-import a module into itself

error[E0432]: unresolved import `crate::*`
--> $DIR/issue-110164.rs:3:5
--> $DIR/issue-110164.rs:6:5
|
LL | use crate::*;
| ^^^^^^^^ cannot glob-import a module into itself

error[E0432]: unresolved import `_`
--> $DIR/issue-110164.rs:8:5
|
LL | use _::*;
| ^ `_` is not a valid crate or module name

error[E0432]: unresolved import `_`
--> $DIR/issue-110164.rs:5:5
|
LL | use _::a;
| ^ `_` is not a valid crate or module name

error[E0432]: unresolved import `_`
--> $DIR/issue-110164.rs:13:9
|
LL | use _::a;
| ^ `_` is not a valid crate or module name

error[E0432]: unresolved import `_`
--> $DIR/issue-110164.rs:16:9
|
LL | use _::*;
| ^ `_` is not a valid crate or module name

error: aborting due to 10 previous errors
error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0432`.
39 changes: 39 additions & 0 deletions tests/ui/underscore-imports/issue-110164.ed2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
error: expected identifier, found reserved identifier `_`
--> $DIR/issue-110164.rs:8:5
|
LL | use _::a;
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/issue-110164.rs:10:5
|
LL | use _::*;
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/issue-110164.rs:14:9
|
LL | use _::a;
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/issue-110164.rs:16:9
|
LL | use _::*;
| ^ expected identifier, found reserved identifier

error[E0432]: unresolved import `self::*`
--> $DIR/issue-110164.rs:4:5
|
LL | use self::*;
| ^^^^^^^ cannot glob-import a module into itself

error[E0432]: unresolved import `crate::*`
--> $DIR/issue-110164.rs:6:5
|
LL | use crate::*;
| ^^^^^^^^ cannot glob-import a module into itself

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0432`.
7 changes: 3 additions & 4 deletions tests/ui/underscore-imports/issue-110164.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
//@ revisions: ed2015 ed2021
//@[ed2015] edition: 2015
//@[ed2021] edition: 2021
use self::*;
//~^ ERROR unresolved import `self::*`
use crate::*;
//~^ ERROR unresolved import `crate::*`
use _::a;
//~^ ERROR expected identifier, found reserved identifier `_`
//~| ERROR unresolved import `_`
use _::*;
//~^ ERROR expected identifier, found reserved identifier `_`
//~| ERROR unresolved import `_`

fn main() {
use _::a;
//~^ ERROR expected identifier, found reserved identifier `_`
//~| ERROR unresolved import `_`
use _::*;
//~^ ERROR expected identifier, found reserved identifier `_`
//~| ERROR unresolved import `_`
}
49 changes: 49 additions & 0 deletions tests/ui/underscore-imports/multiple-uses.ed2015.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
error: expected identifier, found reserved identifier `_`
--> $DIR/multiple-uses.rs:4:9
|
LL | pub use _::{a, b};
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/multiple-uses.rs:6:18
|
LL | pub use std::{a, _};
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/multiple-uses.rs:9:18
|
LL | pub use std::{b, _, c};
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/multiple-uses.rs:12:15
|
LL | pub use std::{_, d};
| ^ expected identifier, found reserved identifier

error[E0432]: unresolved import `std::a`
--> $DIR/multiple-uses.rs:6:15
|
LL | pub use std::{a, _};
| ^ no `a` in the root

error[E0432]: unresolved imports `std::b`, `std::c`
--> $DIR/multiple-uses.rs:9:15
|
LL | pub use std::{b, _, c};
| ^ ^
| | |
| | no `c` in the root
| | help: a similar name exists in the module: `rc`
| no `b` in the root

error[E0432]: unresolved import `std::d`
--> $DIR/multiple-uses.rs:12:18
|
LL | pub use std::{_, d};
| ^ no `d` in the root

error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0432`.
49 changes: 49 additions & 0 deletions tests/ui/underscore-imports/multiple-uses.ed2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
error: expected identifier, found reserved identifier `_`
--> $DIR/multiple-uses.rs:4:9
|
LL | pub use _::{a, b};
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/multiple-uses.rs:6:18
|
LL | pub use std::{a, _};
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/multiple-uses.rs:9:18
|
LL | pub use std::{b, _, c};
| ^ expected identifier, found reserved identifier

error: expected identifier, found reserved identifier `_`
--> $DIR/multiple-uses.rs:12:15
|
LL | pub use std::{_, d};
| ^ expected identifier, found reserved identifier

error[E0432]: unresolved import `std::a`
--> $DIR/multiple-uses.rs:6:15
|
LL | pub use std::{a, _};
| ^ no `a` in the root

error[E0432]: unresolved imports `std::b`, `std::c`
--> $DIR/multiple-uses.rs:9:15
|
LL | pub use std::{b, _, c};
| ^ ^
| | |
| | no `c` in the root
| | help: a similar name exists in the module: `rc`
| no `b` in the root

error[E0432]: unresolved import `std::d`
--> $DIR/multiple-uses.rs:12:18
|
LL | pub use std::{_, d};
| ^ no `d` in the root

error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0432`.
16 changes: 16 additions & 0 deletions tests/ui/underscore-imports/multiple-uses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//@ revisions: ed2015 ed2021
//@[ed2015] edition: 2015
//@[ed2021] edition: 2021
pub use _::{a, b};
//~^ ERROR expected identifier, found reserved identifier `_`
pub use std::{a, _};
//~^ ERROR expected identifier, found reserved identifier `_`
//~| ERROR unresolved import `std::a`
pub use std::{b, _, c};
//~^ ERROR expected identifier, found reserved identifier `_`
//~| ERROR unresolved imports `std::b`, `std::c`
pub use std::{_, d};
//~^ ERROR expected identifier, found reserved identifier `_`
//~| ERROR unresolved import `std::d`

fn main() {}
Loading