From cc584ba24527f16b3c88f46959fedcfc716d5503 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 27 Feb 2024 20:07:58 +0000 Subject: [PATCH] Process alias-relate obligations in CoerceUnsized loop --- compiler/rustc_hir_typeck/src/coercion.rs | 14 ++++++++++++++ .../constrain-alias-goals-in-unsize.rs | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 tests/ui/traits/next-solver/constrain-alias-goals-in-unsize.rs diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 8f4f028fc07f1..d07c9b10f1cbd 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -636,6 +636,20 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { { self.resolve_vars_if_possible(trait_pred) } + // Eagerly process alias-relate obligations in new trait solver, + // since these can be emitted in the process of solving trait goals, + // but we need to constrain vars before processing goals mentioning + // them. + Some(ty::PredicateKind::AliasRelate(..)) => { + let mut fulfill_cx = >::new(self); + fulfill_cx.register_predicate_obligation(self, obligation); + let errs = fulfill_cx.select_where_possible(self); + if !errs.is_empty() { + return Err(TypeError::Mismatch); + } + coercion.obligations.extend(fulfill_cx.pending_obligations()); + continue; + } _ => { coercion.obligations.push(obligation); continue; diff --git a/tests/ui/traits/next-solver/constrain-alias-goals-in-unsize.rs b/tests/ui/traits/next-solver/constrain-alias-goals-in-unsize.rs new file mode 100644 index 0000000000000..1656238bd6188 --- /dev/null +++ b/tests/ui/traits/next-solver/constrain-alias-goals-in-unsize.rs @@ -0,0 +1,18 @@ +//@ compile-flags: -Znext-solver +//@ check-pass + +use std::mem::ManuallyDrop; + +trait Foo {} + +struct Guard { + value: ManuallyDrop, +} + +impl Guard { + fn uwu(&self) { + let x: &dyn Foo = &*self.value; + } +} + +fn main() {}