about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2025-04-25 00:55:28 +0000
committerMichael Goulet <michael@errs.io>2025-04-28 17:15:11 +0000
commit105d1dcefd9baa520e63830f557d9c472f50d4fd (patch)
treee3fe976a3b1f4f0efd533d462d9f8166b92d2924
parenta932eb36f8adf6c8cdfc450f063943da3112d621 (diff)
downloadrust-105d1dcefd9baa520e63830f557d9c472f50d4fd.tar.gz
rust-105d1dcefd9baa520e63830f557d9c472f50d4fd.zip
Do not compute type_of for impl item if impl where clauses are unsatisfied
-rw-r--r--compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs6
-rw-r--r--tests/ui/async-await/in-trait/cycle-if-impl-doesnt-apply.rs39
-rw-r--r--tests/ui/impl-trait/in-trait/cycle-if-impl-doesnt-apply.rs32
3 files changed, 77 insertions, 0 deletions
diff --git a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs
index 119d197de13..fa8cfbba7fc 100644
--- a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs
+++ b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs
@@ -194,6 +194,12 @@ where
                 .map(|pred| goal.with(cx, pred));
             ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);
 
+            // Bail if the nested goals don't hold here. This is to avoid unnecessarily
+            // computing the `type_of` query for associated types that never apply, as
+            // this may result in query cycles in the case of RPITITs.
+            // See <https://github.com/rust-lang/trait-system-refactor-initiative/issues/185>.
+            ecx.try_evaluate_added_goals()?;
+
             // Add GAT where clauses from the trait's definition.
             // FIXME: We don't need these, since these are the type's own WF obligations.
             ecx.add_goals(
diff --git a/tests/ui/async-await/in-trait/cycle-if-impl-doesnt-apply.rs b/tests/ui/async-await/in-trait/cycle-if-impl-doesnt-apply.rs
new file mode 100644
index 00000000000..54992c98655
--- /dev/null
+++ b/tests/ui/async-await/in-trait/cycle-if-impl-doesnt-apply.rs
@@ -0,0 +1,39 @@
+//@ check-pass
+//@ revisions: current next
+//@ ignore-compare-mode-next-solver (explicit revisions)
+//@[next] compile-flags: -Znext-solver
+//@ edition: 2024
+
+// Regression test for <https://github.com/rust-lang/trait-system-refactor-initiative/issues/185>.
+// Avoid unnecessarily computing the RPITIT type of the first impl when checking the WF of the
+// second impl, since the first impl relies on the hidden type of the second impl.
+
+use std::future::Future;
+
+trait Handler {}
+
+struct W<T>(T);
+
+trait SendTarget {
+    fn call(self) -> impl Future<Output = ()> + Send;
+}
+
+impl<T> SendTarget for W<T>
+where
+    T: Handler + Send,
+{
+    async fn call(self) {
+        todo!()
+    }
+}
+
+impl<T> SendTarget for T
+where
+    T: Handler + Send,
+{
+    async fn call(self) {
+        W(self).call().await
+    }
+}
+
+fn main() {}
diff --git a/tests/ui/impl-trait/in-trait/cycle-if-impl-doesnt-apply.rs b/tests/ui/impl-trait/in-trait/cycle-if-impl-doesnt-apply.rs
new file mode 100644
index 00000000000..72e08ed3504
--- /dev/null
+++ b/tests/ui/impl-trait/in-trait/cycle-if-impl-doesnt-apply.rs
@@ -0,0 +1,32 @@
+//@ check-pass
+//@ revisions: current next
+//@ ignore-compare-mode-next-solver (explicit revisions)
+//@[next] compile-flags: -Znext-solver
+//@ edition: 2024
+
+// Regression test for <https://github.com/rust-lang/trait-system-refactor-initiative/issues/185>.
+// Avoid unnecessarily computing the RPITIT type of the first impl when checking the WF of the
+// second impl, since the first impl relies on the hidden type of the second impl.
+
+trait Foo {
+    fn call(self) -> impl Send;
+}
+
+trait Nested {}
+impl<T> Foo for T
+where
+    T: Nested,
+{
+    fn call(self) -> impl Sized {
+        NotSatisfied.call()
+    }
+}
+
+struct NotSatisfied;
+impl Foo for NotSatisfied {
+    fn call(self) -> impl Sized {
+        todo!()
+    }
+}
+
+fn main() {}