Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ebc10a4

Browse files
committedMay 16, 2024·
resolve: mark it undetermined if single import is not has any bindings
1 parent 1871252 commit ebc10a4

10 files changed

+147
-18
lines changed
 

Diff for: ‎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(),
@@ -871,6 +872,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
871872
root_id: item.id,
872873
parent_scope: self.parent_scope,
873874
imported_module: Cell::new(module),
875+
indeterminate: Cell::new(false),
874876
has_attributes: !item.attrs.is_empty(),
875877
use_span_with_attributes: item.span_with_attributes(),
876878
use_span: item.span,
@@ -1072,6 +1074,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
10721074
root_id: item.id,
10731075
parent_scope: this.parent_scope,
10741076
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
1077+
indeterminate: Cell::new(false),
10751078
use_span_with_attributes: item.span_with_attributes(),
10761079
has_attributes: !item.attrs.is_empty(),
10771080
use_span: item.span,
@@ -1236,6 +1239,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
12361239
root_id: item.id,
12371240
parent_scope: self.parent_scope,
12381241
imported_module: Cell::new(None),
1242+
indeterminate: Cell::new(false),
12391243
has_attributes: false,
12401244
use_span_with_attributes: span,
12411245
use_span: span,

Diff for: ‎compiler/rustc_resolve/src/ident.rs

+21
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,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
981996
// named imports.
982997
continue;
983998
}
999+
1000+
if single_import.indeterminate.get()
1001+
&& binding.map_or(false, |binding| binding.module().is_some())
1002+
{
1003+
return Err((Undetermined, Weak::No));
1004+
}
9841005
let Some(module) = single_import.imported_module.get() else {
9851006
return Err((Undetermined, Weak::No));
9861007
};

Diff for: ‎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

Diff for: ‎tests/crashes/124490.rs

-16
This file was deleted.

Diff for: ‎tests/ui/imports/cycle-import-in-diff-module-0.rs

+14
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() {}

Diff for: ‎tests/ui/imports/cycle-import-in-diff-module-1.rs

+14
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() {}

Diff for: ‎tests/ui/imports/shadow-glob-module-resolution-1.rs

+17
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() {}
+23
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`.

Diff for: ‎tests/ui/imports/shadow-glob-module-resolution-2.rs

+19
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() { }
+26
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)
Please sign in to comment.