Skip to content

Commit 71a1ac2

Browse files
authored
Rollup merge of rust-lang#110297 - kylematsuda:earlybinder_tcx_subst, r=BoxyUwU
Make `(try_)subst_and_normalize_erasing_regions` take `EarlyBinder` Changes `subst_and_normalize_erasing_regions` and `try_subst_and_normalize_erasing_regions` to take `EarlyBinder<T>` instead of `T`. (related to rust-lang#105779) This was suggested by `@BoxyUwU` in rust-lang#107753 (comment). After changing `type_of` to return `EarlyBinder`, there were several places where the binder was immediately skipped to call `tcx.subst_and_normalize_erasing_regions`, only for the binder to be reconstructed inside of that method. r? `@BoxyUwU`
2 parents 04c5344 + d27f401 commit 71a1ac2

File tree

14 files changed

+44
-29
lines changed

14 files changed

+44
-29
lines changed

compiler/rustc_codegen_cranelift/src/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
361361
self.instance.subst_mir_and_normalize_erasing_regions(
362362
self.tcx,
363363
ty::ParamEnv::reveal_all(),
364-
value,
364+
ty::EarlyBinder(value),
365365
)
366366
}
367367

compiler/rustc_codegen_llvm/src/debuginfo/create_scope_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn make_mir_scope<'ll, 'tcx>(
9393
let callee = cx.tcx.subst_and_normalize_erasing_regions(
9494
instance.substs,
9595
ty::ParamEnv::reveal_all(),
96-
callee,
96+
ty::EarlyBinder(callee),
9797
);
9898
let callee_fn_abi = cx.fn_abi_of_instance(callee, ty::List::empty());
9999
cx.dbg_scope_fn(callee, callee_fn_abi, None)

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ impl<'ll, 'tcx> DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
529529
let impl_self_ty = cx.tcx.subst_and_normalize_erasing_regions(
530530
instance.substs,
531531
ty::ParamEnv::reveal_all(),
532-
cx.tcx.type_of(impl_def_id).skip_binder(),
532+
cx.tcx.type_of(impl_def_id),
533533
);
534534

535535
// Only "class" methods are generally understood by LLVM,

compiler/rustc_codegen_ssa/src/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
111111
self.instance.subst_mir_and_normalize_erasing_regions(
112112
self.cx.tcx(),
113113
ty::ParamEnv::reveal_all(),
114-
value,
114+
ty::EarlyBinder(value),
115115
)
116116
}
117117
}

compiler/rustc_const_eval/src/interpret/eval_context.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
495495
) -> Result<T, InterpError<'tcx>> {
496496
frame
497497
.instance
498-
.try_subst_mir_and_normalize_erasing_regions(*self.tcx, self.param_env, value)
498+
.try_subst_mir_and_normalize_erasing_regions(
499+
*self.tcx,
500+
self.param_env,
501+
ty::EarlyBinder(value),
502+
)
499503
.map_err(|_| err_inval!(TooGeneric))
500504
}
501505

compiler/rustc_middle/src/ty/instance.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<'tcx> Instance<'tcx> {
115115
/// lifetimes erased, allowing a `ParamEnv` to be specified for use during normalization.
116116
pub fn ty(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Ty<'tcx> {
117117
let ty = tcx.type_of(self.def.def_id());
118-
tcx.subst_and_normalize_erasing_regions(self.substs, param_env, ty.skip_binder())
118+
tcx.subst_and_normalize_erasing_regions(self.substs, param_env, ty)
119119
}
120120

121121
/// Finds a crate that contains a monomorphization of this instance that
@@ -578,14 +578,15 @@ impl<'tcx> Instance<'tcx> {
578578
self.def.has_polymorphic_mir_body().then_some(self.substs)
579579
}
580580

581-
pub fn subst_mir<T>(&self, tcx: TyCtxt<'tcx>, v: &T) -> T
581+
pub fn subst_mir<T>(&self, tcx: TyCtxt<'tcx>, v: EarlyBinder<&T>) -> T
582582
where
583583
T: TypeFoldable<TyCtxt<'tcx>> + Copy,
584584
{
585+
let v = v.map_bound(|v| *v);
585586
if let Some(substs) = self.substs_for_mir_body() {
586-
EarlyBinder(*v).subst(tcx, substs)
587+
v.subst(tcx, substs)
587588
} else {
588-
*v
589+
v.skip_binder()
589590
}
590591
}
591592

@@ -594,15 +595,15 @@ impl<'tcx> Instance<'tcx> {
594595
&self,
595596
tcx: TyCtxt<'tcx>,
596597
param_env: ty::ParamEnv<'tcx>,
597-
v: T,
598+
v: EarlyBinder<T>,
598599
) -> T
599600
where
600601
T: TypeFoldable<TyCtxt<'tcx>> + Clone,
601602
{
602603
if let Some(substs) = self.substs_for_mir_body() {
603604
tcx.subst_and_normalize_erasing_regions(substs, param_env, v)
604605
} else {
605-
tcx.normalize_erasing_regions(param_env, v)
606+
tcx.normalize_erasing_regions(param_env, v.skip_binder())
606607
}
607608
}
608609

@@ -611,15 +612,15 @@ impl<'tcx> Instance<'tcx> {
611612
&self,
612613
tcx: TyCtxt<'tcx>,
613614
param_env: ty::ParamEnv<'tcx>,
614-
v: T,
615+
v: EarlyBinder<T>,
615616
) -> Result<T, NormalizationError<'tcx>>
616617
where
617618
T: TypeFoldable<TyCtxt<'tcx>> + Clone,
618619
{
619620
if let Some(substs) = self.substs_for_mir_body() {
620621
tcx.try_subst_and_normalize_erasing_regions(substs, param_env, v)
621622
} else {
622-
tcx.try_normalize_erasing_regions(param_env, v)
623+
tcx.try_normalize_erasing_regions(param_env, v.skip_binder())
623624
}
624625
}
625626

compiler/rustc_middle/src/ty/normalize_erasing_regions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl<'tcx> TyCtxt<'tcx> {
139139
self,
140140
param_substs: SubstsRef<'tcx>,
141141
param_env: ty::ParamEnv<'tcx>,
142-
value: T,
142+
value: EarlyBinder<T>,
143143
) -> T
144144
where
145145
T: TypeFoldable<TyCtxt<'tcx>>,
@@ -151,7 +151,7 @@ impl<'tcx> TyCtxt<'tcx> {
151151
param_env={:?})",
152152
param_substs, value, param_env,
153153
);
154-
let substituted = EarlyBinder(value).subst(self, param_substs);
154+
let substituted = value.subst(self, param_substs);
155155
self.normalize_erasing_regions(param_env, substituted)
156156
}
157157

@@ -163,7 +163,7 @@ impl<'tcx> TyCtxt<'tcx> {
163163
self,
164164
param_substs: SubstsRef<'tcx>,
165165
param_env: ty::ParamEnv<'tcx>,
166-
value: T,
166+
value: EarlyBinder<T>,
167167
) -> Result<T, NormalizationError<'tcx>>
168168
where
169169
T: TypeFoldable<TyCtxt<'tcx>>,
@@ -175,7 +175,7 @@ impl<'tcx> TyCtxt<'tcx> {
175175
param_env={:?})",
176176
param_substs, value, param_env,
177177
);
178-
let substituted = EarlyBinder(value).subst(self, param_substs);
178+
let substituted = value.subst(self, param_substs);
179179
self.try_normalize_erasing_regions(param_env, substituted)
180180
}
181181
}

compiler/rustc_mir_transform/src/inline.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl<'tcx> Inliner<'tcx> {
180180
let Ok(callee_body) = callsite.callee.try_subst_mir_and_normalize_erasing_regions(
181181
self.tcx,
182182
self.param_env,
183-
callee_body.clone(),
183+
ty::EarlyBinder(callee_body.clone()),
184184
) else {
185185
return Err("failed to normalize callee body");
186186
};
@@ -444,7 +444,9 @@ impl<'tcx> Inliner<'tcx> {
444444
work_list.push(target);
445445

446446
// If the place doesn't actually need dropping, treat it like a regular goto.
447-
let ty = callsite.callee.subst_mir(self.tcx, &place.ty(callee_body, tcx).ty);
447+
let ty = callsite
448+
.callee
449+
.subst_mir(self.tcx, ty::EarlyBinder(&place.ty(callee_body, tcx).ty));
448450
if ty.needs_drop(tcx, self.param_env) && let UnwindAction::Cleanup(unwind) = unwind {
449451
work_list.push(unwind);
450452
}
@@ -788,7 +790,9 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
788790
match terminator.kind {
789791
TerminatorKind::Drop { ref place, unwind, .. } => {
790792
// If the place doesn't actually need dropping, treat it like a regular goto.
791-
let ty = self.instance.subst_mir(tcx, &place.ty(self.callee_body, tcx).ty);
793+
let ty = self
794+
.instance
795+
.subst_mir(tcx, ty::EarlyBinder(&place.ty(self.callee_body, tcx).ty));
792796
if ty.needs_drop(tcx, self.param_env) {
793797
self.cost += CALL_PENALTY;
794798
if let UnwindAction::Cleanup(_) = unwind {
@@ -799,7 +803,7 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
799803
}
800804
}
801805
TerminatorKind::Call { func: Operand::Constant(ref f), unwind, .. } => {
802-
let fn_ty = self.instance.subst_mir(tcx, &f.literal.ty());
806+
let fn_ty = self.instance.subst_mir(tcx, ty::EarlyBinder(&f.literal.ty()));
803807
self.cost += if let ty::FnDef(def_id, _) = *fn_ty.kind() && tcx.is_intrinsic(def_id) {
804808
// Don't give intrinsics the extra penalty for calls
805809
INSTR_COST

compiler/rustc_mir_transform/src/inline/cycle.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ pub(crate) fn mir_callgraph_reachable<'tcx>(
4444
) -> bool {
4545
trace!(%caller);
4646
for &(callee, substs) in tcx.mir_inliner_callees(caller.def) {
47-
let Ok(substs) = caller.try_subst_mir_and_normalize_erasing_regions(tcx, param_env, substs) else {
47+
let Ok(substs) = caller.try_subst_mir_and_normalize_erasing_regions(
48+
tcx,
49+
param_env,
50+
ty::EarlyBinder(substs),
51+
) else {
4852
trace!(?caller, ?param_env, ?substs, "cannot normalize, skipping");
4953
continue;
5054
};

compiler/rustc_monomorphize/src/collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ impl<'a, 'tcx> MirNeighborCollector<'a, 'tcx> {
677677
self.instance.subst_mir_and_normalize_erasing_regions(
678678
self.tcx,
679679
ty::ParamEnv::reveal_all(),
680-
value,
680+
ty::EarlyBinder(value),
681681
)
682682
}
683683
}

compiler/rustc_monomorphize/src/partitioning/default.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ fn characteristic_def_id_of_mono_item<'tcx>(
310310
let impl_self_ty = tcx.subst_and_normalize_erasing_regions(
311311
instance.substs,
312312
ty::ParamEnv::reveal_all(),
313-
tcx.type_of(impl_def_id).skip_binder(),
313+
tcx.type_of(impl_def_id),
314314
);
315315
if let Some(def_id) = characteristic_def_id_of_type(impl_self_ty) {
316316
return Some(def_id);

compiler/rustc_monomorphize/src/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ pub(crate) fn dump_closure_profile<'tcx>(tcx: TyCtxt<'tcx>, closure_instance: In
2929
let before_feature_tys = tcx.subst_and_normalize_erasing_regions(
3030
closure_instance.substs,
3131
param_env,
32-
before_feature_tys,
32+
ty::EarlyBinder(before_feature_tys),
3333
);
3434
let after_feature_tys = tcx.subst_and_normalize_erasing_regions(
3535
closure_instance.substs,
3636
param_env,
37-
after_feature_tys,
37+
ty::EarlyBinder(after_feature_tys),
3838
);
3939

4040
let new_size = tcx

compiler/rustc_ty_utils/src/instance.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ fn resolve_instance<'tcx>(
2727
)
2828
} else {
2929
let ty = tcx.type_of(def);
30-
let item_type =
31-
tcx.subst_and_normalize_erasing_regions(substs, param_env, ty.skip_binder());
30+
let item_type = tcx.subst_and_normalize_erasing_regions(substs, param_env, ty);
3231

3332
let def = match *item_type.kind() {
3433
ty::FnDef(def_id, ..) if tcx.is_intrinsic(def_id) => {

src/tools/clippy/clippy_lints/src/methods/unnecessary_to_owned.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty<
385385
Node::Expr(parent_expr) => {
386386
if let Some((callee_def_id, call_substs, recv, call_args)) = get_callee_substs_and_args(cx, parent_expr)
387387
{
388+
// FIXME: the `subst_identity()` below seems incorrect, since we eventually
389+
// call `tcx.try_subst_and_normalize_erasing_regions` further down
390+
// (i.e., we are explicitly not in the identity context).
388391
let fn_sig = cx.tcx.fn_sig(callee_def_id).subst_identity().skip_binder();
389392
if let Some(arg_index) = recv.into_iter().chain(call_args).position(|arg| arg.hir_id == expr.hir_id)
390393
&& let Some(param_ty) = fn_sig.inputs().get(arg_index)
@@ -435,7 +438,7 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty<
435438
let output_ty = fn_sig.output();
436439
if output_ty.contains(*param_ty) {
437440
if let Ok(new_ty) = cx.tcx.try_subst_and_normalize_erasing_regions(
438-
new_subst, cx.param_env, output_ty) {
441+
new_subst, cx.param_env, EarlyBinder(output_ty)) {
439442
expr = parent_expr;
440443
ty = new_ty;
441444
continue;

0 commit comments

Comments
 (0)