Skip to content

Commit fe4b485

Browse files
authored
Rollup merge of #73359 - jonas-schievink:do-the-shimmy, r=matthewjasper
shim.rs: avoid creating `Call` terminators calling `Self` Also contains some cleanup and doc comment additions so I could make sense of the code. Fixes #73109 Closes #73175 r? @matthewjasper
2 parents 17b80d9 + 4cb26ad commit fe4b485

File tree

6 files changed

+123
-33
lines changed

6 files changed

+123
-33
lines changed

src/librustc_middle/ty/instance.rs

+41-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ use rustc_macros::HashStable;
99

1010
use std::fmt;
1111

12+
/// A monomorphized `InstanceDef`.
13+
///
14+
/// Monomorphization happens on-the-fly and no monomorphized MIR is ever created. Instead, this type
15+
/// simply couples a potentially generic `InstanceDef` with some substs, and codegen and const eval
16+
/// will do all required substitution as they run.
1217
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
1318
#[derive(HashStable, Lift)]
1419
pub struct Instance<'tcx> {
@@ -18,10 +23,26 @@ pub struct Instance<'tcx> {
1823

1924
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, HashStable)]
2025
pub enum InstanceDef<'tcx> {
26+
/// A user-defined callable item.
27+
///
28+
/// This includes:
29+
/// - `fn` items
30+
/// - closures
31+
/// - generators
2132
Item(DefId),
33+
34+
/// An intrinsic `fn` item (with `"rust-intrinsic"` or `"platform-intrinsic"` ABI).
35+
///
36+
/// Alongside `Virtual`, this is the only `InstanceDef` that does not have its own callable MIR.
37+
/// Instead, codegen and const eval "magically" evaluate calls to intrinsics purely in the
38+
/// caller.
2239
Intrinsic(DefId),
2340

24-
/// `<T as Trait>::method` where `method` receives unsizeable `self: Self`.
41+
/// `<T as Trait>::method` where `method` receives unsizeable `self: Self` (part of the
42+
/// `unsized_locals` feature).
43+
///
44+
/// The generated shim will take `Self` via `*mut Self` - conceptually this is `&owned Self` -
45+
/// and dereference the argument to call the original function.
2546
VtableShim(DefId),
2647

2748
/// `fn()` pointer where the function itself cannot be turned into a pointer.
@@ -37,27 +58,31 @@ pub enum InstanceDef<'tcx> {
3758
/// (the definition of the function itself).
3859
ReifyShim(DefId),
3960

40-
/// `<fn() as FnTrait>::call_*`
61+
/// `<fn() as FnTrait>::call_*` (generated `FnTrait` implementation for `fn()` pointers).
62+
///
4163
/// `DefId` is `FnTrait::call_*`.
4264
///
4365
/// NB: the (`fn` pointer) type must currently be monomorphic to avoid double substitution
4466
/// problems with the MIR shim bodies. `Instance::resolve` enforces this.
4567
// FIXME(#69925) support polymorphic MIR shim bodies properly instead.
4668
FnPtrShim(DefId, Ty<'tcx>),
4769

48-
/// `<dyn Trait as Trait>::fn`, "direct calls" of which are implicitly
49-
/// codegen'd as virtual calls.
70+
/// Dynamic dispatch to `<dyn Trait as Trait>::fn`.
5071
///
51-
/// NB: if this is reified to a `fn` pointer, a `ReifyShim` is used
52-
/// (see `ReifyShim` above for more details on that).
72+
/// This `InstanceDef` does not have callable MIR. Calls to `Virtual` instances must be
73+
/// codegen'd as virtual calls through the vtable.
74+
///
75+
/// If this is reified to a `fn` pointer, a `ReifyShim` is used (see `ReifyShim` above for more
76+
/// details on that).
5377
Virtual(DefId, usize),
5478

55-
/// `<[mut closure] as FnOnce>::call_once`
56-
ClosureOnceShim {
57-
call_once: DefId,
58-
},
79+
/// `<[FnMut closure] as FnOnce>::call_once`.
80+
///
81+
/// The `DefId` is the ID of the `call_once` method in `FnOnce`.
82+
ClosureOnceShim { call_once: DefId },
5983

6084
/// `core::ptr::drop_in_place::<T>`.
85+
///
6186
/// The `DefId` is for `core::ptr::drop_in_place`.
6287
/// The `Option<Ty<'tcx>>` is either `Some(T)`, or `None` for empty drop
6388
/// glue.
@@ -67,7 +92,12 @@ pub enum InstanceDef<'tcx> {
6792
// FIXME(#69925) support polymorphic MIR shim bodies properly instead.
6893
DropGlue(DefId, Option<Ty<'tcx>>),
6994

70-
///`<T as Clone>::clone` shim.
95+
/// Compiler-generated `<T as Clone>::clone` implementation.
96+
///
97+
/// For all types that automatically implement `Copy`, a trivial `Clone` impl is provided too.
98+
/// Additionally, arrays, tuples, and closures get a `Clone` shim even if they aren't `Copy`.
99+
///
100+
/// The `DefId` is for `Clone::clone`, the `Ty` is the type `T` with the builtin `Clone` impl.
71101
///
72102
/// NB: the type must currently be monomorphic to avoid double substitution
73103
/// problems with the MIR shim bodies. `Instance::resolve` enforces this.

src/librustc_mir/shim.rs

+49-15
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,9 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
3232

3333
let mut result = match instance {
3434
ty::InstanceDef::Item(..) => bug!("item {:?} passed to make_shim", instance),
35-
ty::InstanceDef::VtableShim(def_id) => build_call_shim(
36-
tcx,
37-
instance,
38-
Some(Adjustment::DerefMove),
39-
CallKind::Direct(def_id),
40-
None,
41-
),
35+
ty::InstanceDef::VtableShim(def_id) => {
36+
build_call_shim(tcx, instance, Some(Adjustment::Deref), CallKind::Direct(def_id), None)
37+
}
4238
ty::InstanceDef::FnPtrShim(def_id, ty) => {
4339
// FIXME(eddyb) support generating shims for a "shallow type",
4440
// e.g. `Foo<_>` or `[_]` instead of requiring a fully monomorphic
@@ -60,7 +56,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
6056
let sig = tcx.erase_late_bound_regions(&ty.fn_sig(tcx));
6157
let arg_tys = sig.inputs();
6258

63-
build_call_shim(tcx, instance, Some(adjustment), CallKind::Indirect, Some(arg_tys))
59+
build_call_shim(tcx, instance, Some(adjustment), CallKind::Indirect(ty), Some(arg_tys))
6460
}
6561
// We are generating a call back to our def-id, which the
6662
// codegen backend knows to turn to an actual call, be it
@@ -134,15 +130,28 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
134130

135131
#[derive(Copy, Clone, Debug, PartialEq)]
136132
enum Adjustment {
133+
/// Pass the receiver as-is.
137134
Identity,
135+
136+
/// We get passed `&[mut] self` and call the target with `*self`.
137+
///
138+
/// This either copies `self` (if `Self: Copy`, eg. for function items), or moves out of it
139+
/// (for `VtableShim`, which effectively is passed `&own Self`).
138140
Deref,
139-
DerefMove,
141+
142+
/// We get passed `self: Self` and call the target with `&mut self`.
143+
///
144+
/// In this case we need to ensure that the `Self` is dropped after the call, as the callee
145+
/// won't do it for us.
140146
RefMut,
141147
}
142148

143149
#[derive(Copy, Clone, Debug, PartialEq)]
144-
enum CallKind {
145-
Indirect,
150+
enum CallKind<'tcx> {
151+
/// Call the `FnPtr` that was passed as the receiver.
152+
Indirect(Ty<'tcx>),
153+
154+
/// Call a known `FnDef`.
146155
Direct(DefId),
147156
}
148157

@@ -662,7 +671,7 @@ fn build_call_shim<'tcx>(
662671
tcx: TyCtxt<'tcx>,
663672
instance: ty::InstanceDef<'tcx>,
664673
rcvr_adjustment: Option<Adjustment>,
665-
call_kind: CallKind,
674+
call_kind: CallKind<'tcx>,
666675
untuple_args: Option<&[Ty<'tcx>]>,
667676
) -> Body<'tcx> {
668677
debug!(
@@ -675,6 +684,29 @@ fn build_call_shim<'tcx>(
675684
let sig = tcx.fn_sig(def_id);
676685
let mut sig = tcx.erase_late_bound_regions(&sig);
677686

687+
if let CallKind::Indirect(fnty) = call_kind {
688+
// `sig` determines our local decls, and thus the callee type in the `Call` terminator. This
689+
// can only be an `FnDef` or `FnPtr`, but currently will be `Self` since the types come from
690+
// the implemented `FnX` trait.
691+
692+
// Apply the opposite adjustment to the MIR input.
693+
let mut inputs_and_output = sig.inputs_and_output.to_vec();
694+
695+
// Initial signature is `fn(&? Self, Args) -> Self::Output` where `Args` is a tuple of the
696+
// fn arguments. `Self` may be passed via (im)mutable reference or by-value.
697+
assert_eq!(inputs_and_output.len(), 3);
698+
699+
// `Self` is always the original fn type `ty`. The MIR call terminator is only defined for
700+
// `FnDef` and `FnPtr` callees, not the `Self` type param.
701+
let self_arg = &mut inputs_and_output[0];
702+
*self_arg = match rcvr_adjustment.unwrap() {
703+
Adjustment::Identity => fnty,
704+
Adjustment::Deref => tcx.mk_imm_ptr(fnty),
705+
Adjustment::RefMut => tcx.mk_mut_ptr(fnty),
706+
};
707+
sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output);
708+
}
709+
678710
// FIXME(eddyb) avoid having this snippet both here and in
679711
// `Instance::fn_sig` (introduce `InstanceDef::fn_sig`?).
680712
if let ty::InstanceDef::VtableShim(..) = instance {
@@ -701,8 +733,7 @@ fn build_call_shim<'tcx>(
701733

702734
let rcvr = rcvr_adjustment.map(|rcvr_adjustment| match rcvr_adjustment {
703735
Adjustment::Identity => Operand::Move(rcvr_place()),
704-
Adjustment::Deref => Operand::Move(tcx.mk_place_deref(rcvr_place())), // Can't copy `&mut`
705-
Adjustment::DerefMove => Operand::Move(tcx.mk_place_deref(rcvr_place())),
736+
Adjustment::Deref => Operand::Move(tcx.mk_place_deref(rcvr_place())),
706737
Adjustment::RefMut => {
707738
// let rcvr = &mut rcvr;
708739
let ref_rcvr = local_decls.push(
@@ -728,7 +759,10 @@ fn build_call_shim<'tcx>(
728759
});
729760

730761
let (callee, mut args) = match call_kind {
731-
CallKind::Indirect => (rcvr.unwrap(), vec![]),
762+
// `FnPtr` call has no receiver. Args are untupled below.
763+
CallKind::Indirect(_) => (rcvr.unwrap(), vec![]),
764+
765+
// `FnDef` call with optional receiver.
732766
CallKind::Direct(def_id) => {
733767
let ty = tcx.type_of(def_id);
734768
(

src/librustc_mir/transform/validate.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_middle::{
99
},
1010
ty::{self, ParamEnv, TyCtxt},
1111
};
12-
use rustc_span::def_id::DefId;
1312

1413
#[derive(Copy, Clone, Debug)]
1514
enum EdgeKind {
@@ -24,15 +23,14 @@ pub struct Validator {
2423

2524
impl<'tcx> MirPass<'tcx> for Validator {
2625
fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut Body<'tcx>) {
27-
let def_id = source.def_id();
28-
let param_env = tcx.param_env(def_id);
29-
TypeChecker { when: &self.when, def_id, body, tcx, param_env }.visit_body(body);
26+
let param_env = tcx.param_env(source.def_id());
27+
TypeChecker { when: &self.when, source, body, tcx, param_env }.visit_body(body);
3028
}
3129
}
3230

3331
struct TypeChecker<'a, 'tcx> {
3432
when: &'a str,
35-
def_id: DefId,
33+
source: MirSource<'tcx>,
3634
body: &'a Body<'tcx>,
3735
tcx: TyCtxt<'tcx>,
3836
param_env: ParamEnv<'tcx>,
@@ -47,7 +45,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
4745
span,
4846
&format!(
4947
"broken MIR in {:?} ({}) at {:?}:\n{}",
50-
self.def_id,
48+
self.source.instance,
5149
self.when,
5250
location,
5351
msg.as_ref()

src/librustc_trait_selection/traits/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ pub fn get_vtable_index_of_object_method<N>(
302302
) -> usize {
303303
// Count number of methods preceding the one we are selecting and
304304
// add them to the total offset.
305-
// Skip over associated types and constants.
305+
// Skip over associated types and constants, as those aren't stored in the vtable.
306306
let mut entries = object.vtable_base;
307307
for trait_item in tcx.associated_items(object.upcast_trait_ref.def_id()).in_definition_order() {
308308
if trait_item.def_id == method_def_id {

src/test/mir-opt/fn-ptr-shim.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// compile-flags: -Zmir-opt-level=0 -Zvalidate-mir
2+
3+
// Tests that the `<fn() as Fn>` shim does not create a `Call` terminator with a `Self` callee
4+
// (as only `FnDef` and `FnPtr` callees are allowed in MIR).
5+
6+
// EMIT_MIR rustc.ops-function-Fn-call.AddMovesForPackedDrops.before.mir
7+
fn main() {
8+
call(noop as fn());
9+
}
10+
11+
fn noop() {}
12+
13+
fn call<F: Fn()>(f: F) {
14+
f();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// MIR for `std::ops::Fn::call` before AddMovesForPackedDrops
2+
3+
fn std::ops::Fn::call(_1: *const fn(), _2: Args) -> <Self as std::ops::FnOnce<Args>>::Output {
4+
let mut _0: <Self as std::ops::FnOnce<Args>>::Output; // return place in scope 0 at $SRC_DIR/libcore/ops/function.rs:LL:COL
5+
6+
bb0: {
7+
_0 = move (*_1)() -> bb1; // scope 0 at $SRC_DIR/libcore/ops/function.rs:LL:COL
8+
}
9+
10+
bb1: {
11+
return; // scope 0 at $SRC_DIR/libcore/ops/function.rs:LL:COL
12+
}
13+
}

0 commit comments

Comments
 (0)