Skip to content

Commit

Permalink
Fix has_body() and change resolve_drop_in_place() sig
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
celinval committed Nov 16, 2023
1 parent 4c00aa3 commit 8e81fc0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
10 changes: 9 additions & 1 deletion compiler/rustc_smir/src/rustc_smir/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ impl<'tcx> BodyBuilder<'tcx> {
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 {
let mut body = self.tcx.instance_mir(self.instance.def).clone();
self.visit_body(&mut body);
if self.tcx.def_kind(self.instance.def_id()).is_fn_like() || !self.instance.args.is_empty()
{
self.visit_body(&mut body);
}
body.stable(tables)
}

Expand Down
23 changes: 9 additions & 14 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
}
}

fn resolve_drop_in_place(
&self,
ty: stable_mir::ty::Ty,
) -> Option<stable_mir::mir::mono::Instance> {
fn resolve_drop_in_place(&self, ty: stable_mir::ty::Ty) -> stable_mir::mir::mono::Instance {
let mut tables = self.0.borrow_mut();
let internal_ty = ty.internal(&mut *tables);
let instance = Instance::resolve_drop_in_place(tables.tcx, internal_ty);
matches!(instance.def, ty::InstanceDef::DropGlue(_, Some(_)))
.then(|| instance.stable(&mut *tables))
instance.stable(&mut *tables)
}

fn resolve_for_fn_ptr(
Expand Down Expand Up @@ -328,9 +324,11 @@ impl<'tcx> Tables<'tcx> {
fn has_body(&self, instance: Instance<'tcx>) -> bool {
let def_id = instance.def_id();
self.tcx.is_mir_available(def_id)
&& !matches!(
|| !matches!(
instance.def,
ty::InstanceDef::Virtual(..) | ty::InstanceDef::Intrinsic(..)
ty::InstanceDef::Virtual(..)
| ty::InstanceDef::Intrinsic(..)
| ty::InstanceDef::Item(..)
)
}
}
Expand Down Expand Up @@ -364,15 +362,12 @@ fn new_item_kind(kind: DefKind) -> ItemKind {
| DefKind::OpaqueTy
| DefKind::Field
| DefKind::LifetimeParam
| DefKind::Impl { .. }
| DefKind::Ctor(_, _)
| DefKind::GlobalAsm => {
unreachable!("Not a valid item kind: {kind:?}");
}
DefKind::Closure
| DefKind::Coroutine
| DefKind::Ctor(_, _)
| DefKind::AssocFn
| DefKind::Impl { .. }
| DefKind::Fn => ItemKind::Fn,
DefKind::Closure | DefKind::Coroutine | DefKind::AssocFn | DefKind::Fn => ItemKind::Fn,
DefKind::Const | DefKind::InlineConst | DefKind::AssocConst | DefKind::AnonConst => {
ItemKind::Const
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/stable_mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ pub trait Context {
fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;

/// 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.
fn resolve_for_fn_ptr(&self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
Expand Down
3 changes: 1 addition & 2 deletions compiler/stable_mir/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ impl Instance {
}

/// 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) -> Option<Instance> {
pub fn resolve_drop_in_place(ty: Ty) -> Instance {
with(|cx| cx.resolve_drop_in_place(ty))
}

Expand Down

0 comments on commit 8e81fc0

Please sign in to comment.