From 876b69c5f966923bc4fa15ee4235a64031d0fb3d Mon Sep 17 00:00:00 2001 From: Amirhossein Akhlaghpour Date: Sat, 8 Aug 2026 15:41:08 +0330 Subject: [PATCH] fix: avoid inference constraints from opaque blanket impls Signed-off-by: Amirhossein Akhlaghpour --- .../src/solve/assembly/mod.rs | 16 +++++++---- ...nce-constraints-from-blanket-2.next.stderr | 16 ----------- ...id-inference-constraints-from-blanket-2.rs | 9 ++---- ...id-inference-constraints-from-blanket-3.rs | 9 ++---- ...nference-constraints-from-blanket-3.stderr | 28 ------------------- 5 files changed, 16 insertions(+), 62 deletions(-) delete mode 100644 tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-2.next.stderr delete mode 100644 tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-3.stderr diff --git a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs index 040f98de7bcfd..2ea2a58125087 100644 --- a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs @@ -1177,13 +1177,19 @@ where if ecx.shallow_resolve(self_ty).is_ty_var() { // We force the certainty of impl candidates to be `Maybe`. let certainty = certainty.and(Certainty::AMBIGUOUS); - ecx.evaluate_added_goals_and_make_canonical_response(certainty) + let response = + ecx.evaluate_added_goals_and_make_canonical_response(certainty)?; + + let Certainty::Maybe(maybe) = response.value.certainty else { + unreachable!(); + }; + + // Blanket impls here only guide inference while the opaque hidden type + // is unknown. Do not let constraints from matching the impl itself + // constrain other inference variables. + Ok(ecx.make_ambiguous_response_no_constraints(maybe)) } else { // We don't want to use impls if they constrain the opaque. - // - // FIXME(trait-system-refactor-initiative#229): This isn't - // perfect yet as it still allows us to incorrectly constrain - // other inference variables. Err(NoSolution.into()) } }) diff --git a/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-2.next.stderr b/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-2.next.stderr deleted file mode 100644 index 86ac1bdad0416..0000000000000 --- a/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-2.next.stderr +++ /dev/null @@ -1,16 +0,0 @@ -error[E0308]: mismatched types - --> $DIR/avoid-inference-constraints-from-blanket-2.rs:27:18 - | -LL | let _: u32 = x; - | --- ^ expected `u32`, found `u64` - | | - | expected due to this - | -help: you can convert a `u64` to a `u32` and panic if the converted value doesn't fit - | -LL | let _: u32 = x.try_into().unwrap(); - | ++++++++++++++++++++ - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-2.rs b/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-2.rs index b4f853de4aad3..88cf375433a2a 100644 --- a/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-2.rs +++ b/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-2.rs @@ -1,15 +1,11 @@ //@ revisions: current next //@[next] compile-flags: -Znext-solver //@ ignore-compare-mode-next-solver (explicit revisions) -//@[current] check-pass +//@ check-pass -// Regression test for trait-system-refactor-initiative#205. Avoid +// Regression test for trait-system-refactor-initiative#205 and #229. Avoid // constraining other impl arguments when applying blanket impls. -// FIXME(-Znext-solver): This currently incompletely constrains the -// argument of `opaque: Trait` using the blanket impl of trait. -// Ideally we don't do that. - trait Trait {} impl Trait for T {} @@ -25,7 +21,6 @@ fn foo() -> impl Sized { return impls_trait::<_, _>(x); } let _: u32 = x; - //[next]~^ ERROR mismatched types 1u64 } fn main() {} diff --git a/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-3.rs b/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-3.rs index 2f29cb4ee6b4f..8b8f2b494c3b5 100644 --- a/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-3.rs +++ b/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-3.rs @@ -1,16 +1,13 @@ //@ compile-flags: -Znext-solver //@ ignore-compare-mode-next-solver (explicit revisions) +//@ check-pass #![allow(unconditional_recursion)] -// Regression test for trait-system-refactor-initiative#205. Avoid +// Regression test for trait-system-refactor-initiative#205 and #229. Avoid // constraining other impl arguments when applying blanket impls, // especially if the nested where-bounds of the blanket impl don't // actually apply for the opaque. -// FIXME(-Znext-solver): This currently incompletely constrains the -// argument of `opaque: Trait` using the blanket impl of trait. -// Ideally we don't do that. - trait Trait {} impl Trait for T {} @@ -19,7 +16,7 @@ fn impls_trait, U>(_: T) {} fn test() -> impl Sized { let x = test(); - impls_trait(x); //~ ERROR the trait bound `String: Trait` is not satisfied + impls_trait(x); String::new() } fn main() {} diff --git a/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-3.stderr b/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-3.stderr deleted file mode 100644 index e3c8c8dfa337d..0000000000000 --- a/tests/ui/impl-trait/non-defining-uses/avoid-inference-constraints-from-blanket-3.stderr +++ /dev/null @@ -1,28 +0,0 @@ -error[E0277]: the trait bound `String: Trait` is not satisfied - --> $DIR/avoid-inference-constraints-from-blanket-3.rs:22:17 - | -LL | impls_trait(x); - | ----------- ^ the trait `Trait` is not implemented for `String` - | | - | required by a bound introduced by this call - | -note: required for `String` to implement `Trait` - --> $DIR/avoid-inference-constraints-from-blanket-3.rs:16:15 - | -LL | impl Trait for T {} - | ---- ^^^^^^^^^^ ^ - | | - | unsatisfied trait bound introduced here -note: required by a bound in `impls_trait` - --> $DIR/avoid-inference-constraints-from-blanket-3.rs:18:19 - | -LL | fn impls_trait, U>(_: T) {} - | ^^^^^^^^ required by this bound in `impls_trait` -help: consider borrowing here - | -LL | impls_trait(&x); - | + - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0277`.