Skip to content

Commit fdd89b8

Browse files
committed
resolve: mark it undetermined if single import is not has any bindings
1 parent 60a7c19 commit fdd89b8

File tree

7 files changed

+80
-10
lines changed

7 files changed

+80
-10
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+4
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
355355
parent_scope: self.parent_scope,
356356
module_path,
357357
imported_module: Cell::new(None),
358+
indeterminate: Cell::new(true),
358359
span,
359360
use_span: item.span,
360361
use_span_with_attributes: item.span_with_attributes(),
@@ -869,6 +870,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
869870
root_id: item.id,
870871
parent_scope: self.parent_scope,
871872
imported_module: Cell::new(module),
873+
indeterminate: Cell::new(true),
872874
has_attributes: !item.attrs.is_empty(),
873875
use_span_with_attributes: item.span_with_attributes(),
874876
use_span: item.span,
@@ -1070,6 +1072,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
10701072
root_id: item.id,
10711073
parent_scope: this.parent_scope,
10721074
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
1075+
indeterminate: Cell::new(false),
10731076
use_span_with_attributes: item.span_with_attributes(),
10741077
has_attributes: !item.attrs.is_empty(),
10751078
use_span: item.span,
@@ -1234,6 +1237,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
12341237
root_id: item.id,
12351238
parent_scope: self.parent_scope,
12361239
imported_module: Cell::new(None),
1240+
indeterminate: Cell::new(true),
12371241
has_attributes: false,
12381242
use_span_with_attributes: span,
12391243
use_span: span,

compiler/rustc_resolve/src/ident.rs

+19
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,21 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
967967
// if it can then our result is not determined and can be invalidated.
968968
for single_import in &resolution.single_imports {
969969
let Some(import_vis) = single_import.vis.get() else {
970+
// This branch handles a cycle in single imports, which occurs
971+
// when we've previously captured the `vis` value during an import
972+
// process.
973+
//
974+
// For example:
975+
// ```
976+
// use a::b;
977+
// use b as a;
978+
// ```
979+
// 1. Steal the `vis` in `use a::b` and attempt to locate `a` in the
980+
// current module.
981+
// 2. Encounter the import `use b as a`, which is a `single_import` for `a`,
982+
// and try to find `b` in the current module.
983+
// 3. Re-encounter the `use a::b` import since it's a `single_import` of `b`.
984+
// This leads to entering this branch.
970985
continue;
971986
};
972987
if !self.is_accessible_from(import_vis, parent_scope.module) {
@@ -981,6 +996,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
981996
// named imports.
982997
continue;
983998
}
999+
1000+
if single_import.indeterminate.get() {
1001+
return Err((Undetermined, Weak::No));
1002+
}
9841003
let Some(module) = single_import.imported_module.get() else {
9851004
return Err((Undetermined, Weak::No));
9861005
};

compiler/rustc_resolve/src/imports.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ pub(crate) struct ImportData<'a> {
173173
pub module_path: Vec<Segment>,
174174
/// The resolution of `module_path`.
175175
pub imported_module: Cell<Option<ModuleOrUniformRoot<'a>>>,
176+
/// `true` if this import is indeterminate in all namespaces.
177+
pub indeterminate: Cell<bool>,
176178
pub vis: Cell<Option<ty::Visibility>>,
177179
pub used: Cell<Option<Used>>,
178180
}
@@ -351,9 +353,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
351353
(old_glob @ true, false) | (old_glob @ false, true) => {
352354
let (glob_binding, nonglob_binding) =
353355
if old_glob { (old_binding, binding) } else { (binding, old_binding) };
354-
if glob_binding.res() != nonglob_binding.res()
355-
&& key.ns == MacroNS
356+
if key.ns == MacroNS
356357
&& nonglob_binding.expansion != LocalExpnId::ROOT
358+
&& glob_binding.res() != nonglob_binding.res()
357359
{
358360
resolution.binding = Some(this.ambiguity(
359361
AmbiguityKind::GlobVsExpanded,
@@ -794,6 +796,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
794796
..
795797
} => (source, target, source_bindings, target_bindings, type_ns_only),
796798
ImportKind::Glob { .. } => {
799+
import.indeterminate.set(false);
797800
self.resolve_glob_import(import);
798801
return 0;
799802
}
@@ -844,6 +847,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
844847
}
845848
});
846849

850+
if indeterminate_count < 3 {
851+
import.indeterminate.set(false);
852+
}
853+
847854
indeterminate_count
848855
}
849856

tests/ui/imports/import-loop-2.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0432]: unresolved import `a::x`
2-
--> $DIR/import-loop-2.rs:8:13
1+
error[E0432]: unresolved import `b::x`
2+
--> $DIR/import-loop-2.rs:4:13
33
|
4-
LL | pub use a::x;
5-
| ^^^^ no `x` in `a`
4+
LL | pub use b::x;
5+
| ^^^^ no `x` in `b`
66

77
error: aborting due to 1 previous error
88

tests/ui/imports/import4.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0432]: unresolved import `a::foo`
2-
--> $DIR/import4.rs:5:17
1+
error[E0432]: unresolved import `b::foo`
2+
--> $DIR/import4.rs:4:17
33
|
4-
LL | mod b { pub use a::foo; }
5-
| ^^^^^^ no `foo` in `a`
4+
LL | mod a { pub use b::foo; }
5+
| ^^^^^^ no `foo` in `b`
66

77
error: aborting due to 1 previous error
88

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.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.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.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`.

0 commit comments

Comments
 (0)