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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {

let mut diag = struct_span_code_err!(self.dcx(), span, E0432, "{msg}");

if errors.iter().all(|(_, err)| err.segment == Some(kw::Underscore)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leads to inconsistent behavior in code like use {_, a}. I'd rather we filter out the errors mentioning _ like we're doing with that errors.retain call above.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And while we're at it, we probably should delay a bug in the:

if errors.is_empty() {
    return;
}

on 691.

// If we've encountered something like `use _;`, we've already emitted an error stating
// that `_` is not a valid identifier, so we silence the resolve error.
diag.downgrade_to_delayed_bug();
}

if let Some((_, UnresolvedImportError { note: Some(note), .. })) = errors.iter().last() {
diag.note(note.clone());
}
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this pr touches this test, could we also rename it with some more descriptive name and put a small comment with a link on an issue related to this instead?

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 `_`
}
Loading