diff --git a/compiler/rustc_smir/src/rustc_smir/builder.rs b/compiler/rustc_smir/src/rustc_smir/builder.rs index c133adfdede..7e74a1d92c7 100644 --- a/compiler/rustc_smir/src/rustc_smir/builder.rs +++ b/compiler/rustc_smir/src/rustc_smir/builder.rs @@ -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) } diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 8662a9c7ec1..3df09cef1c7 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -267,15 +267,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> { } } - fn resolve_drop_in_place( - &self, - ty: stable_mir::ty::Ty, - ) -> Option { + 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( @@ -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(..) ) } } @@ -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 } diff --git a/compiler/stable_mir/src/lib.rs b/compiler/stable_mir/src/lib.rs index 9873e7e3eed..0262fb536e7 100644 --- a/compiler/stable_mir/src/lib.rs +++ b/compiler/stable_mir/src/lib.rs @@ -253,7 +253,7 @@ pub trait Context { fn resolve_instance(&self, def: FnDef, args: &GenericArgs) -> Option; /// Resolve an instance for drop_in_place for the given type. - fn resolve_drop_in_place(&self, ty: Ty) -> Option; + 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; diff --git a/compiler/stable_mir/src/mir/mono.rs b/compiler/stable_mir/src/mir/mono.rs index 3524295f597..8562bfd3905 100644 --- a/compiler/stable_mir/src/mir/mono.rs +++ b/compiler/stable_mir/src/mir/mono.rs @@ -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 { + pub fn resolve_drop_in_place(ty: Ty) -> Instance { with(|cx| cx.resolve_drop_in_place(ty)) }