about summary refs log tree commit diff
diff options
context:
space:
mode:
authorStuart Cook <Zalathar@users.noreply.github.com>2025-04-15 15:47:30 +1000
committerGitHub <noreply@github.com>2025-04-15 15:47:30 +1000
commite8c9dcc79eca6315c8b34bd6e8c9b36f040e3574 (patch)
tree4dc673d2b100189b541deb48d955e689cedc9f04
parent8118fca7fd8ccb18cf443122e8b95b32564c5b2f (diff)
parent2c2c9df653079893c34dc9ef72201fdab3a67abc (diff)
downloadrust-e8c9dcc79eca6315c8b34bd6e8c9b36f040e3574.tar.gz
rust-e8c9dcc79eca6315c8b34bd6e8c9b36f040e3574.zip
Rollup merge of #139791 - lcnr:ignore-global-where-bounds, r=compiler-errors
drop global where-bounds before merging candidates

fixes https://github.com/rust-lang/trait-system-refactor-initiative/issues/172

r? ```@compiler-errors```
-rw-r--r--compiler/rustc_next_trait_solver/src/solve/trait_goals.rs8
-rw-r--r--tests/ui/traits/next-solver/cycles/inductive-cycle-but-err.rs8
-rw-r--r--tests/ui/traits/next-solver/cycles/inductive-cycle-but-err.stderr21
-rw-r--r--tests/ui/traits/winnowing/global-where-bound-region-constraints-2.rs33
-rw-r--r--tests/ui/traits/winnowing/global-where-bound-region-constraints.rs29
5 files changed, 93 insertions, 6 deletions
diff --git a/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs b/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs
index 3adfac3d429..9262da2906d 100644
--- a/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs
+++ b/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs
@@ -1256,10 +1256,11 @@ where
     D: SolverDelegate<Interner = I>,
     I: Interner,
 {
+    #[instrument(level = "debug", skip(self, goal), ret)]
     pub(super) fn merge_trait_candidates(
         &mut self,
         goal: Goal<I, TraitPredicate<I>>,
-        candidates: Vec<Candidate<I>>,
+        mut candidates: Vec<Candidate<I>>,
     ) -> Result<(CanonicalResponse<I>, Option<TraitGoalProvenVia>), NoSolution> {
         if let TypingMode::Coherence = self.typing_mode() {
             let all_candidates: Vec<_> = candidates.into_iter().map(|c| c.result).collect();
@@ -1341,13 +1342,16 @@ where
 
         // If there are *only* global where bounds, then make sure to return that this
         // is still reported as being proven-via the param-env so that rigid projections
-        // operate correctly.
+        // operate correctly. Otherwise, drop all global where-bounds before merging the
+        // remaining candidates.
         let proven_via =
             if candidates.iter().all(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
                 TraitGoalProvenVia::ParamEnv
             } else {
+                candidates.retain(|c| !matches!(c.source, CandidateSource::ParamEnv(_)));
                 TraitGoalProvenVia::Misc
             };
+
         let all_candidates: Vec<_> = candidates.into_iter().map(|c| c.result).collect();
         if let Some(response) = self.try_merge_responses(&all_candidates) {
             Ok((response, Some(proven_via)))
diff --git a/tests/ui/traits/next-solver/cycles/inductive-cycle-but-err.rs b/tests/ui/traits/next-solver/cycles/inductive-cycle-but-err.rs
index b0c778e7f57..754fc872e45 100644
--- a/tests/ui/traits/next-solver/cycles/inductive-cycle-but-err.rs
+++ b/tests/ui/traits/next-solver/cycles/inductive-cycle-but-err.rs
@@ -34,9 +34,17 @@ where
     MultipleNested: Trait,
 {}
 
+// We ignore the trivially true global where-bounds when checking that this
+// impl is well-formed, meaning that we depend on `MultipleNested: Trait` when
+// recursively proving `MultipleCandidates: Trait`.
+//
+// These overflow errors will disappear once we treat these cycles as either
+// productive or an error.
 impl Trait for MultipleNested
+//~^ ERROR overflow evaluating the requirement `MultipleNested: Trait`
 where
     MultipleCandidates: Trait,
+    //~^ ERROR overflow evaluating the requirement `MultipleCandidates: Trait`
     DoesNotImpl: Trait,
 {}
 
diff --git a/tests/ui/traits/next-solver/cycles/inductive-cycle-but-err.stderr b/tests/ui/traits/next-solver/cycles/inductive-cycle-but-err.stderr
index acacaf6a331..7895a263634 100644
--- a/tests/ui/traits/next-solver/cycles/inductive-cycle-but-err.stderr
+++ b/tests/ui/traits/next-solver/cycles/inductive-cycle-but-err.stderr
@@ -1,16 +1,29 @@
+error[E0275]: overflow evaluating the requirement `MultipleNested: Trait`
+  --> $DIR/inductive-cycle-but-err.rs:43:16
+   |
+LL | impl Trait for MultipleNested
+   |                ^^^^^^^^^^^^^^
+
+error[E0275]: overflow evaluating the requirement `MultipleCandidates: Trait`
+  --> $DIR/inductive-cycle-but-err.rs:46:25
+   |
+LL |     MultipleCandidates: Trait,
+   |                         ^^^^^
+
 error[E0277]: the trait bound `MultipleCandidates: Trait` is not satisfied
-  --> $DIR/inductive-cycle-but-err.rs:46:19
+  --> $DIR/inductive-cycle-but-err.rs:54:19
    |
 LL |     impls_trait::<MultipleCandidates>();
    |                   ^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `MultipleCandidates`
    |
    = help: the trait `Trait` is implemented for `MultipleCandidates`
 note: required by a bound in `impls_trait`
-  --> $DIR/inductive-cycle-but-err.rs:43:19
+  --> $DIR/inductive-cycle-but-err.rs:51:19
    |
 LL | fn impls_trait<T: Trait>() {}
    |                   ^^^^^ required by this bound in `impls_trait`
 
-error: aborting due to 1 previous error
+error: aborting due to 3 previous errors
 
-For more information about this error, try `rustc --explain E0277`.
+Some errors have detailed explanations: E0275, E0277.
+For more information about an error, try `rustc --explain E0275`.
diff --git a/tests/ui/traits/winnowing/global-where-bound-region-constraints-2.rs b/tests/ui/traits/winnowing/global-where-bound-region-constraints-2.rs
new file mode 100644
index 00000000000..d422605a292
--- /dev/null
+++ b/tests/ui/traits/winnowing/global-where-bound-region-constraints-2.rs
@@ -0,0 +1,33 @@
+//@ revisions: current next
+//@ ignore-compare-mode-next-solver (explicit revisions)
+//@[next] compile-flags: -Znext-solver
+//@ check-pass
+
+// Regression test for trait-system-refactor-initiative#172.
+//
+// In this test the global where-bound simply constrains the
+// object lifetime bound to 'static while the builtin impl
+// ends up also emitting a `dyn Any: 'static` type outlives
+// constraint. This previously resulted in ambiguity. We now
+// always prefer the impl.
+
+pub trait Any: 'static {}
+
+pub trait Downcast<T>: Any
+where
+    T: Any,
+{
+}
+
+// elided object lifetime: `dyn Any + 'static`
+impl dyn Any {
+    pub fn is<T>(&self)
+    where
+        T: Any,
+        // elaboration adds global where-clause `dyn Any + 'static: Any`
+        Self: Downcast<T>,
+    {
+    }
+}
+
+fn main() {}
diff --git a/tests/ui/traits/winnowing/global-where-bound-region-constraints.rs b/tests/ui/traits/winnowing/global-where-bound-region-constraints.rs
new file mode 100644
index 00000000000..3bc8b0438bf
--- /dev/null
+++ b/tests/ui/traits/winnowing/global-where-bound-region-constraints.rs
@@ -0,0 +1,29 @@
+//@ revisions: current next
+//@ ignore-compare-mode-next-solver (explicit revisions)
+//@[next] compile-flags: -Znext-solver
+//@ check-pass
+
+// Regression test for trait-system-refactor-initiative#172.
+//
+// The next-generation trait solver previously simply tried
+// to merge the global where-bounds with the impl candidates.
+// This caused ambiguity in case the where-bound had stricter
+// region requirements than the impl.
+
+trait Trait {}
+struct Foo<'a, 'b>(&'a (), &'b ());
+impl<'a> Trait for Foo<'a, 'static> {}
+
+fn impls_trait<T: Trait>() {}
+fn foo()
+where
+    Foo<'static, 'static>: Trait,
+{
+    // impl requires `'1 to be 'static
+    // global where-bound requires both '0 and '1 to be 'static
+    //
+    // we always prefer the impl here.
+    impls_trait::<Foo<'_, '_>>();
+}
+
+fn main() {}