Skip to content

Commit baa0ebc

Browse files
committed
rename instantiate_binder_with_placeholders
1 parent 9659e6c commit baa0ebc

File tree

10 files changed

+38
-23
lines changed

10 files changed

+38
-23
lines changed

compiler/rustc_infer/src/infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ impl<'tcx> InferCtxt<'tcx> {
10311031
_ => {}
10321032
}
10331033

1034-
// FIXME(tree_universes): leaking universes
1034+
// FIXME(tree_universes): leaking placeholders
10351035
self.enter_forall(predicate, |ty::SubtypePredicate { a_is_expected, a, b }| {
10361036
Ok(self.at(cause, param_env).sub_exp(DefineOpaqueTypes::No, a_is_expected, a, b))
10371037
})
@@ -1042,7 +1042,7 @@ impl<'tcx> InferCtxt<'tcx> {
10421042
cause: &traits::ObligationCause<'tcx>,
10431043
predicate: ty::PolyRegionOutlivesPredicate<'tcx>,
10441044
) {
1045-
// FIXME(tree_universes): leaking universes
1045+
// FIXME(tree_universes): leaking placeholders
10461046
self.enter_forall(predicate, |ty::OutlivesPredicate(r_a, r_b)| {
10471047
let origin = SubregionOrigin::from_obligation_cause(cause, || {
10481048
RelateRegionParamBound(cause.span)

compiler/rustc_infer/src/infer/relate/higher_ranked.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ impl<'a, 'tcx> CombineFields<'a, 'tcx> {
4949
debug!("b_prime={:?}", sup_prime);
5050

5151
// Compare types now that bound regions have been replaced.
52-
// FIXME(tree_universes): leaked dead universes
52+
// FIXME(tree_universes): leaked universes
5353
let result = self.sub(sub_is_expected).relate(sub_prime, sup_prime);
5454
if result.is_ok() {
5555
debug!("OK result={result:?}");
5656
}
57+
// NOTE: returning the result here would be dangerous as it contains
58+
// placeholders which **must not** be named afterwards.
5759
result.map(|_| ())
5860
})
5961
}
@@ -68,9 +70,11 @@ impl<'tcx> InferCtxt<'tcx> {
6870
/// This is the first step of checking subtyping when higher-ranked things are involved.
6971
/// For more details visit the relevant sections of the [rustc dev guide].
7072
///
73+
/// `enter_forall` should be preferred over this method.
74+
///
7175
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
7276
#[instrument(level = "debug", skip(self), ret)]
73-
pub fn instantiate_binder_with_placeholders<T>(&self, binder: ty::Binder<'tcx, T>) -> T
77+
pub fn enter_forall_and_leak_universe<T>(&self, binder: ty::Binder<'tcx, T>) -> T
7478
where
7579
T: TypeFoldable<TyCtxt<'tcx>> + Copy,
7680
{
@@ -106,11 +110,24 @@ impl<'tcx> InferCtxt<'tcx> {
106110
self.tcx.replace_bound_vars_uncached(binder, delegate)
107111
}
108112

113+
/// Replaces all bound variables (lifetimes, types, and constants) bound by
114+
/// `binder` with placeholder variables in a new universe. This means that the
115+
/// new placeholders can only be named by inference variables created after
116+
/// this method has been called.
117+
///
118+
/// This is the first step of checking subtyping when higher-ranked things are involved.
119+
/// For more details visit the relevant sections of the [rustc dev guide].
120+
///
121+
/// This method should be preferred over `enter_forall_and_leak_universe`.
122+
///
123+
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
124+
#[instrument(level = "debug", skip(self, f))]
109125
pub fn enter_forall<T, U>(&self, forall: ty::Binder<'tcx, T>, f: impl FnOnce(T) -> U) -> U
110126
where
111127
T: TypeFoldable<TyCtxt<'tcx>> + Copy,
112128
{
113-
let value = self.instantiate_binder_with_placeholders(forall);
129+
let value = self.enter_forall_and_leak_universe(forall);
130+
debug!("?value");
114131
f(value)
115132
}
116133

compiler/rustc_infer/src/infer/relate/nll.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ where
262262
}
263263

264264
#[instrument(skip(self), level = "debug")]
265-
fn instantiate_binder_with_placeholders<T>(&mut self, binder: ty::Binder<'tcx, T>) -> T
265+
fn enter_forall_and_leak_universe<T>(&mut self, binder: ty::Binder<'tcx, T>) -> T
266266
where
267267
T: ty::TypeFoldable<TyCtxt<'tcx>> + Copy,
268268
{
@@ -317,7 +317,7 @@ where
317317
where
318318
T: ty::TypeFoldable<TyCtxt<'tcx>> + Copy,
319319
{
320-
let value = self.instantiate_binder_with_placeholders(binder);
320+
let value = self.enter_forall_and_leak_universe(binder);
321321
f(self, value)
322322
}
323323

compiler/rustc_trait_selection/src/solve/fulfill.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> {
140140
)
141141
}
142142
ty::PredicateKind::Subtype(pred) => {
143-
let (a, b) = infcx.instantiate_binder_with_placeholders(
143+
let (a, b) = infcx.enter_forall_and_leak_universe(
144144
goal.predicate.kind().rebind((pred.a, pred.b)),
145145
);
146146
let expected_found = ExpectedFound::new(true, a, b);
@@ -150,7 +150,7 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> {
150150
)
151151
}
152152
ty::PredicateKind::Coerce(pred) => {
153-
let (a, b) = infcx.instantiate_binder_with_placeholders(
153+
let (a, b) = infcx.enter_forall_and_leak_universe(
154154
goal.predicate.kind().rebind((pred.a, pred.b)),
155155
);
156156
let expected_found = ExpectedFound::new(false, a, b);

compiler/rustc_trait_selection/src/traits/fulfill.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
358358
| ty::PredicateKind::Coerce(_)
359359
| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
360360
| ty::PredicateKind::ConstEquate(..) => {
361-
let pred =
362-
ty::Binder::dummy(infcx.instantiate_binder_with_placeholders(binder));
361+
let pred = ty::Binder::dummy(infcx.enter_forall_and_leak_universe(binder));
363362
ProcessResult::Changed(mk_pending(vec![obligation.with(infcx.tcx, pred)]))
364363
}
365364
ty::PredicateKind::Ambiguous => ProcessResult::Unchanged,

compiler/rustc_trait_selection/src/traits/project.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ pub(super) fn poly_project_and_unify_type<'cx, 'tcx>(
250250
let infcx = selcx.infcx;
251251
let r = infcx.commit_if_ok(|_snapshot| {
252252
let old_universe = infcx.universe();
253-
let placeholder_predicate =
254-
infcx.instantiate_binder_with_placeholders(obligation.predicate);
253+
let placeholder_predicate = infcx.enter_forall_and_leak_universe(obligation.predicate);
255254
let new_universe = infcx.universe();
256255

257256
let placeholder_obligation = obligation.with(infcx.tcx, placeholder_predicate);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
666666
self.infcx.probe(|_snapshot| {
667667
let poly_trait_predicate = self.infcx.resolve_vars_if_possible(obligation.predicate);
668668
let placeholder_trait_predicate =
669-
self.infcx.instantiate_binder_with_placeholders(poly_trait_predicate);
669+
self.infcx.enter_forall_and_leak_universe(poly_trait_predicate);
670670

671671
let self_ty = placeholder_trait_predicate.self_ty();
672672
let principal_trait_ref = match self_ty.kind() {

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
150150

151151
let trait_predicate = self.infcx.shallow_resolve(obligation.predicate);
152152
let placeholder_trait_predicate =
153-
self.infcx.instantiate_binder_with_placeholders(trait_predicate).trait_ref;
153+
self.infcx.enter_forall_and_leak_universe(trait_predicate).trait_ref;
154154
let placeholder_self_ty = placeholder_trait_predicate.self_ty();
155155
let placeholder_trait_predicate = ty::Binder::dummy(placeholder_trait_predicate);
156156
let (def_id, args) = match *placeholder_self_ty.kind() {
@@ -393,7 +393,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
393393
let cause = obligation.derived_cause(BuiltinDerivedObligation);
394394

395395
let poly_trait_ref = obligation.predicate.to_poly_trait_ref();
396-
let trait_ref = self.infcx.instantiate_binder_with_placeholders(poly_trait_ref);
396+
let trait_ref = self.infcx.enter_forall_and_leak_universe(poly_trait_ref);
397397
let trait_obligations: Vec<PredicateObligation<'_>> = self.impl_or_trait_obligations(
398398
&cause,
399399
obligation.recursion_depth + 1,
@@ -484,7 +484,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
484484
let tcx = self.tcx();
485485
debug!(?obligation, ?index, "confirm_object_candidate");
486486

487-
let trait_predicate = self.infcx.instantiate_binder_with_placeholders(obligation.predicate);
487+
let trait_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
488488
let self_ty = self.infcx.shallow_resolve(trait_predicate.self_ty());
489489
let obligation_trait_ref = ty::Binder::dummy(trait_predicate.trait_ref);
490490
let ty::Dynamic(data, ..) = *self_ty.kind() else {
@@ -682,7 +682,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
682682
let cause = obligation.derived_cause(BuiltinDerivedObligation);
683683

684684
// Confirm the `type Output: Sized;` bound that is present on `FnOnce`
685-
let output_ty = self.infcx.instantiate_binder_with_placeholders(sig.output());
685+
let output_ty = self.infcx.enter_forall_and_leak_universe(sig.output());
686686
let output_ty = normalize_with_depth_to(
687687
self,
688688
obligation.param_env,
@@ -703,7 +703,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
703703
) -> Vec<PredicateObligation<'tcx>> {
704704
debug!(?obligation, "confirm_trait_alias_candidate");
705705

706-
let predicate = self.infcx.instantiate_binder_with_placeholders(obligation.predicate);
706+
let predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
707707
let trait_ref = predicate.trait_ref;
708708
let trait_def_id = trait_ref.def_id;
709709
let args = trait_ref.args;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16061606
) -> smallvec::SmallVec<[usize; 2]> {
16071607
let poly_trait_predicate = self.infcx.resolve_vars_if_possible(obligation.predicate);
16081608
let placeholder_trait_predicate =
1609-
self.infcx.instantiate_binder_with_placeholders(poly_trait_predicate);
1609+
self.infcx.enter_forall_and_leak_universe(poly_trait_predicate);
16101610
debug!(?placeholder_trait_predicate);
16111611

16121612
let tcx = self.infcx.tcx;
@@ -2365,7 +2365,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
23652365
.flat_map(|ty| {
23662366
let ty: ty::Binder<'tcx, Ty<'tcx>> = types.rebind(*ty); // <----/
23672367

2368-
let placeholder_ty = self.infcx.instantiate_binder_with_placeholders(ty);
2368+
let placeholder_ty = self.infcx.enter_forall_and_leak_universe(ty);
23692369
let Normalized { value: normalized_ty, mut obligations } =
23702370
ensure_sufficient_stack(|| {
23712371
project::normalize_with_depth(
@@ -2451,7 +2451,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
24512451
obligation: &PolyTraitObligation<'tcx>,
24522452
) -> Result<Normalized<'tcx, GenericArgsRef<'tcx>>, ()> {
24532453
let placeholder_obligation =
2454-
self.infcx.instantiate_binder_with_placeholders(obligation.predicate);
2454+
self.infcx.enter_forall_and_leak_universe(obligation.predicate);
24552455
let placeholder_obligation_trait_ref = placeholder_obligation.trait_ref;
24562456

24572457
let impl_args = self.infcx.fresh_args_for_item(obligation.cause.span, impl_def_id);

compiler/rustc_type_ir/src/region_kind.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub enum RegionKind<I: Interner> {
164164
/// Should not exist outside of type inference.
165165
///
166166
/// Used when instantiating a `forall` binder via
167-
/// `infcx.enter_forall` and `infcx.instantiate_binder_with_placeholders`.
167+
/// `infcx.enter_forall` and `infcx.enter_forall_and_leak_universe`.
168168
RePlaceholder(I::PlaceholderRegion),
169169

170170
/// Erased region, used by trait selection, in MIR and during codegen.

0 commit comments

Comments
 (0)