Skip to content

Commit

Permalink
resolve: mark it undetermined if single import is not has any bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed May 7, 2024
1 parent 60a7c19 commit fdd89b8
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 10 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
parent_scope: self.parent_scope,
module_path,
imported_module: Cell::new(None),
indeterminate: Cell::new(true),
span,
use_span: item.span,
use_span_with_attributes: item.span_with_attributes(),
Expand Down Expand Up @@ -869,6 +870,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
root_id: item.id,
parent_scope: self.parent_scope,
imported_module: Cell::new(module),
indeterminate: Cell::new(true),
has_attributes: !item.attrs.is_empty(),
use_span_with_attributes: item.span_with_attributes(),
use_span: item.span,
Expand Down Expand Up @@ -1070,6 +1072,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
root_id: item.id,
parent_scope: this.parent_scope,
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
indeterminate: Cell::new(false),
use_span_with_attributes: item.span_with_attributes(),
has_attributes: !item.attrs.is_empty(),
use_span: item.span,
Expand Down Expand Up @@ -1234,6 +1237,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
root_id: item.id,
parent_scope: self.parent_scope,
imported_module: Cell::new(None),
indeterminate: Cell::new(true),
has_attributes: false,
use_span_with_attributes: span,
use_span: span,
Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,21 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
// if it can then our result is not determined and can be invalidated.
for single_import in &resolution.single_imports {
let Some(import_vis) = single_import.vis.get() else {
// This branch handles a cycle in single imports, which occurs
// when we've previously captured the `vis` value during an import
// process.
//
// For example:
// ```
// use a::b;
// use b as a;
// ```
// 1. Steal the `vis` in `use a::b` and attempt to locate `a` in the
// current module.
// 2. Encounter the import `use b as a`, which is a `single_import` for `a`,
// and try to find `b` in the current module.
// 3. Re-encounter the `use a::b` import since it's a `single_import` of `b`.
// This leads to entering this branch.
continue;
};
if !self.is_accessible_from(import_vis, parent_scope.module) {
Expand All @@ -981,6 +996,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
// named imports.
continue;
}

if single_import.indeterminate.get() {
return Err((Undetermined, Weak::No));
}
let Some(module) = single_import.imported_module.get() else {
return Err((Undetermined, Weak::No));
};
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ pub(crate) struct ImportData<'a> {
pub module_path: Vec<Segment>,
/// The resolution of `module_path`.
pub imported_module: Cell<Option<ModuleOrUniformRoot<'a>>>,
/// `true` if this import is indeterminate in all namespaces.
pub indeterminate: Cell<bool>,
pub vis: Cell<Option<ty::Visibility>>,
pub used: Cell<Option<Used>>,
}
Expand Down Expand Up @@ -351,9 +353,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
(old_glob @ true, false) | (old_glob @ false, true) => {
let (glob_binding, nonglob_binding) =
if old_glob { (old_binding, binding) } else { (binding, old_binding) };
if glob_binding.res() != nonglob_binding.res()
&& key.ns == MacroNS
if key.ns == MacroNS
&& nonglob_binding.expansion != LocalExpnId::ROOT
&& glob_binding.res() != nonglob_binding.res()
{
resolution.binding = Some(this.ambiguity(
AmbiguityKind::GlobVsExpanded,
Expand Down Expand Up @@ -794,6 +796,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
..
} => (source, target, source_bindings, target_bindings, type_ns_only),
ImportKind::Glob { .. } => {
import.indeterminate.set(false);
self.resolve_glob_import(import);
return 0;
}
Expand Down Expand Up @@ -844,6 +847,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
});

if indeterminate_count < 3 {
import.indeterminate.set(false);
}

indeterminate_count
}

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/imports/import-loop-2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0432]: unresolved import `a::x`
--> $DIR/import-loop-2.rs:8:13
error[E0432]: unresolved import `b::x`
--> $DIR/import-loop-2.rs:4:13
|
LL | pub use a::x;
| ^^^^ no `x` in `a`
LL | pub use b::x;
| ^^^^ no `x` in `b`

error: aborting due to 1 previous error

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/imports/import4.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0432]: unresolved import `a::foo`
--> $DIR/import4.rs:5:17
error[E0432]: unresolved import `b::foo`
--> $DIR/import4.rs:4:17
|
LL | mod b { pub use a::foo; }
| ^^^^^^ no `foo` in `a`
LL | mod a { pub use b::foo; }
| ^^^^^^ no `foo` in `b`

error: aborting due to 1 previous error

Expand Down
17 changes: 17 additions & 0 deletions tests/ui/imports/shadow-glob-module-resolution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// https:/rust-lang/rust/issues/124490

mod a {
pub mod b {
pub mod c {}
}
}

use a::*;

use b::c;
//~^ ERROR: cannot determine resolution for the import
//~| ERROR: cannot determine resolution for the import
//~| ERROR: unresolved import `b::c`
use c as b;

fn main() {}
23 changes: 23 additions & 0 deletions tests/ui/imports/shadow-glob-module-resolution.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error: cannot determine resolution for the import
--> $DIR/shadow-glob-module-resolution.rs:11:5
|
LL | use b::c;
| ^^^^

error: cannot determine resolution for the import
--> $DIR/shadow-glob-module-resolution.rs:11:5
|
LL | use b::c;
| ^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error[E0432]: unresolved import `b::c`
--> $DIR/shadow-glob-module-resolution.rs:11:5
|
LL | use b::c;
| ^^^^

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0432`.

0 comments on commit fdd89b8

Please sign in to comment.