Skip to content

Commit 6425815

Browse files
committed
Auto merge of rust-lang#124840 - bvanjoi:fix-124490, r=<try>
resolve: mark it undetermined if single import is not has any bindings - Fixes rust-lang#124490 - Fixes rust-lang#125013 This issue arises from incorrect resolution updates, for example: ```rust mod a { pub mod b { pub mod c {} } } use a::*; use b::c; use c as b; fn main() {} ``` 1. In the first loop, binding `(root, b)` is refer to `root::a::b` due to `use a::*`. 1. However, binding `(root, c)` isn't defined by `use b::c` during this stage because `use c as b` falls under the `single_imports` of `(root, b)`, where the `imported_module` hasn't been computed yet. This results in marking the `path_res` for `b` as `Indeterminate`. 2. Then, the `imported_module` for `use c as b` will be recorded. 2. In the second loop, `use b::c` will be processed again: 1. Firstly, it attempts to find the `path_res` for `(root, b)`. 2. It will iterate through the `single_imports` of `use b::c`, encounter `use c as b`, attempt to resolve `c` in `root`, and ultimately return `Err(Undetermined)`, thus passing the iterator. 3. Use the binding `(root, b)` -> `root::a::b` introduced by `use a::*` and ultimately return `root::a::b` as the `path_res` of `b`. 4. Then define the binding `(root, c)` -> `root::a::b::c`. 3. Then process `use c as b`, update the resolution for `(root, b)` to refer to `root::a::b::c`, ultimately causing inconsistency. In my view, step `2.2` has an issue where it should exit early, similar to the behavior when there's no `imported_module`. Therefore, I've added an attribute called `indeterminate` to `ImportData`. This will help us handle only those single imports that have at least one determined binding. r? `@petrochenkov`
2 parents f2e1a3a + 8714888 commit 6425815

9 files changed

+139
-20
lines changed

compiler/rustc_resolve/src/ident.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,21 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
965965
// if it can then our result is not determined and can be invalidated.
966966
for single_import in &resolution.single_imports {
967967
let Some(import_vis) = single_import.vis.get() else {
968+
// This branch handles a cycle in single imports, which occurs
969+
// when we've previously captured the `vis` value during an import
970+
// process.
971+
//
972+
// For example:
973+
// ```
974+
// use a::b;
975+
// use b as a;
976+
// ```
977+
// 1. Steal the `vis` in `use a::b` and attempt to locate `a` in the
978+
// current module.
979+
// 2. Encounter the import `use b as a`, which is a `single_import` for `a`,
980+
// and try to find `b` in the current module.
981+
// 3. Re-encounter the `use a::b` import since it's a `single_import` of `b`.
982+
// This leads to entering this branch.
968983
continue;
969984
};
970985
if !self.is_accessible_from(import_vis, parent_scope.module) {
@@ -979,15 +994,22 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
979994
// named imports.
980995
continue;
981996
}
997+
982998
let Some(module) = single_import.imported_module.get() else {
983999
return Err((Undetermined, Weak::No));
9841000
};
985-
let ImportKind::Single { source: ident, .. } = single_import.kind else {
1001+
let ImportKind::Single { source: ident, source_bindings, .. } = &single_import.kind
1002+
else {
9861003
unreachable!();
9871004
};
1005+
if binding.map_or(false, |binding| binding.module().is_some())
1006+
&& source_bindings.iter().all(|binding| matches!(binding.get(), Err(Undetermined)))
1007+
{
1008+
return Err((Undetermined, Weak::No));
1009+
}
9881010
match self.resolve_ident_in_module(
9891011
module,
990-
ident,
1012+
*ident,
9911013
ns,
9921014
&single_import.parent_scope,
9931015
None,

compiler/rustc_resolve/src/imports.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
352352
(old_glob @ true, false) | (old_glob @ false, true) => {
353353
let (glob_binding, nonglob_binding) =
354354
if old_glob { (old_binding, binding) } else { (binding, old_binding) };
355-
if glob_binding.res() != nonglob_binding.res()
356-
&& key.ns == MacroNS
355+
if key.ns == MacroNS
357356
&& nonglob_binding.expansion != LocalExpnId::ROOT
357+
&& glob_binding.res() != nonglob_binding.res()
358358
{
359359
resolution.binding = Some(this.ambiguity(
360360
AmbiguityKind::GlobVsExpanded,

tests/crashes/124490.rs

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ check-pass
2+
3+
// https://github.com/rust-lang/rust/pull/124840#issuecomment-2098148587
4+
5+
mod a {
6+
pub(crate) use crate::S;
7+
}
8+
mod b {
9+
pub struct S;
10+
}
11+
use self::a::S;
12+
use self::b::*;
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ check-pass
2+
3+
// similar `cycle-import-in-diff-module-0.rs`
4+
5+
mod a {
6+
pub(crate) use crate::s;
7+
}
8+
mod b {
9+
pub mod s {}
10+
}
11+
use self::b::*;
12+
use self::a::s;
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// https://github.com/rust-lang/rust/issues/124490
2+
3+
mod a {
4+
pub mod b {
5+
pub mod c {}
6+
}
7+
}
8+
9+
use a::*;
10+
11+
use b::c;
12+
//~^ ERROR: cannot determine resolution for the import
13+
//~| ERROR: cannot determine resolution for the import
14+
//~| ERROR: unresolved import `b::c`
15+
use c as b;
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: cannot determine resolution for the import
2+
--> $DIR/shadow-glob-module-resolution-1.rs:11:5
3+
|
4+
LL | use b::c;
5+
| ^^^^
6+
7+
error: cannot determine resolution for the import
8+
--> $DIR/shadow-glob-module-resolution-1.rs:11:5
9+
|
10+
LL | use b::c;
11+
| ^^^^
12+
|
13+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
14+
15+
error[E0432]: unresolved import `b::c`
16+
--> $DIR/shadow-glob-module-resolution-1.rs:11:5
17+
|
18+
LL | use b::c;
19+
| ^^^^
20+
21+
error: aborting due to 3 previous errors
22+
23+
For more information about this error, try `rustc --explain E0432`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://github.com/rust-lang/rust/issues/125013
2+
3+
mod a {
4+
pub mod b {
5+
pub mod c {
6+
pub trait D {}
7+
}
8+
}
9+
}
10+
11+
use a::*;
12+
13+
use e as b;
14+
//~^ ERROR: unresolved import `e`
15+
use b::c::D as e;
16+
//~^ ERROR: cannot determine resolution for the import
17+
//~| ERROR: cannot determine resolution for the import
18+
19+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: cannot determine resolution for the import
2+
--> $DIR/shadow-glob-module-resolution-2.rs:15:5
3+
|
4+
LL | use b::c::D as e;
5+
| ^^^^^^^^^^^^
6+
7+
error: cannot determine resolution for the import
8+
--> $DIR/shadow-glob-module-resolution-2.rs:15:5
9+
|
10+
LL | use b::c::D as e;
11+
| ^^^^^^^^^^^^
12+
|
13+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
14+
15+
error[E0432]: unresolved import `e`
16+
--> $DIR/shadow-glob-module-resolution-2.rs:13:5
17+
|
18+
LL | use e as b;
19+
| -^^^^^
20+
| |
21+
| no `e` in the root
22+
| help: a similar name exists in the module: `a`
23+
24+
error: aborting due to 3 previous errors
25+
26+
For more information about this error, try `rustc --explain E0432`.

0 commit comments

Comments
 (0)