From 93feaa668511cfd03a645217a2287699694002cf Mon Sep 17 00:00:00 2001
From: bohan <bohan-zhang@foxmail.com>
Date: Thu, 6 Jun 2024 16:09:20 +0800
Subject: [PATCH] mark binding undetermined if target name exist and not
 obtained

---
 compiler/rustc_resolve/src/ident.rs           | 10 +++++-----
 tests/ui/imports/cycle-import-in-std-1.rs     |  9 +++++++++
 tests/ui/imports/cycle-import-in-std-1.stderr | 13 +++++++++++++
 tests/ui/imports/cycle-import-in-std-2.rs     |  9 +++++++++
 tests/ui/imports/cycle-import-in-std-2.stderr | 13 +++++++++++++
 5 files changed, 49 insertions(+), 5 deletions(-)
 create mode 100644 tests/ui/imports/cycle-import-in-std-1.rs
 create mode 100644 tests/ui/imports/cycle-import-in-std-1.stderr
 create mode 100644 tests/ui/imports/cycle-import-in-std-2.rs
 create mode 100644 tests/ui/imports/cycle-import-in-std-2.stderr

diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs
index 78bd3c4e49f..b6a23317dc9 100644
--- a/compiler/rustc_resolve/src/ident.rs
+++ b/compiler/rustc_resolve/src/ident.rs
@@ -998,14 +998,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
             let Some(module) = single_import.imported_module.get() else {
                 return Err((Undetermined, Weak::No));
             };
-            let ImportKind::Single { source: ident, source_bindings, .. } = &single_import.kind
+            let ImportKind::Single { source: ident, target, target_bindings, .. } =
+                &single_import.kind
             else {
                 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,
+            if (ident != target) && target_bindings.iter().all(|binding| binding.get().is_none()) {
+                // This branch allows the binding to be defined or updated later if the target name
+                // can hide the source but these bindings are not obtained.
                 // avoiding module inconsistency between the resolve process and the finalize process.
                 // See more details in #124840
                 return Err((Undetermined, Weak::No));
diff --git a/tests/ui/imports/cycle-import-in-std-1.rs b/tests/ui/imports/cycle-import-in-std-1.rs
new file mode 100644
index 00000000000..2fa492c155d
--- /dev/null
+++ b/tests/ui/imports/cycle-import-in-std-1.rs
@@ -0,0 +1,9 @@
+//@ edition: 2018
+
+// https://github.com/rust-lang/rust/issues/124490
+
+use ops::{self as std};
+//~^ ERROR: unresolved import `ops`
+use std::collections::{self as ops};
+
+fn main() {}
diff --git a/tests/ui/imports/cycle-import-in-std-1.stderr b/tests/ui/imports/cycle-import-in-std-1.stderr
new file mode 100644
index 00000000000..d4e6f32cc10
--- /dev/null
+++ b/tests/ui/imports/cycle-import-in-std-1.stderr
@@ -0,0 +1,13 @@
+error[E0432]: unresolved import `ops`
+  --> $DIR/cycle-import-in-std-1.rs:5:11
+   |
+LL | use ops::{self as std};
+   |           ^^^^^^^^^^^ no external crate `ops`
+   |
+   = help: consider importing one of these items instead:
+           core::ops
+           std::ops
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0432`.
diff --git a/tests/ui/imports/cycle-import-in-std-2.rs b/tests/ui/imports/cycle-import-in-std-2.rs
new file mode 100644
index 00000000000..d73c57944bc
--- /dev/null
+++ b/tests/ui/imports/cycle-import-in-std-2.rs
@@ -0,0 +1,9 @@
+//@ edition: 2018
+
+// https://github.com/rust-lang/rust/issues/125013
+
+use ops::{self as std};
+//~^ ERROR: unresolved import `ops`
+use std::ops::Deref::{self as ops};
+
+fn main() {}
diff --git a/tests/ui/imports/cycle-import-in-std-2.stderr b/tests/ui/imports/cycle-import-in-std-2.stderr
new file mode 100644
index 00000000000..dc0270dffe4
--- /dev/null
+++ b/tests/ui/imports/cycle-import-in-std-2.stderr
@@ -0,0 +1,13 @@
+error[E0432]: unresolved import `ops`
+  --> $DIR/cycle-import-in-std-2.rs:5:11
+   |
+LL | use ops::{self as std};
+   |           ^^^^^^^^^^^ no external crate `ops`
+   |
+   = help: consider importing one of these items instead:
+           core::ops
+           std::ops
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0432`.