Skip to content

Commit e34e710

Browse files
committed
Refactor to create InstanceDef::fn_sig
1 parent 137710e commit e34e710

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,27 @@ impl<'tcx> InstanceDef<'tcx> {
321321
| InstanceDef::VTableShim(..) => true,
322322
}
323323
}
324+
325+
/// Computes the signature of the InstanceDef, possibly with adjustments based on the kind of
326+
/// shim.
327+
pub fn fn_sig(&self, tcx: TyCtxt<'tcx>) -> EarlyBinder<ty::PolyFnSig<'tcx>> {
328+
tcx.fn_sig(self.def_id()).map_bound(|sig| {
329+
sig.map_bound(|mut sig| {
330+
if let InstanceDef::VTableShim(..) = self {
331+
// Modify fn(self, ...) to fn(self: *mut Self, ...)
332+
let mut inputs_and_output = sig.inputs_and_output.to_vec();
333+
let self_arg = &mut inputs_and_output[0];
334+
debug_assert!(
335+
tcx.generics_of(self.def_id()).has_self
336+
&& *self_arg == tcx.types.self_param
337+
);
338+
*self_arg = Ty::new_mut_ptr(tcx, *self_arg);
339+
sig.inputs_and_output = tcx.mk_type_list(&inputs_and_output);
340+
}
341+
sig
342+
})
343+
})
344+
}
324345
}
325346

326347
fn fmt_instance_def(f: &mut fmt::Formatter<'_>, instance_def: &InstanceDef<'_>) -> fmt::Result {

compiler/rustc_mir_transform/src/shim.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ fn build_call_shim<'tcx>(
736736
};
737737

738738
let def_id = instance.def_id();
739-
let sig = tcx.fn_sig(def_id);
739+
let sig = instance.fn_sig(tcx);
740740
let sig = sig.map_bound(|sig| tcx.instantiate_bound_regions_with_erased(sig));
741741

742742
assert_eq!(sig_args.is_some(), !instance.has_polymorphic_mir_body());
@@ -773,17 +773,6 @@ fn build_call_shim<'tcx>(
773773
sig.inputs_and_output = tcx.mk_type_list(&inputs_and_output);
774774
}
775775

776-
// FIXME(eddyb) avoid having this snippet both here and in
777-
// `Instance::fn_sig` (introduce `InstanceDef::fn_sig`?).
778-
if let ty::InstanceDef::VTableShim(..) = instance {
779-
// Modify fn(self, ...) to fn(self: *mut Self, ...)
780-
let mut inputs_and_output = sig.inputs_and_output.to_vec();
781-
let self_arg = &mut inputs_and_output[0];
782-
debug_assert!(tcx.generics_of(def_id).has_self && *self_arg == tcx.types.self_param);
783-
*self_arg = Ty::new_mut_ptr(tcx, *self_arg);
784-
sig.inputs_and_output = tcx.mk_type_list(&inputs_and_output);
785-
}
786-
787776
let span = tcx.def_span(def_id);
788777

789778
debug!(?sig);

compiler/rustc_ty_utils/src/abi.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,19 @@ fn fn_sig_for_fn_abi<'tcx>(
4343

4444
let ty = instance.ty(tcx, param_env);
4545
match *ty.kind() {
46-
ty::FnDef(..) => {
46+
ty::FnDef(def_id, args) => {
4747
// HACK(davidtwco,eddyb): This is a workaround for polymorphization considering
4848
// parameters unused if they show up in the signature, but not in the `mir::Body`
4949
// (i.e. due to being inside a projection that got normalized, see
5050
// `tests/ui/polymorphization/normalized_sig_types.rs`), and codegen not keeping
5151
// track of a polymorphization `ParamEnv` to allow normalizing later.
5252
//
5353
// We normalize the `fn_sig` again after instantiating at a later point.
54-
let mut sig = match *ty.kind() {
55-
ty::FnDef(def_id, args) => tcx
56-
.fn_sig(def_id)
57-
.map_bound(|fn_sig| {
58-
tcx.normalize_erasing_regions(tcx.param_env(def_id), fn_sig)
59-
})
60-
.instantiate(tcx, args),
61-
_ => unreachable!(),
62-
};
63-
64-
if let ty::InstanceDef::VTableShim(..) = instance.def {
65-
// Modify `fn(self, ...)` to `fn(self: *mut Self, ...)`.
66-
sig = sig.map_bound(|mut sig| {
67-
let mut inputs_and_output = sig.inputs_and_output.to_vec();
68-
inputs_and_output[0] = Ty::new_mut_ptr(tcx, inputs_and_output[0]);
69-
sig.inputs_and_output = tcx.mk_type_list(&inputs_and_output);
70-
sig
71-
});
72-
}
73-
sig
54+
instance
55+
.def
56+
.fn_sig(tcx)
57+
.map_bound(|fn_sig| tcx.normalize_erasing_regions(tcx.param_env(def_id), fn_sig))
58+
.instantiate(tcx, args)
7459
}
7560
ty::Closure(def_id, args) => {
7661
let sig = args.as_closure().sig();

0 commit comments

Comments
 (0)