Skip to content

Commit 8e81fc0

Browse files
committed
Fix has_body() and change resolve_drop_in_place() sig
Fixed the `has_body()` function operator. Before that, this function was returning false for all shims. Change resolve_drop_in_place() to also return an instance for empty shims, since they may still be required for vtable construction.
1 parent 4c00aa3 commit 8e81fc0

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

compiler/rustc_smir/src/rustc_smir/builder.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,17 @@ impl<'tcx> BodyBuilder<'tcx> {
1919
BodyBuilder { tcx, instance }
2020
}
2121

22+
/// Build a stable monomorphic body for a given instance based on the MIR body.
23+
///
24+
/// Note that we skip instantiation for static and constants. Trying to do so can cause ICE.
25+
///
26+
/// We do monomorphize non-generic functions to eval unevaluated constants.
2227
pub fn build(mut self, tables: &mut Tables<'tcx>) -> stable_mir::mir::Body {
2328
let mut body = self.tcx.instance_mir(self.instance.def).clone();
24-
self.visit_body(&mut body);
29+
if self.tcx.def_kind(self.instance.def_id()).is_fn_like() || !self.instance.args.is_empty()
30+
{
31+
self.visit_body(&mut body);
32+
}
2533
body.stable(tables)
2634
}
2735

compiler/rustc_smir/src/rustc_smir/mod.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
267267
}
268268
}
269269

270-
fn resolve_drop_in_place(
271-
&self,
272-
ty: stable_mir::ty::Ty,
273-
) -> Option<stable_mir::mir::mono::Instance> {
270+
fn resolve_drop_in_place(&self, ty: stable_mir::ty::Ty) -> stable_mir::mir::mono::Instance {
274271
let mut tables = self.0.borrow_mut();
275272
let internal_ty = ty.internal(&mut *tables);
276273
let instance = Instance::resolve_drop_in_place(tables.tcx, internal_ty);
277-
matches!(instance.def, ty::InstanceDef::DropGlue(_, Some(_)))
278-
.then(|| instance.stable(&mut *tables))
274+
instance.stable(&mut *tables)
279275
}
280276

281277
fn resolve_for_fn_ptr(
@@ -328,9 +324,11 @@ impl<'tcx> Tables<'tcx> {
328324
fn has_body(&self, instance: Instance<'tcx>) -> bool {
329325
let def_id = instance.def_id();
330326
self.tcx.is_mir_available(def_id)
331-
&& !matches!(
327+
|| !matches!(
332328
instance.def,
333-
ty::InstanceDef::Virtual(..) | ty::InstanceDef::Intrinsic(..)
329+
ty::InstanceDef::Virtual(..)
330+
| ty::InstanceDef::Intrinsic(..)
331+
| ty::InstanceDef::Item(..)
334332
)
335333
}
336334
}
@@ -364,15 +362,12 @@ fn new_item_kind(kind: DefKind) -> ItemKind {
364362
| DefKind::OpaqueTy
365363
| DefKind::Field
366364
| DefKind::LifetimeParam
365+
| DefKind::Impl { .. }
366+
| DefKind::Ctor(_, _)
367367
| DefKind::GlobalAsm => {
368368
unreachable!("Not a valid item kind: {kind:?}");
369369
}
370-
DefKind::Closure
371-
| DefKind::Coroutine
372-
| DefKind::Ctor(_, _)
373-
| DefKind::AssocFn
374-
| DefKind::Impl { .. }
375-
| DefKind::Fn => ItemKind::Fn,
370+
DefKind::Closure | DefKind::Coroutine | DefKind::AssocFn | DefKind::Fn => ItemKind::Fn,
376371
DefKind::Const | DefKind::InlineConst | DefKind::AssocConst | DefKind::AnonConst => {
377372
ItemKind::Const
378373
}

compiler/stable_mir/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ pub trait Context {
253253
fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
254254

255255
/// Resolve an instance for drop_in_place for the given type.
256-
fn resolve_drop_in_place(&self, ty: Ty) -> Option<Instance>;
256+
fn resolve_drop_in_place(&self, ty: Ty) -> Instance;
257257

258258
/// Resolve instance for a function pointer.
259259
fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;

compiler/stable_mir/src/mir/mono.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ impl Instance {
5656
}
5757

5858
/// Resolve the drop in place for a given type.
59-
/// Return `None` if the drop is a no-op.
60-
pub fn resolve_drop_in_place(ty: Ty) -> Option<Instance> {
59+
pub fn resolve_drop_in_place(ty: Ty) -> Instance {
6160
with(|cx| cx.resolve_drop_in_place(ty))
6261
}
6362

0 commit comments

Comments
 (0)