Skip to content

Commit b30866f

Browse files
committed
rustc: use ReifyShim for reifying Virtual call instances.
1 parent b5f38f0 commit b30866f

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

src/librustc/ty/instance.rs

+21-13
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,22 @@ pub enum InstanceDef<'tcx> {
2828

2929
/// `fn()` pointer where the function itself cannot be turned into a pointer.
3030
///
31-
/// One example in the compiler today is functions annotated with `#[track_caller]`, which
32-
/// must have their implicit caller location argument populated for a call. Because this is a
33-
/// required part of the function's ABI but can't be tracked as a property of the function
34-
/// pointer, we create a single "caller location" at the site where the function is reified.
31+
/// One example is `<dyn Trait as Trait>::fn`, where the shim contains
32+
/// a virtual call, which codegen supports only via a direct call to the
33+
/// `<dyn Trait as Trait>::fn` instance (an `InstanceDef::Virtual`).
34+
///
35+
/// Another example is functions annotated with `#[track_caller]`, which
36+
/// must have their implicit caller location argument populated for a call.
37+
/// Because this is a required part of the function's ABI but can't be tracked
38+
/// as a property of the function pointer, we use a single "caller location"
39+
/// (the definition of the function itself).
3540
ReifyShim(DefId),
3641

3742
/// `<fn() as FnTrait>::call_*`
3843
/// `DefId` is `FnTrait::call_*`
3944
FnPtrShim(DefId, Ty<'tcx>),
4045

41-
/// `<Trait as Trait>::fn`
46+
/// `<dyn Trait as Trait>::fn`
4247
Virtual(DefId, usize),
4348

4449
/// `<[mut closure] as FnOnce>::call_once`
@@ -193,7 +198,7 @@ impl<'tcx> fmt::Display for Instance<'tcx> {
193198
write!(f, " - intrinsic")
194199
}
195200
InstanceDef::Virtual(_, num) => {
196-
write!(f, " - shim(#{})", num)
201+
write!(f, " - virtual#{}", num)
197202
}
198203
InstanceDef::FnPtrShim(_, ty) => {
199204
write!(f, " - shim({:?})", ty)
@@ -308,20 +313,23 @@ impl<'tcx> Instance<'tcx> {
308313
substs: SubstsRef<'tcx>,
309314
) -> Option<Instance<'tcx>> {
310315
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
311-
Instance::resolve(tcx, param_env, def_id, substs).map(|resolved| {
316+
Instance::resolve(tcx, param_env, def_id, substs).map(|mut resolved| {
312317
let has_track_caller = |def| tcx.codegen_fn_attrs(def).flags
313318
.contains(CodegenFnAttrFlags::TRACK_CALLER);
314319

315320
match resolved.def {
316321
InstanceDef::Item(def_id) if has_track_caller(def_id) => {
317322
debug!(" => fn pointer created for function with #[track_caller]");
318-
Instance {
319-
def: InstanceDef::ReifyShim(def_id),
320-
substs,
321-
}
322-
},
323-
_ => resolved,
323+
resolved.def = InstanceDef::ReifyShim(def_id);
324+
}
325+
InstanceDef::Virtual(def_id, _) => {
326+
debug!(" => fn pointer created for virtual call");
327+
resolved.def = InstanceDef::ReifyShim(def_id);
328+
}
329+
_ => {}
324330
}
331+
332+
resolved
325333
})
326334
}
327335

src/librustc_mir/monomorphize/collector.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -743,23 +743,21 @@ fn visit_instance_use<'tcx>(
743743
}
744744

745745
match instance.def {
746-
ty::InstanceDef::Intrinsic(def_id) => {
746+
ty::InstanceDef::Virtual(..) |
747+
ty::InstanceDef::Intrinsic(_) => {
747748
if !is_direct_call {
748-
bug!("intrinsic {:?} being reified", def_id);
749+
bug!("{:?} being reified", instance);
749750
}
750751
}
751-
ty::InstanceDef::VtableShim(..) |
752-
ty::InstanceDef::ReifyShim(..) |
753-
ty::InstanceDef::Virtual(..) |
754752
ty::InstanceDef::DropGlue(_, None) => {
755-
// don't need to emit shim if we are calling directly.
753+
// Don't need to emit noop drop glue if we are calling directly.
756754
if !is_direct_call {
757755
output.push(create_fn_mono_item(instance));
758756
}
759757
}
760-
ty::InstanceDef::DropGlue(_, Some(_)) => {
761-
output.push(create_fn_mono_item(instance));
762-
}
758+
ty::InstanceDef::DropGlue(_, Some(_)) |
759+
ty::InstanceDef::VtableShim(..) |
760+
ty::InstanceDef::ReifyShim(..) |
763761
ty::InstanceDef::ClosureOnceShim { .. } |
764762
ty::InstanceDef::Item(..) |
765763
ty::InstanceDef::FnPtrShim(..) |

src/librustc_mir/shim.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> &'tcx
6767
)
6868
}
6969
// We are generating a call back to our def-id, which the
70-
// codegen backend knows to turn to an actual virtual call.
71-
ty::InstanceDef::Virtual(def_id, _) |
72-
// ...or we are generating a direct call to a function for which indirect calls must be
73-
// codegen'd differently than direct ones (example: #[track_caller])
70+
// codegen backend knows to turn to an actual call, be it
71+
// a virtual call, or a direct call to a function for which
72+
// indirect calls must be codegen'd differently than direct ones
73+
// (such as `#[track_caller]`).
7474
ty::InstanceDef::ReifyShim(def_id) => {
7575
build_call_shim(
7676
tcx,
@@ -109,6 +109,9 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> &'tcx
109109
bug!("builtin clone shim {:?} not supported", instance)
110110
}
111111
}
112+
ty::InstanceDef::Virtual(..) => {
113+
bug!("InstanceDef::Virtual ({:?}) is for direct calls only", instance)
114+
}
112115
ty::InstanceDef::Intrinsic(_) => {
113116
bug!("creating shims from intrinsics ({:?}) is unsupported", instance)
114117
}

0 commit comments

Comments
 (0)