Skip to content

Commit 98f6e96

Browse files
authored
Rollup merge of #112495 - bvanjoi:fix-109153, r=petrochenkov
fix(resolve): update shadowed_glob more precision - Fixes #109153 - Fixes #109962 ## Why does it panic? We use #109153 as an illustration. The process of `resolve_imports` is: | Iter | resolve | resolution of **`(Mod(root), Ident(bar) in type ns)`** | | - | - | - | | 0 | `use foo::*` | `binding` -> foo::bar, `shallowed_glob` -> `None` | | 1 | `use bar::bar` | `binding` -> foo::bar::bar, `shallowed_glob` -> foo::bar | | 2 | `use bar::*` | `binding` -> foo::bar::bar, `shallowed_glob` -> foo::bar::bar::bar | So during `finalize_import`, the `root::bar` in `use bar::bar` had been pointed to `foo::bar::bar::bar`, which is different from the `initial_module` valued of `foo::bar`, therefore, the panic had been triggered. ## Try to solve it ~I think #109153 should check-pass rather than throw an ambiguous error. Following this idea, there are two ways to solve this problem:~ ~1. Give up the `initial_module` and update `import.imported_module` after each resolution update. However, I think this method may have too much impact.~ ~2. Do not update the `shadowed_glob` when it is defined.~ ~To be honest, I am not sure if this is the right way to solve this ICE. Perhaps there is a better resolution.~ Edit: we had made the `resolution.shadowed_glob` update more detailed. r? `@petrochenkov`
2 parents 6fc50da + f7330eb commit 98f6e96

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

compiler/rustc_resolve/src/imports.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,21 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
338338
} else {
339339
resolution.binding = Some(nonglob_binding);
340340
}
341-
resolution.shadowed_glob = Some(glob_binding);
341+
342+
if let Some(old_binding) = resolution.shadowed_glob {
343+
assert!(old_binding.is_glob_import());
344+
if glob_binding.res() != old_binding.res() {
345+
resolution.shadowed_glob = Some(this.ambiguity(
346+
AmbiguityKind::GlobVsGlob,
347+
old_binding,
348+
glob_binding,
349+
));
350+
} else if !old_binding.vis.is_at_least(binding.vis, this.tcx) {
351+
resolution.shadowed_glob = Some(glob_binding);
352+
}
353+
} else {
354+
resolution.shadowed_glob = Some(glob_binding);
355+
}
342356
}
343357
(false, false) => {
344358
return Err(old_binding);

tests/ui/resolve/issue-105069.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ error[E0659]: `V` is ambiguous
44
LL | use V;
55
| ^ ambiguous name
66
|
7-
= note: ambiguous because of multiple potential import sources
7+
= note: ambiguous because of multiple glob imports of a name in the same module
88
note: `V` could refer to the variant imported here
99
--> $DIR/issue-105069.rs:1:5
1010
|
1111
LL | use self::A::*;
1212
| ^^^^^^^^^^
13+
= help: consider adding an explicit import of `V` to disambiguate
1314
note: `V` could also refer to the variant imported here
1415
--> $DIR/issue-105069.rs:3:5
1516
|
1617
LL | use self::B::*;
1718
| ^^^^^^^^^^
19+
= help: consider adding an explicit import of `V` to disambiguate
1820

1921
error: aborting due to previous error
2022

tests/ui/resolve/issue-109153.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use foo::*;
2+
3+
mod foo {
4+
pub mod bar {
5+
pub mod bar {
6+
pub mod bar {}
7+
}
8+
}
9+
}
10+
11+
use bar::bar; //~ ERROR `bar` is ambiguous
12+
use bar::*;
13+
14+
fn main() { }

tests/ui/resolve/issue-109153.stderr

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0659]: `bar` is ambiguous
2+
--> $DIR/issue-109153.rs:11:5
3+
|
4+
LL | use bar::bar;
5+
| ^^^ ambiguous name
6+
|
7+
= note: ambiguous because of multiple glob imports of a name in the same module
8+
note: `bar` could refer to the module imported here
9+
--> $DIR/issue-109153.rs:1:5
10+
|
11+
LL | use foo::*;
12+
| ^^^^^^
13+
= help: consider adding an explicit import of `bar` to disambiguate
14+
note: `bar` could also refer to the module imported here
15+
--> $DIR/issue-109153.rs:12:5
16+
|
17+
LL | use bar::*;
18+
| ^^^^^^
19+
= help: consider adding an explicit import of `bar` to disambiguate
20+
21+
error: aborting due to previous error
22+
23+
For more information about this error, try `rustc --explain E0659`.

0 commit comments

Comments
 (0)