Skip to content

Commit d82fb1e

Browse files
committed
Emit a single error when importing a path with _
When encountering `use _;`, `use _::*'` or similar, do not emit two errors for that single mistake. This also side-steps the issue of resolve errors suggesting adding a crate named `_` to `Cargo.toml`.
1 parent a56bf51 commit d82fb1e

9 files changed

+138
-81
lines changed

compiler/rustc_resolve/src/imports.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
608608
}
609609
}
610610

611-
self.throw_unresolved_import_error(errors, glob_error);
611+
if !errors.is_empty() {
612+
self.throw_unresolved_import_error(errors, glob_error);
613+
}
612614
}
613615

614616
pub(crate) fn check_hidden_glob_reexports(
@@ -688,14 +690,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
688690
Some(def_id) if self.mods_with_parse_errors.contains(&def_id) => false,
689691
_ => true,
690692
});
693+
errors.retain(|(_import, err)| {
694+
// If we've encountered something like `use _;`, we've already emitted an error stating
695+
// that `_` is not a valid identifier, so we ignore that resolve error.
696+
err.segment != Some(kw::Underscore)
697+
});
698+
691699
if errors.is_empty() {
700+
self.tcx.dcx().delayed_bug("expected a parse or \"`_` can't be an identifier\" error");
692701
return;
693702
}
694703

695-
/// Upper limit on the number of `span_label` messages.
696-
const MAX_LABEL_COUNT: usize = 10;
697-
698704
let span = MultiSpan::from_spans(errors.iter().map(|(_, err)| err.span).collect());
705+
699706
let paths = errors
700707
.iter()
701708
.map(|(import, err)| {
@@ -715,6 +722,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
715722
diag.note(note.clone());
716723
}
717724

725+
/// Upper limit on the number of `span_label` messages.
726+
const MAX_LABEL_COUNT: usize = 10;
727+
718728
for (import, err) in errors.into_iter().take(MAX_LABEL_COUNT) {
719729
if let Some(label) = err.label {
720730
diag.span_label(err.span, label);

tests/ui/imports/multiple-extern-by-macro-for-underscore.ed2015.stderr

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,5 @@ error: expected identifier, found reserved identifier `_`
44
LL | use ::_;
55
| ^ expected identifier, found reserved identifier
66

7-
error[E0432]: unresolved import `_`
8-
--> $DIR/multiple-extern-by-macro-for-underscore.rs:18:9
9-
|
10-
LL | use ::_;
11-
| ^^^ no `_` in the root
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
148

15-
For more information about this error, try `rustc --explain E0432`.

tests/ui/imports/multiple-extern-by-macro-for-underscore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ macro_rules! m {
1515
m!();
1616

1717
fn main() {
18-
use ::_; //[ed2015]~ ERROR: unresolved import `_`
18+
use ::_;
1919
//~^ ERROR: expected identifier, found reserved identifier `_`
2020
}

tests/ui/underscore-imports/issue-110164.ed2015.stderr

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ LL | use _::a;
55
| ^ expected identifier, found reserved identifier
66

77
error: expected identifier, found reserved identifier `_`
8-
--> $DIR/issue-110164.rs:11:5
8+
--> $DIR/issue-110164.rs:10:5
99
|
1010
LL | use _::*;
1111
| ^ expected identifier, found reserved identifier
1212

1313
error: expected identifier, found reserved identifier `_`
14-
--> $DIR/issue-110164.rs:16:9
14+
--> $DIR/issue-110164.rs:14:9
1515
|
1616
LL | use _::a;
1717
| ^ expected identifier, found reserved identifier
1818

1919
error: expected identifier, found reserved identifier `_`
20-
--> $DIR/issue-110164.rs:19:9
20+
--> $DIR/issue-110164.rs:16:9
2121
|
2222
LL | use _::*;
2323
| ^ expected identifier, found reserved identifier
@@ -34,30 +34,6 @@ error[E0432]: unresolved import `crate::*`
3434
LL | use crate::*;
3535
| ^^^^^^^^ cannot glob-import a module into itself
3636

37-
error[E0432]: unresolved import `_`
38-
--> $DIR/issue-110164.rs:11:5
39-
|
40-
LL | use _::*;
41-
| ^ `_` is not a valid crate or module name
42-
43-
error[E0432]: unresolved import `_`
44-
--> $DIR/issue-110164.rs:8:5
45-
|
46-
LL | use _::a;
47-
| ^ `_` is not a valid crate or module name
48-
49-
error[E0432]: unresolved import `_`
50-
--> $DIR/issue-110164.rs:16:9
51-
|
52-
LL | use _::a;
53-
| ^ `_` is not a valid crate or module name
54-
55-
error[E0432]: unresolved import `_`
56-
--> $DIR/issue-110164.rs:19:9
57-
|
58-
LL | use _::*;
59-
| ^ `_` is not a valid crate or module name
60-
61-
error: aborting due to 10 previous errors
37+
error: aborting due to 6 previous errors
6238

6339
For more information about this error, try `rustc --explain E0432`.

tests/ui/underscore-imports/issue-110164.ed2021.stderr

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ LL | use _::a;
55
| ^ expected identifier, found reserved identifier
66

77
error: expected identifier, found reserved identifier `_`
8-
--> $DIR/issue-110164.rs:11:5
8+
--> $DIR/issue-110164.rs:10:5
99
|
1010
LL | use _::*;
1111
| ^ expected identifier, found reserved identifier
1212

1313
error: expected identifier, found reserved identifier `_`
14-
--> $DIR/issue-110164.rs:16:9
14+
--> $DIR/issue-110164.rs:14:9
1515
|
1616
LL | use _::a;
1717
| ^ expected identifier, found reserved identifier
1818

1919
error: expected identifier, found reserved identifier `_`
20-
--> $DIR/issue-110164.rs:19:9
20+
--> $DIR/issue-110164.rs:16:9
2121
|
2222
LL | use _::*;
2323
| ^ expected identifier, found reserved identifier
@@ -34,38 +34,6 @@ error[E0432]: unresolved import `crate::*`
3434
LL | use crate::*;
3535
| ^^^^^^^^ cannot glob-import a module into itself
3636

37-
error[E0432]: unresolved import `_`
38-
--> $DIR/issue-110164.rs:11:5
39-
|
40-
LL | use _::*;
41-
| ^ use of unresolved module or unlinked crate `_`
42-
|
43-
= help: you might be missing a crate named `_`
44-
45-
error[E0432]: unresolved import `_`
46-
--> $DIR/issue-110164.rs:8:5
47-
|
48-
LL | use _::a;
49-
| ^ use of unresolved module or unlinked crate `_`
50-
|
51-
= help: you might be missing a crate named `_`
52-
53-
error[E0432]: unresolved import `_`
54-
--> $DIR/issue-110164.rs:16:9
55-
|
56-
LL | use _::a;
57-
| ^ use of unresolved module or unlinked crate `_`
58-
|
59-
= help: you might be missing a crate named `_`
60-
61-
error[E0432]: unresolved import `_`
62-
--> $DIR/issue-110164.rs:19:9
63-
|
64-
LL | use _::*;
65-
| ^ use of unresolved module or unlinked crate `_`
66-
|
67-
= help: you might be missing a crate named `_`
68-
69-
error: aborting due to 10 previous errors
37+
error: aborting due to 6 previous errors
7038

7139
For more information about this error, try `rustc --explain E0432`.

tests/ui/underscore-imports/issue-110164.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ use crate::*;
77
//~^ ERROR unresolved import `crate::*`
88
use _::a;
99
//~^ ERROR expected identifier, found reserved identifier `_`
10-
//~| ERROR unresolved import `_`
1110
use _::*;
1211
//~^ ERROR expected identifier, found reserved identifier `_`
13-
//~| ERROR unresolved import `_`
1412

1513
fn main() {
1614
use _::a;
1715
//~^ ERROR expected identifier, found reserved identifier `_`
18-
//~| ERROR unresolved import `_`
1916
use _::*;
2017
//~^ ERROR expected identifier, found reserved identifier `_`
21-
//~| ERROR unresolved import `_`
2218
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error: expected identifier, found reserved identifier `_`
2+
--> $DIR/multiple-uses.rs:4:9
3+
|
4+
LL | pub use _::{a, b};
5+
| ^ expected identifier, found reserved identifier
6+
7+
error: expected identifier, found reserved identifier `_`
8+
--> $DIR/multiple-uses.rs:6:18
9+
|
10+
LL | pub use std::{a, _};
11+
| ^ expected identifier, found reserved identifier
12+
13+
error: expected identifier, found reserved identifier `_`
14+
--> $DIR/multiple-uses.rs:9:18
15+
|
16+
LL | pub use std::{b, _, c};
17+
| ^ expected identifier, found reserved identifier
18+
19+
error: expected identifier, found reserved identifier `_`
20+
--> $DIR/multiple-uses.rs:12:15
21+
|
22+
LL | pub use std::{_, d};
23+
| ^ expected identifier, found reserved identifier
24+
25+
error[E0432]: unresolved import `std::a`
26+
--> $DIR/multiple-uses.rs:6:15
27+
|
28+
LL | pub use std::{a, _};
29+
| ^ no `a` in the root
30+
31+
error[E0432]: unresolved imports `std::b`, `std::c`
32+
--> $DIR/multiple-uses.rs:9:15
33+
|
34+
LL | pub use std::{b, _, c};
35+
| ^ ^
36+
| | |
37+
| | no `c` in the root
38+
| | help: a similar name exists in the module: `rc`
39+
| no `b` in the root
40+
41+
error[E0432]: unresolved import `std::d`
42+
--> $DIR/multiple-uses.rs:12:18
43+
|
44+
LL | pub use std::{_, d};
45+
| ^ no `d` in the root
46+
47+
error: aborting due to 7 previous errors
48+
49+
For more information about this error, try `rustc --explain E0432`.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error: expected identifier, found reserved identifier `_`
2+
--> $DIR/multiple-uses.rs:4:9
3+
|
4+
LL | pub use _::{a, b};
5+
| ^ expected identifier, found reserved identifier
6+
7+
error: expected identifier, found reserved identifier `_`
8+
--> $DIR/multiple-uses.rs:6:18
9+
|
10+
LL | pub use std::{a, _};
11+
| ^ expected identifier, found reserved identifier
12+
13+
error: expected identifier, found reserved identifier `_`
14+
--> $DIR/multiple-uses.rs:9:18
15+
|
16+
LL | pub use std::{b, _, c};
17+
| ^ expected identifier, found reserved identifier
18+
19+
error: expected identifier, found reserved identifier `_`
20+
--> $DIR/multiple-uses.rs:12:15
21+
|
22+
LL | pub use std::{_, d};
23+
| ^ expected identifier, found reserved identifier
24+
25+
error[E0432]: unresolved import `std::a`
26+
--> $DIR/multiple-uses.rs:6:15
27+
|
28+
LL | pub use std::{a, _};
29+
| ^ no `a` in the root
30+
31+
error[E0432]: unresolved imports `std::b`, `std::c`
32+
--> $DIR/multiple-uses.rs:9:15
33+
|
34+
LL | pub use std::{b, _, c};
35+
| ^ ^
36+
| | |
37+
| | no `c` in the root
38+
| | help: a similar name exists in the module: `rc`
39+
| no `b` in the root
40+
41+
error[E0432]: unresolved import `std::d`
42+
--> $DIR/multiple-uses.rs:12:18
43+
|
44+
LL | pub use std::{_, d};
45+
| ^ no `d` in the root
46+
47+
error: aborting due to 7 previous errors
48+
49+
For more information about this error, try `rustc --explain E0432`.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ revisions: ed2015 ed2021
2+
//@[ed2015] edition: 2015
3+
//@[ed2021] edition: 2021
4+
pub use _::{a, b};
5+
//~^ ERROR expected identifier, found reserved identifier `_`
6+
pub use std::{a, _};
7+
//~^ ERROR expected identifier, found reserved identifier `_`
8+
//~| ERROR unresolved import `std::a`
9+
pub use std::{b, _, c};
10+
//~^ ERROR expected identifier, found reserved identifier `_`
11+
//~| ERROR unresolved imports `std::b`, `std::c`
12+
pub use std::{_, d};
13+
//~^ ERROR expected identifier, found reserved identifier `_`
14+
//~| ERROR unresolved import `std::d`
15+
16+
fn main() {}

0 commit comments

Comments
 (0)