Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1b8df38

Browse files
committed
Fix -Zpolymorphize
1 parent 95511cb commit 1b8df38

File tree

6 files changed

+52
-46
lines changed

6 files changed

+52
-46
lines changed

src/abi/mod.rs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,26 @@ use crate::prelude::*;
1313

1414
pub(crate) use self::returning::{can_return_to_ssa_var, codegen_return};
1515

16-
// Copied from https://github.com/rust-lang/rust/blob/b2c1a606feb1fbdb0ac0acba76f881ef172ed474/src/librustc_middle/ty/layout.rs#L2287
16+
// Copied from https://github.com/rust-lang/rust/blob/f52c72948aa1dd718cc1f168d21c91c584c0a662/src/librustc_middle/ty/layout.rs#L2301
1717
pub(crate) fn fn_sig_for_fn_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> ty::PolyFnSig<'tcx> {
18-
let ty = instance.ty(tcx, ParamEnv::reveal_all());
18+
use rustc_middle::ty::subst::Subst;
19+
20+
// FIXME(davidtwco,eddyb): A `ParamEnv` should be passed through to this function.
21+
let ty = instance.ty(tcx, ty::ParamEnv::reveal_all());
1922
match ty.kind {
20-
ty::FnDef(..) |
21-
// Shims currently have type FnPtr. Not sure this should remain.
22-
ty::FnPtr(_) => {
23-
let mut sig = ty.fn_sig(tcx);
23+
ty::FnDef(..) => {
24+
// HACK(davidtwco,eddyb): This is a workaround for polymorphization considering
25+
// parameters unused if they show up in the signature, but not in the `mir::Body`
26+
// (i.e. due to being inside a projection that got normalized, see
27+
// `src/test/ui/polymorphization/normalized_sig_types.rs`), and codegen not keeping
28+
// track of a polymorphization `ParamEnv` to allow normalizing later.
29+
let mut sig = match ty.kind {
30+
ty::FnDef(def_id, substs) => tcx
31+
.normalize_erasing_regions(tcx.param_env(def_id), tcx.fn_sig(def_id))
32+
.subst(tcx, substs),
33+
_ => unreachable!(),
34+
};
35+
2436
if let ty::InstanceDef::VtableShim(..) = instance.def {
2537
// Modify `fn(self, ...)` to `fn(self: *mut Self, ...)`.
2638
sig = sig.map_bound(|mut sig| {
@@ -36,44 +48,44 @@ pub(crate) fn fn_sig_for_fn_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx
3648
let sig = substs.as_closure().sig();
3749

3850
let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
39-
sig.map_bound(|sig| tcx.mk_fn_sig(
40-
std::iter::once(env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
41-
sig.output(),
42-
sig.c_variadic,
43-
sig.unsafety,
44-
sig.abi
45-
))
51+
sig.map_bound(|sig| {
52+
tcx.mk_fn_sig(
53+
std::iter::once(env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
54+
sig.output(),
55+
sig.c_variadic,
56+
sig.unsafety,
57+
sig.abi,
58+
)
59+
})
4660
}
4761
ty::Generator(_, substs, _) => {
4862
let sig = substs.as_generator().poly_sig();
4963

5064
let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
5165
let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
5266

53-
let pin_did = tcx.lang_items().pin_type().unwrap();
67+
let pin_did = tcx.require_lang_item(rustc_hir::LangItem::PinTypeLangItem, None);
5468
let pin_adt_ref = tcx.adt_def(pin_did);
5569
let pin_substs = tcx.intern_substs(&[env_ty.into()]);
5670
let env_ty = tcx.mk_adt(pin_adt_ref, pin_substs);
5771

5872
sig.map_bound(|sig| {
59-
let state_did = tcx.lang_items().gen_state().unwrap();
73+
let state_did = tcx.require_lang_item(rustc_hir::LangItem::GeneratorStateLangItem, None);
6074
let state_adt_ref = tcx.adt_def(state_did);
61-
let state_substs = tcx.intern_substs(&[
62-
sig.yield_ty.into(),
63-
sig.return_ty.into(),
64-
]);
75+
let state_substs =
76+
tcx.intern_substs(&[sig.yield_ty.into(), sig.return_ty.into()]);
6577
let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
6678

6779
tcx.mk_fn_sig(
6880
[env_ty, sig.resume_ty].iter(),
6981
&ret_ty,
7082
false,
7183
rustc_hir::Unsafety::Normal,
72-
rustc_target::spec::abi::Abi::Rust
84+
rustc_target::spec::abi::Abi::Rust,
7385
)
7486
})
7587
}
76-
_ => bug!("unexpected type {:?} in Instance::fn_sig", ty)
88+
_ => bug!("unexpected type {:?} in Instance::fn_sig", ty),
7789
}
7890
}
7991

@@ -464,7 +476,8 @@ pub(crate) fn codegen_terminator_call<'tcx>(
464476
let instance = if let ty::FnDef(def_id, substs) = fn_ty.kind {
465477
let instance = ty::Instance::resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs)
466478
.unwrap()
467-
.unwrap();
479+
.unwrap()
480+
.polymorphize(fx.tcx);
468481

469482
if fx.tcx.symbol_name(instance).name.starts_with("llvm.") {
470483
crate::intrinsics::codegen_llvm_intrinsic_call(
@@ -655,7 +668,7 @@ pub(crate) fn codegen_drop<'tcx>(
655668
drop_place: CPlace<'tcx>,
656669
) {
657670
let ty = drop_place.layout().ty;
658-
let drop_fn = Instance::resolve_drop_in_place(fx.tcx, ty);
671+
let drop_fn = Instance::resolve_drop_in_place(fx.tcx, ty).polymorphize(fx.tcx);
659672

660673
if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
661674
// we don't actually need to drop anything
@@ -685,16 +698,7 @@ pub(crate) fn codegen_drop<'tcx>(
685698
fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]);
686699
}
687700
_ => {
688-
let instance = match drop_fn_ty.kind {
689-
ty::FnDef(def_id, substs) => {
690-
Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs)
691-
.unwrap()
692-
.unwrap()
693-
}
694-
_ => unreachable!("{:?}", drop_fn_ty),
695-
};
696-
697-
assert!(!matches!(instance.def, InstanceDef::Virtual(_, _)));
701+
assert!(!matches!(drop_fn.def, InstanceDef::Virtual(_, _)));
698702

699703
let arg_place = CPlace::new_stack_slot(
700704
fx,
@@ -712,13 +716,13 @@ pub(crate) fn codegen_drop<'tcx>(
712716

713717
let mut call_args: Vec<Value> = arg_value.into_iter().collect::<Vec<_>>();
714718

715-
if instance.def.requires_caller_location(fx.tcx) {
719+
if drop_fn.def.requires_caller_location(fx.tcx) {
716720
// Pass the caller location for `#[track_caller]`.
717721
let caller_location = fx.get_caller_location(span);
718722
call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter());
719723
}
720724

721-
let func_ref = fx.get_function_ref(instance);
725+
let func_ref = fx.get_function_ref(drop_fn);
722726
fx.bcx.ins().call(func_ref, &call_args);
723727
}
724728
}

src/base.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Backend>) {
267267
fx.tcx.sess.span_fatal(bb_data.terminator().source_info.span, &s)
268268
});
269269

270-
let instance = Instance::mono(fx.tcx, def_id);
270+
let instance = Instance::mono(fx.tcx, def_id).polymorphize(fx.tcx);
271271
let symbol_name = fx.tcx.symbol_name(instance).name;
272272

273273
fx.lib_call(&*symbol_name, vec![fx.pointer_type, fx.pointer_type, fx.pointer_type], vec![], &args);
@@ -469,7 +469,8 @@ fn trans_stmt<'tcx>(
469469
ty::FnDef(def_id, substs) => {
470470
let func_ref = fx.get_function_ref(
471471
Instance::resolve_for_fn_ptr(fx.tcx, ParamEnv::reveal_all(), def_id, substs)
472-
.unwrap(),
472+
.unwrap()
473+
.polymorphize(fx.tcx),
473474
);
474475
let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
475476
lval.write_cvalue(fx, CValue::by_val(func_addr, to_layout));
@@ -580,7 +581,7 @@ fn trans_stmt<'tcx>(
580581
def_id,
581582
substs,
582583
ty::ClosureKind::FnOnce,
583-
);
584+
).polymorphize(fx.tcx);
584585
let func_ref = fx.get_function_ref(instance);
585586
let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
586587
lval.write_cvalue(fx, CValue::by_val(func_addr, lval.layout()));
@@ -641,7 +642,7 @@ fn trans_stmt<'tcx>(
641642
.fatal(&format!("allocation of `{}` {}", box_layout.ty, s));
642643
}
643644
};
644-
let instance = ty::Instance::mono(fx.tcx, def_id);
645+
let instance = ty::Instance::mono(fx.tcx, def_id).polymorphize(fx.tcx);
645646
let func_ref = fx.get_function_ref(instance);
646647
let call = fx.bcx.ins().call(func_ref, &[llsize, llalign]);
647648
let ptr = fx.bcx.inst_results(call)[0];

src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ impl<'tcx, B: Backend> LayoutOf for FunctionCx<'_, 'tcx, B> {
287287
type TyAndLayout = TyAndLayout<'tcx>;
288288

289289
fn layout_of(&self, ty: Ty<'tcx>) -> TyAndLayout<'tcx> {
290-
assert!(!ty.needs_subst());
290+
assert!(!ty.still_further_specializable());
291291
self.tcx
292292
.layout_of(ParamEnv::reveal_all().and(&ty))
293293
.unwrap_or_else(|e| {

src/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ fn data_id_for_static(
228228
def_id: DefId,
229229
linkage: Linkage,
230230
) -> DataId {
231-
let instance = Instance::mono(tcx, def_id);
231+
let instance = Instance::mono(tcx, def_id).polymorphize(tcx);
232232
let symbol_name = tcx.symbol_name(instance).name;
233233
let ty = instance.ty(tcx, ParamEnv::reveal_all());
234234
let is_mutable = if tcx.is_mutable_static(def_id) {

src/main_shim.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub(crate) fn maybe_create_entry_wrapper(
2121
None => return,
2222
};
2323

24-
let instance = Instance::mono(tcx, main_def_id);
24+
let instance = Instance::mono(tcx, main_def_id).polymorphize(tcx);
2525
if module.get_name(&*tcx.symbol_name(instance).name).is_none() {
2626
return;
2727
}
@@ -58,7 +58,7 @@ pub(crate) fn maybe_create_entry_wrapper(
5858
.declare_function("main", Linkage::Export, &cmain_sig)
5959
.unwrap();
6060

61-
let instance = Instance::mono(tcx, rust_main_def_id);
61+
let instance = Instance::mono(tcx, rust_main_def_id).polymorphize(tcx);
6262

6363
let (main_name, main_sig) =
6464
get_function_name_and_sig(tcx, m.isa().triple(), instance, false);
@@ -90,7 +90,8 @@ pub(crate) fn maybe_create_entry_wrapper(
9090
tcx.intern_substs(&[main_ret_ty.into()]),
9191
)
9292
.unwrap()
93-
.unwrap();
93+
.unwrap()
94+
.polymorphize(tcx);
9495
let start_func_id = import_function(tcx, m, start_instance);
9596

9697
let main_val = bcx

src/vtable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn build_vtable<'tcx>(
9393
let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize;
9494

9595
let drop_in_place_fn =
96-
import_function(tcx, fx.module, Instance::resolve_drop_in_place(tcx, layout.ty));
96+
import_function(tcx, fx.module, Instance::resolve_drop_in_place(tcx, layout.ty).polymorphize(fx.tcx));
9797

9898
let mut components: Vec<_> = vec![Some(drop_in_place_fn), None, None];
9999

@@ -109,7 +109,7 @@ fn build_vtable<'tcx>(
109109
Some(import_function(
110110
tcx,
111111
fx.module,
112-
Instance::resolve_for_vtable(tcx, ParamEnv::reveal_all(), def_id, substs).unwrap(),
112+
Instance::resolve_for_vtable(tcx, ParamEnv::reveal_all(), def_id, substs).unwrap().polymorphize(fx.tcx),
113113
))
114114
})
115115
});

0 commit comments

Comments
 (0)