Skip to content

Commit d7e10f0

Browse files
authored
Rollup merge of #142805 - estebank:underscore-import, r=compiler-errors
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`. Fix #142662.
2 parents 03c2197 + d82fb1e commit d7e10f0

10 files changed

+188
-40
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.stderr renamed to tests/ui/imports/multiple-extern-by-macro-for-underscore.ed2015.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: expected identifier, found reserved identifier `_`
2-
--> $DIR/multiple-extern-by-macro-for-underscore.rs:16:11
2+
--> $DIR/multiple-extern-by-macro-for-underscore.rs:18:11
33
|
44
LL | use ::_;
55
| ^ expected identifier, found reserved identifier
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected identifier, found reserved identifier `_`
2+
--> $DIR/multiple-extern-by-macro-for-underscore.rs:18:11
3+
|
4+
LL | use ::_;
5+
| ^ expected identifier, found reserved identifier
6+
7+
error: aborting due to 1 previous error
8+

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//@ edition: 2021
1+
//@ revisions: ed2015 ed2021
2+
//@[ed2015] edition: 2015
3+
//@[ed2021] edition: 2021
24

35
// issue#128813
46

Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
error: expected identifier, found reserved identifier `_`
2-
--> $DIR/issue-110164.rs:5:5
2+
--> $DIR/issue-110164.rs:8:5
33
|
44
LL | use _::a;
55
| ^ expected identifier, found reserved identifier
66

77
error: expected identifier, found reserved identifier `_`
8-
--> $DIR/issue-110164.rs:8: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:13:9
14+
--> $DIR/issue-110164.rs:14:9
1515
|
1616
LL | use _::a;
1717
| ^ expected identifier, found reserved identifier
@@ -23,41 +23,17 @@ LL | use _::*;
2323
| ^ expected identifier, found reserved identifier
2424

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

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

37-
error[E0432]: unresolved import `_`
38-
--> $DIR/issue-110164.rs:8: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:5: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:13: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:16: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`.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error: expected identifier, found reserved identifier `_`
2+
--> $DIR/issue-110164.rs:8:5
3+
|
4+
LL | use _::a;
5+
| ^ expected identifier, found reserved identifier
6+
7+
error: expected identifier, found reserved identifier `_`
8+
--> $DIR/issue-110164.rs:10:5
9+
|
10+
LL | use _::*;
11+
| ^ expected identifier, found reserved identifier
12+
13+
error: expected identifier, found reserved identifier `_`
14+
--> $DIR/issue-110164.rs:14:9
15+
|
16+
LL | use _::a;
17+
| ^ expected identifier, found reserved identifier
18+
19+
error: expected identifier, found reserved identifier `_`
20+
--> $DIR/issue-110164.rs:16:9
21+
|
22+
LL | use _::*;
23+
| ^ expected identifier, found reserved identifier
24+
25+
error[E0432]: unresolved import `self::*`
26+
--> $DIR/issue-110164.rs:4:5
27+
|
28+
LL | use self::*;
29+
| ^^^^^^^ cannot glob-import a module into itself
30+
31+
error[E0432]: unresolved import `crate::*`
32+
--> $DIR/issue-110164.rs:6:5
33+
|
34+
LL | use crate::*;
35+
| ^^^^^^^^ cannot glob-import a module into itself
36+
37+
error: aborting due to 6 previous errors
38+
39+
For more information about this error, try `rustc --explain E0432`.
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1+
//@ revisions: ed2015 ed2021
2+
//@[ed2015] edition: 2015
3+
//@[ed2021] edition: 2021
14
use self::*;
25
//~^ ERROR unresolved import `self::*`
36
use crate::*;
47
//~^ ERROR unresolved import `crate::*`
58
use _::a;
69
//~^ ERROR expected identifier, found reserved identifier `_`
7-
//~| ERROR unresolved import `_`
810
use _::*;
911
//~^ ERROR expected identifier, found reserved identifier `_`
10-
//~| ERROR unresolved import `_`
1112

1213
fn main() {
1314
use _::a;
1415
//~^ ERROR expected identifier, found reserved identifier `_`
15-
//~| ERROR unresolved import `_`
1616
use _::*;
1717
//~^ ERROR expected identifier, found reserved identifier `_`
18-
//~| ERROR unresolved import `_`
1918
}
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)