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.
This commit is contained in:
Celina G. Val 2023-11-16 12:04:25 -08:00
parent 4c00aa3d74
commit 8e81fc0087
4 changed files with 20 additions and 18 deletions

View File

@ -19,9 +19,17 @@ impl<'tcx> BodyBuilder<'tcx> {
BodyBuilder { tcx, instance } BodyBuilder { tcx, instance }
} }
/// Build a stable monomorphic body for a given instance based on the MIR body.
///
/// Note that we skip instantiation for static and constants. Trying to do so can cause ICE.
///
/// We do monomorphize non-generic functions to eval unevaluated constants.
pub fn build(mut self, tables: &mut Tables<'tcx>) -> stable_mir::mir::Body { pub fn build(mut self, tables: &mut Tables<'tcx>) -> stable_mir::mir::Body {
let mut body = self.tcx.instance_mir(self.instance.def).clone(); let mut body = self.tcx.instance_mir(self.instance.def).clone();
if self.tcx.def_kind(self.instance.def_id()).is_fn_like() || !self.instance.args.is_empty()
{
self.visit_body(&mut body); self.visit_body(&mut body);
}
body.stable(tables) body.stable(tables)
} }

View File

@ -267,15 +267,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
} }
} }
fn resolve_drop_in_place( fn resolve_drop_in_place(&self, ty: stable_mir::ty::Ty) -> stable_mir::mir::mono::Instance {
&self,
ty: stable_mir::ty::Ty,
) -> Option<stable_mir::mir::mono::Instance> {
let mut tables = self.0.borrow_mut(); let mut tables = self.0.borrow_mut();
let internal_ty = ty.internal(&mut *tables); let internal_ty = ty.internal(&mut *tables);
let instance = Instance::resolve_drop_in_place(tables.tcx, internal_ty); let instance = Instance::resolve_drop_in_place(tables.tcx, internal_ty);
matches!(instance.def, ty::InstanceDef::DropGlue(_, Some(_))) instance.stable(&mut *tables)
.then(|| instance.stable(&mut *tables))
} }
fn resolve_for_fn_ptr( fn resolve_for_fn_ptr(
@ -328,9 +324,11 @@ impl<'tcx> Tables<'tcx> {
fn has_body(&self, instance: Instance<'tcx>) -> bool { fn has_body(&self, instance: Instance<'tcx>) -> bool {
let def_id = instance.def_id(); let def_id = instance.def_id();
self.tcx.is_mir_available(def_id) self.tcx.is_mir_available(def_id)
&& !matches!( || !matches!(
instance.def, instance.def,
ty::InstanceDef::Virtual(..) | ty::InstanceDef::Intrinsic(..) ty::InstanceDef::Virtual(..)
| ty::InstanceDef::Intrinsic(..)
| ty::InstanceDef::Item(..)
) )
} }
} }
@ -364,15 +362,12 @@ fn new_item_kind(kind: DefKind) -> ItemKind {
| DefKind::OpaqueTy | DefKind::OpaqueTy
| DefKind::Field | DefKind::Field
| DefKind::LifetimeParam | DefKind::LifetimeParam
| DefKind::Impl { .. }
| DefKind::Ctor(_, _)
| DefKind::GlobalAsm => { | DefKind::GlobalAsm => {
unreachable!("Not a valid item kind: {kind:?}"); unreachable!("Not a valid item kind: {kind:?}");
} }
DefKind::Closure DefKind::Closure | DefKind::Coroutine | DefKind::AssocFn | DefKind::Fn => ItemKind::Fn,
| DefKind::Coroutine
| DefKind::Ctor(_, _)
| DefKind::AssocFn
| DefKind::Impl { .. }
| DefKind::Fn => ItemKind::Fn,
DefKind::Const | DefKind::InlineConst | DefKind::AssocConst | DefKind::AnonConst => { DefKind::Const | DefKind::InlineConst | DefKind::AssocConst | DefKind::AnonConst => {
ItemKind::Const ItemKind::Const
} }

View File

@ -253,7 +253,7 @@ pub trait Context {
fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>; fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
/// Resolve an instance for drop_in_place for the given type. /// Resolve an instance for drop_in_place for the given type.
fn resolve_drop_in_place(&self, ty: Ty) -> Option<Instance>; fn resolve_drop_in_place(&self, ty: Ty) -> Instance;
/// Resolve instance for a function pointer. /// Resolve instance for a function pointer.
fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>; fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;

View File

@ -56,8 +56,7 @@ impl Instance {
} }
/// Resolve the drop in place for a given type. /// Resolve the drop in place for a given type.
/// Return `None` if the drop is a no-op. pub fn resolve_drop_in_place(ty: Ty) -> Instance {
pub fn resolve_drop_in_place(ty: Ty) -> Option<Instance> {
with(|cx| cx.resolve_drop_in_place(ty)) with(|cx| cx.resolve_drop_in_place(ty))
} }