diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index d2fc5ae05434a..9c07f97a73400 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -1190,9 +1190,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { (ty::FnDef(..), ty::FnDef(..)) => { // Don't reify if the function types have a LUB, i.e., they // are the same function and their parameters have a LUB. - match self - .commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty)) - { + match self.commit_if_ok(|_| { + // We need to eagerly handle nested obligations due to lazy norm. + if self.next_trait_solver() { + let ocx = ObligationCtxt::new(self); + let value = ocx.lub(cause, self.param_env, prev_ty, new_ty)?; + if ocx.select_where_possible().is_empty() { + Ok(InferOk { + value, + obligations: ocx.into_pending_obligations(), + }) + } else { + Err(TypeError::Mismatch) + } + } else { + self.at(cause, self.param_env).lub(prev_ty, new_ty) + } + }) { // We have a LUB of prev_ty and new_ty, just return it. Ok(ok) => return Ok(self.register_infer_ok_obligations(ok)), Err(_) => { diff --git a/compiler/rustc_trait_selection/src/traits/engine.rs b/compiler/rustc_trait_selection/src/traits/engine.rs index 8d6e6b4a65165..18f28d72f6f8f 100644 --- a/compiler/rustc_trait_selection/src/traits/engine.rs +++ b/compiler/rustc_trait_selection/src/traits/engine.rs @@ -188,6 +188,20 @@ where .map(|infer_ok| self.register_infer_ok_obligations(infer_ok)) } + /// Computes the least-upper-bound, or mutual supertype, of two values. + pub fn lub>( + &self, + cause: &ObligationCause<'tcx>, + param_env: ty::ParamEnv<'tcx>, + expected: T, + actual: T, + ) -> Result> { + self.infcx + .at(cause, param_env) + .lub(expected, actual) + .map(|infer_ok| self.register_infer_ok_obligations(infer_ok)) + } + #[must_use] pub fn select_where_possible(&self) -> Vec { self.engine.borrow_mut().select_where_possible(self.infcx) diff --git a/tests/ui/traits/next-solver/coerce-ambig-alias-to-rigid-alias.rs b/tests/ui/traits/next-solver/coercion/coerce-ambig-alias-to-rigid-alias.rs similarity index 100% rename from tests/ui/traits/next-solver/coerce-ambig-alias-to-rigid-alias.rs rename to tests/ui/traits/next-solver/coercion/coerce-ambig-alias-to-rigid-alias.rs diff --git a/tests/ui/traits/next-solver/coerce-depth.rs b/tests/ui/traits/next-solver/coercion/coerce-depth.rs similarity index 100% rename from tests/ui/traits/next-solver/coerce-depth.rs rename to tests/ui/traits/next-solver/coercion/coerce-depth.rs diff --git a/tests/ui/traits/next-solver/coercion/fn-def-coerce-nested-obligations.rs b/tests/ui/traits/next-solver/coercion/fn-def-coerce-nested-obligations.rs new file mode 100644 index 0000000000000..1b5abcb02f2f8 --- /dev/null +++ b/tests/ui/traits/next-solver/coercion/fn-def-coerce-nested-obligations.rs @@ -0,0 +1,16 @@ +//@ compile-flags: -Znext-solver +//@ check-pass + +// Make sure that we consider nested obligations when checking whether +// we should coerce fn definitions to function pointers. + +fn foo() {} +fn bar() {} +fn main() { + let _ = if true { foo::<{ 0 + 0 }> } else { foo::<1> }; + let _ = if true { + bar:: fn( as IntoIterator>::Item)> + } else { + bar:: + }; +} diff --git a/tests/ui/traits/next-solver/non-wf-in-coerce-pointers.rs b/tests/ui/traits/next-solver/coercion/non-wf-in-coerce-pointers.rs similarity index 100% rename from tests/ui/traits/next-solver/non-wf-in-coerce-pointers.rs rename to tests/ui/traits/next-solver/coercion/non-wf-in-coerce-pointers.rs diff --git a/tests/ui/traits/next-solver/non-wf-in-coerce-pointers.stderr b/tests/ui/traits/next-solver/coercion/non-wf-in-coerce-pointers.stderr similarity index 100% rename from tests/ui/traits/next-solver/non-wf-in-coerce-pointers.stderr rename to tests/ui/traits/next-solver/coercion/non-wf-in-coerce-pointers.stderr diff --git a/tests/ui/traits/next-solver/trait-upcast-lhs-needs-normalization.rs b/tests/ui/traits/next-solver/coercion/trait-upcast-lhs-needs-normalization.rs similarity index 100% rename from tests/ui/traits/next-solver/trait-upcast-lhs-needs-normalization.rs rename to tests/ui/traits/next-solver/coercion/trait-upcast-lhs-needs-normalization.rs diff --git a/tests/ui/traits/next-solver/upcast-right-substs.rs b/tests/ui/traits/next-solver/coercion/upcast-right-substs.rs similarity index 100% rename from tests/ui/traits/next-solver/upcast-right-substs.rs rename to tests/ui/traits/next-solver/coercion/upcast-right-substs.rs diff --git a/tests/ui/traits/next-solver/upcast-wrong-substs.rs b/tests/ui/traits/next-solver/coercion/upcast-wrong-substs.rs similarity index 100% rename from tests/ui/traits/next-solver/upcast-wrong-substs.rs rename to tests/ui/traits/next-solver/coercion/upcast-wrong-substs.rs diff --git a/tests/ui/traits/next-solver/upcast-wrong-substs.stderr b/tests/ui/traits/next-solver/coercion/upcast-wrong-substs.stderr similarity index 100% rename from tests/ui/traits/next-solver/upcast-wrong-substs.stderr rename to tests/ui/traits/next-solver/coercion/upcast-wrong-substs.stderr