Skip to content

Commit ea1da92

Browse files
committed
Auto merge of rust-lang#138915 - compiler-errors:binder-tweak, r=lcnr
Instantiate binder before registering nested obligations for auto/built-in traits Instead of turning a `Binder<Vec<Ty>>` into a bunch of higher-ranked predicates, instantiate the binder eagerly *once* and turn them into a bunch of non-higher-ranked predicates. Right now this feels like a noop, but this `enter_forall_and_leak_universe` call would be the singular place where we could instantiate bound lifetime assumptions for coroutine witnesses... if we had them. Thus consolidating the binder instantiation here is useful if we want to fix the coroutine-auto-trait problem. r? lcnr
2 parents 2216f26 + 93b3be3 commit ea1da92

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,18 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
266266
} else {
267267
bug!("unexpected builtin trait {:?}", trait_def)
268268
};
269-
let BuiltinImplConditions::Where(nested) = conditions else {
269+
let BuiltinImplConditions::Where(types) = conditions else {
270270
bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation);
271271
};
272+
let types = self.infcx.enter_forall_and_leak_universe(types);
272273

273274
let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
274275
self.collect_predicates_for_types(
275276
obligation.param_env,
276277
cause,
277278
obligation.recursion_depth + 1,
278279
trait_def,
279-
nested,
280+
types,
280281
)
281282
} else {
282283
PredicateObligations::new()
@@ -444,37 +445,25 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
444445
&mut self,
445446
obligation: &PolyTraitObligation<'tcx>,
446447
) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
447-
debug!(?obligation, "confirm_auto_impl_candidate");
448-
449-
let self_ty = obligation.predicate.self_ty().map_bound(|ty| self.infcx.shallow_resolve(ty));
450-
let types = self.constituent_types_for_ty(self_ty)?;
451-
Ok(self.vtable_auto_impl(obligation, obligation.predicate.def_id(), types))
452-
}
453-
454-
/// See `confirm_auto_impl_candidate`.
455-
fn vtable_auto_impl(
456-
&mut self,
457-
obligation: &PolyTraitObligation<'tcx>,
458-
trait_def_id: DefId,
459-
nested: ty::Binder<'tcx, Vec<Ty<'tcx>>>,
460-
) -> PredicateObligations<'tcx> {
461-
debug!(?nested, "vtable_auto_impl");
462448
ensure_sufficient_stack(|| {
463-
let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
464-
465449
assert_eq!(obligation.predicate.polarity(), ty::PredicatePolarity::Positive);
466450

451+
let self_ty =
452+
obligation.predicate.self_ty().map_bound(|ty| self.infcx.shallow_resolve(ty));
453+
454+
let types = self.constituent_types_for_ty(self_ty)?;
455+
let types = self.infcx.enter_forall_and_leak_universe(types);
456+
457+
let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
467458
let obligations = self.collect_predicates_for_types(
468459
obligation.param_env,
469460
cause,
470461
obligation.recursion_depth + 1,
471-
trait_def_id,
472-
nested,
462+
obligation.predicate.def_id(),
463+
types,
473464
);
474465

475-
debug!(?obligations, "vtable_auto_impl");
476-
477-
obligations
466+
Ok(obligations)
478467
})
479468
}
480469

compiler/rustc_trait_selection/src/traits/select/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -2370,7 +2370,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
23702370
cause: ObligationCause<'tcx>,
23712371
recursion_depth: usize,
23722372
trait_def_id: DefId,
2373-
types: ty::Binder<'tcx, Vec<Ty<'tcx>>>,
2373+
types: Vec<Ty<'tcx>>,
23742374
) -> PredicateObligations<'tcx> {
23752375
// Because the types were potentially derived from
23762376
// higher-ranked obligations they may reference late-bound
@@ -2387,13 +2387,8 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
23872387
// 3. Re-bind the regions back to `for<'a> &'a i32 : Copy`
23882388

23892389
types
2390-
.as_ref()
2391-
.skip_binder() // binder moved -\
2392-
.iter()
2393-
.flat_map(|ty| {
2394-
let ty: ty::Binder<'tcx, Ty<'tcx>> = types.rebind(*ty); // <----/
2395-
2396-
let placeholder_ty = self.infcx.enter_forall_and_leak_universe(ty);
2390+
.into_iter()
2391+
.flat_map(|placeholder_ty| {
23972392
let Normalized { value: normalized_ty, mut obligations } =
23982393
ensure_sufficient_stack(|| {
23992394
normalize_with_depth(

0 commit comments

Comments
 (0)