Skip to content

Commit 171c1fc

Browse files
committed
Auto merge of #57185 - petrochenkov:impice4, r=estebank
resolve: Fix one more ICE in import validation So if you have an unresolved import ```rust mod m { use foo::bar; } ``` error recovery will insert a special item with `Def::Err` definition into module `m`, so other things depending on `bar` won't produce extra errors. The issue was that erroneous `bar` was overwriting legitimate `bar`s coming from globs, e.g. ```rust mod m { use baz::*; // imports real existing `bar` use foo::bar; } ``` causing some unwanted diagnostics talking about "unresolved items", and producing inconsistent resolutions like #57015. This PR stops overwriting real successful resolutions with `Def::Err`s. Fixes #57015
2 parents a35cf79 + ddb550a commit 171c1fc

File tree

6 files changed

+33
-26
lines changed

6 files changed

+33
-26
lines changed

src/librustc_resolve/resolve_imports.rs

+4
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,10 @@ impl<'a> Resolver<'a> {
472472
self.set_binding_parent_module(binding, module);
473473
self.update_resolution(module, ident, ns, |this, resolution| {
474474
if let Some(old_binding) = resolution.binding {
475+
if binding.def() == Def::Err {
476+
// Do not override real bindings with `Def::Err`s from error recovery.
477+
return Ok(());
478+
}
475479
match (old_binding.is_glob_import(), binding.is_glob_import()) {
476480
(true, true) => {
477481
if binding.def() != old_binding.def() {

src/test/ui/imports/duplicate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ mod g {
3333
fn main() {
3434
e::foo();
3535
f::foo(); //~ ERROR `foo` is ambiguous
36-
g::foo(); //~ ERROR `foo` is ambiguous
36+
g::foo();
3737
}
3838

3939
mod ambiguous_module_errors {

src/test/ui/imports/duplicate.stderr

+1-20
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,6 @@ LL | pub use b::*;
5050
| ^^^^
5151
= help: consider adding an explicit import of `foo` to disambiguate
5252

53-
error[E0659]: `foo` is ambiguous (glob import vs glob import in the same module)
54-
--> $DIR/duplicate.rs:36:8
55-
|
56-
LL | g::foo(); //~ ERROR `foo` is ambiguous
57-
| ^^^ ambiguous name
58-
|
59-
note: `foo` could refer to the function imported here
60-
--> $DIR/duplicate.rs:29:13
61-
|
62-
LL | pub use a::*;
63-
| ^^^^
64-
= help: consider adding an explicit import of `foo` to disambiguate
65-
note: `foo` could also refer to the unresolved item imported here
66-
--> $DIR/duplicate.rs:30:13
67-
|
68-
LL | pub use f::*;
69-
| ^^^^
70-
= help: consider adding an explicit import of `foo` to disambiguate
71-
7253
error[E0659]: `foo` is ambiguous (glob import vs glob import in the same module)
7354
--> $DIR/duplicate.rs:49:9
7455
|
@@ -88,7 +69,7 @@ LL | use self::m2::*;
8869
| ^^^^^^^^^^^
8970
= help: consider adding an explicit import of `foo` to disambiguate
9071

91-
error: aborting due to 5 previous errors
72+
error: aborting due to 4 previous errors
9273

9374
Some errors occurred: E0252, E0659.
9475
For more information about an error, try `rustc --explain E0252`.

src/test/ui/imports/issue-56125.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ LL | use issue_56125::*; //~ ERROR `issue_56125` is ambiguous
4242
|
4343
= note: `issue_56125` could refer to an extern crate passed with `--extern`
4444
= help: use `::issue_56125` to refer to this extern crate unambiguously
45-
note: `issue_56125` could also refer to the unresolved item imported here
46-
--> $DIR/issue-56125.rs:19:9
45+
note: `issue_56125` could also refer to the module imported here
46+
--> $DIR/issue-56125.rs:20:9
4747
|
48-
LL | use empty::issue_56125; //~ ERROR unresolved import `empty::issue_56125`
49-
| ^^^^^^^^^^^^^^^^^^
50-
= help: use `self::issue_56125` to refer to this unresolved item unambiguously
48+
LL | use issue_56125::*; //~ ERROR `issue_56125` is ambiguous
49+
| ^^^^^^^^^^^^^^
50+
= help: use `self::issue_56125` to refer to this module unambiguously
5151

5252
error: aborting due to 4 previous errors
5353

src/test/ui/imports/issue-57015.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
mod glob_ok {
2+
pub mod something {
3+
pub mod something_else {}
4+
}
5+
}
6+
7+
mod single_err {}
8+
9+
use glob_ok::*; // glob_ok::something
10+
use single_err::something; //~ ERROR unresolved import `single_err::something`
11+
use something::something_else;
12+
13+
fn main() {}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0432]: unresolved import `single_err::something`
2+
--> $DIR/issue-57015.rs:10:5
3+
|
4+
LL | use single_err::something; //~ ERROR unresolved import `single_err::something`
5+
| ^^^^^^^^^^^^^^^^^^^^^ no `something` in `single_err`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0432`.

0 commit comments

Comments
 (0)