From 93b3be33001d15e85b38d99560eaa337812ee90a Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 24 Mar 2025 17:09:48 +0000 Subject: [PATCH] Instantiate binder before registering nested obligations for auto/built-in traits --- .../src/traits/select/confirmation.rs | 37 +++++++------------ .../src/traits/select/mod.rs | 11 ++---- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 4404324d5cd7..a66c958c1097 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -266,9 +266,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } else { bug!("unexpected builtin trait {:?}", trait_def) }; - let BuiltinImplConditions::Where(nested) = conditions else { + let BuiltinImplConditions::Where(types) = conditions else { bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation); }; + let types = self.infcx.enter_forall_and_leak_universe(types); let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived); self.collect_predicates_for_types( @@ -276,7 +277,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { cause, obligation.recursion_depth + 1, trait_def, - nested, + types, ) } else { PredicateObligations::new() @@ -444,37 +445,25 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { &mut self, obligation: &PolyTraitObligation<'tcx>, ) -> Result, SelectionError<'tcx>> { - debug!(?obligation, "confirm_auto_impl_candidate"); - - let self_ty = obligation.predicate.self_ty().map_bound(|ty| self.infcx.shallow_resolve(ty)); - let types = self.constituent_types_for_ty(self_ty)?; - Ok(self.vtable_auto_impl(obligation, obligation.predicate.def_id(), types)) - } - - /// See `confirm_auto_impl_candidate`. - fn vtable_auto_impl( - &mut self, - obligation: &PolyTraitObligation<'tcx>, - trait_def_id: DefId, - nested: ty::Binder<'tcx, Vec>>, - ) -> PredicateObligations<'tcx> { - debug!(?nested, "vtable_auto_impl"); ensure_sufficient_stack(|| { - let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived); - assert_eq!(obligation.predicate.polarity(), ty::PredicatePolarity::Positive); + let self_ty = + obligation.predicate.self_ty().map_bound(|ty| self.infcx.shallow_resolve(ty)); + + let types = self.constituent_types_for_ty(self_ty)?; + let types = self.infcx.enter_forall_and_leak_universe(types); + + let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived); let obligations = self.collect_predicates_for_types( obligation.param_env, cause, obligation.recursion_depth + 1, - trait_def_id, - nested, + obligation.predicate.def_id(), + types, ); - debug!(?obligations, "vtable_auto_impl"); - - obligations + Ok(obligations) }) } diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index e1adabbeaa66..e439df76cd4b 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -2370,7 +2370,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> { cause: ObligationCause<'tcx>, recursion_depth: usize, trait_def_id: DefId, - types: ty::Binder<'tcx, Vec>>, + types: Vec>, ) -> PredicateObligations<'tcx> { // Because the types were potentially derived from // higher-ranked obligations they may reference late-bound @@ -2387,13 +2387,8 @@ impl<'tcx> SelectionContext<'_, 'tcx> { // 3. Re-bind the regions back to `for<'a> &'a i32 : Copy` types - .as_ref() - .skip_binder() // binder moved -\ - .iter() - .flat_map(|ty| { - let ty: ty::Binder<'tcx, Ty<'tcx>> = types.rebind(*ty); // <----/ - - let placeholder_ty = self.infcx.enter_forall_and_leak_universe(ty); + .into_iter() + .flat_map(|placeholder_ty| { let Normalized { value: normalized_ty, mut obligations } = ensure_sufficient_stack(|| { normalize_with_depth(