resolve: mark it undetermined if single import is not has any bindings
This commit is contained in:
parent
1689a5a531
commit
f67a0eb2b7
11 changed files with 142 additions and 41 deletions
|
@ -965,6 +965,21 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
||||||
// if it can then our result is not determined and can be invalidated.
|
// if it can then our result is not determined and can be invalidated.
|
||||||
for single_import in &resolution.single_imports {
|
for single_import in &resolution.single_imports {
|
||||||
let Some(import_vis) = single_import.vis.get() else {
|
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;
|
continue;
|
||||||
};
|
};
|
||||||
if !self.is_accessible_from(import_vis, parent_scope.module) {
|
if !self.is_accessible_from(import_vis, parent_scope.module) {
|
||||||
|
@ -979,15 +994,25 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
||||||
// named imports.
|
// named imports.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let Some(module) = single_import.imported_module.get() else {
|
let Some(module) = single_import.imported_module.get() else {
|
||||||
return Err((Undetermined, Weak::No));
|
return Err((Undetermined, Weak::No));
|
||||||
};
|
};
|
||||||
let ImportKind::Single { source: ident, .. } = single_import.kind else {
|
let ImportKind::Single { source: ident, source_bindings, .. } = &single_import.kind
|
||||||
|
else {
|
||||||
unreachable!();
|
unreachable!();
|
||||||
};
|
};
|
||||||
|
if binding.map_or(false, |binding| binding.module().is_some())
|
||||||
|
&& source_bindings.iter().all(|binding| matches!(binding.get(), Err(Undetermined)))
|
||||||
|
{
|
||||||
|
// This branch allows the binding to be defined or updated later,
|
||||||
|
// avoiding module inconsistency between the resolve process and the finalize process.
|
||||||
|
// See more details in #124840
|
||||||
|
return Err((Undetermined, Weak::No));
|
||||||
|
}
|
||||||
match self.resolve_ident_in_module(
|
match self.resolve_ident_in_module(
|
||||||
module,
|
module,
|
||||||
ident,
|
*ident,
|
||||||
ns,
|
ns,
|
||||||
&single_import.parent_scope,
|
&single_import.parent_scope,
|
||||||
None,
|
None,
|
||||||
|
|
|
@ -352,9 +352,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
||||||
(old_glob @ true, false) | (old_glob @ false, true) => {
|
(old_glob @ true, false) | (old_glob @ false, true) => {
|
||||||
let (glob_binding, nonglob_binding) =
|
let (glob_binding, nonglob_binding) =
|
||||||
if old_glob { (old_binding, binding) } else { (binding, old_binding) };
|
if old_glob { (old_binding, binding) } else { (binding, old_binding) };
|
||||||
if glob_binding.res() != nonglob_binding.res()
|
if key.ns == MacroNS
|
||||||
&& key.ns == MacroNS
|
|
||||||
&& nonglob_binding.expansion != LocalExpnId::ROOT
|
&& nonglob_binding.expansion != LocalExpnId::ROOT
|
||||||
|
&& glob_binding.res() != nonglob_binding.res()
|
||||||
{
|
{
|
||||||
resolution.binding = Some(this.ambiguity(
|
resolution.binding = Some(this.ambiguity(
|
||||||
AmbiguityKind::GlobVsExpanded,
|
AmbiguityKind::GlobVsExpanded,
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
//@ known-bug: rust-lang/rust#124490
|
|
||||||
use io::{self as std};
|
|
||||||
use std::collections::{self as io};
|
|
||||||
|
|
||||||
mod a {
|
|
||||||
pub mod b {
|
|
||||||
pub mod c {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
use a::*;
|
|
||||||
|
|
||||||
use b::c;
|
|
||||||
use c as b;
|
|
||||||
|
|
||||||
fn main() {}
|
|
|
@ -1,5 +0,0 @@
|
||||||
//@ known-bug: rust-lang/rust#125013
|
|
||||||
//@ edition:2021
|
|
||||||
use io::{self as std};
|
|
||||||
use std::ops::Deref::{self as io};
|
|
||||||
pub fn main() {}
|
|
|
@ -1,16 +0,0 @@
|
||||||
//@ known-bug: rust-lang/rust#125013
|
|
||||||
//@ edition:2021
|
|
||||||
mod a {
|
|
||||||
pub mod b {
|
|
||||||
pub mod c {
|
|
||||||
pub trait D {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
use a::*;
|
|
||||||
|
|
||||||
use e as b;
|
|
||||||
use b::c::D as e;
|
|
||||||
|
|
||||||
fn main() { }
|
|
14
tests/ui/imports/cycle-import-in-diff-module-0.rs
Normal file
14
tests/ui/imports/cycle-import-in-diff-module-0.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
//@ check-pass
|
||||||
|
|
||||||
|
// https://github.com/rust-lang/rust/pull/124840#issuecomment-2098148587
|
||||||
|
|
||||||
|
mod a {
|
||||||
|
pub(crate) use crate::S;
|
||||||
|
}
|
||||||
|
mod b {
|
||||||
|
pub struct S;
|
||||||
|
}
|
||||||
|
use self::a::S;
|
||||||
|
use self::b::*;
|
||||||
|
|
||||||
|
fn main() {}
|
14
tests/ui/imports/cycle-import-in-diff-module-1.rs
Normal file
14
tests/ui/imports/cycle-import-in-diff-module-1.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
//@ check-pass
|
||||||
|
|
||||||
|
// similar `cycle-import-in-diff-module-0.rs`
|
||||||
|
|
||||||
|
mod a {
|
||||||
|
pub(crate) use crate::s;
|
||||||
|
}
|
||||||
|
mod b {
|
||||||
|
pub mod s {}
|
||||||
|
}
|
||||||
|
use self::b::*;
|
||||||
|
use self::a::s;
|
||||||
|
|
||||||
|
fn main() {}
|
17
tests/ui/imports/shadow-glob-module-resolution-1.rs
Normal file
17
tests/ui/imports/shadow-glob-module-resolution-1.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
// https://github.com/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
tests/ui/imports/shadow-glob-module-resolution-1.stderr
Normal file
23
tests/ui/imports/shadow-glob-module-resolution-1.stderr
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
error: cannot determine resolution for the import
|
||||||
|
--> $DIR/shadow-glob-module-resolution-1.rs:11:5
|
||||||
|
|
|
||||||
|
LL | use b::c;
|
||||||
|
| ^^^^
|
||||||
|
|
||||||
|
error: cannot determine resolution for the import
|
||||||
|
--> $DIR/shadow-glob-module-resolution-1.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-1.rs:11:5
|
||||||
|
|
|
||||||
|
LL | use b::c;
|
||||||
|
| ^^^^
|
||||||
|
|
||||||
|
error: aborting due to 3 previous errors
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0432`.
|
19
tests/ui/imports/shadow-glob-module-resolution-2.rs
Normal file
19
tests/ui/imports/shadow-glob-module-resolution-2.rs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
// https://github.com/rust-lang/rust/issues/125013
|
||||||
|
|
||||||
|
mod a {
|
||||||
|
pub mod b {
|
||||||
|
pub mod c {
|
||||||
|
pub trait D {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
use a::*;
|
||||||
|
|
||||||
|
use e as b;
|
||||||
|
//~^ ERROR: unresolved import `e`
|
||||||
|
use b::c::D as e;
|
||||||
|
//~^ ERROR: cannot determine resolution for the import
|
||||||
|
//~| ERROR: cannot determine resolution for the import
|
||||||
|
|
||||||
|
fn main() { }
|
26
tests/ui/imports/shadow-glob-module-resolution-2.stderr
Normal file
26
tests/ui/imports/shadow-glob-module-resolution-2.stderr
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
error: cannot determine resolution for the import
|
||||||
|
--> $DIR/shadow-glob-module-resolution-2.rs:15:5
|
||||||
|
|
|
||||||
|
LL | use b::c::D as e;
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: cannot determine resolution for the import
|
||||||
|
--> $DIR/shadow-glob-module-resolution-2.rs:15:5
|
||||||
|
|
|
||||||
|
LL | use b::c::D as e;
|
||||||
|
| ^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
|
||||||
|
|
||||||
|
error[E0432]: unresolved import `e`
|
||||||
|
--> $DIR/shadow-glob-module-resolution-2.rs:13:5
|
||||||
|
|
|
||||||
|
LL | use e as b;
|
||||||
|
| -^^^^^
|
||||||
|
| |
|
||||||
|
| no `e` in the root
|
||||||
|
| help: a similar name exists in the module: `a`
|
||||||
|
|
||||||
|
error: aborting due to 3 previous errors
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0432`.
|
Loading…
Add table
Add a link
Reference in a new issue