diff --git a/kani-compiler/src/codegen_cprover_gotoc/codegen/ty_stable.rs b/kani-compiler/src/codegen_cprover_gotoc/codegen/ty_stable.rs index 7e644cdecb77..7262f55a0719 100644 --- a/kani-compiler/src/codegen_cprover_gotoc/codegen/ty_stable.rs +++ b/kani-compiler/src/codegen_cprover_gotoc/codegen/ty_stable.rs @@ -7,8 +7,9 @@ //! `typ.rs`. use crate::codegen_cprover_gotoc::GotocCtx; +use crate::kani_middle::abi::LayoutOf; use cbmc::goto_program::Type; -use rustc_middle::ty::layout::{LayoutOf, TyAndLayout}; +use rustc_middle::ty::layout::{LayoutOf as _, TyAndLayout}; use rustc_smir::rustc_internal; use stable_mir::mir::mono::Instance; use stable_mir::mir::{Local, Operand, Place, Rvalue}; @@ -36,7 +37,7 @@ impl<'tcx> GotocCtx<'tcx> { } pub fn is_zst_stable(&self, ty: Ty) -> bool { - self.is_zst(rustc_internal::internal(self.tcx, ty)) + LayoutOf::new(ty).is_zst() } pub fn layout_of_stable(&self, ty: Ty) -> TyAndLayout<'tcx> { @@ -113,7 +114,7 @@ impl<'tcx> GotocCtx<'tcx> { /// This should be replaced by StableMIR `pretty_ty()` after /// is merged. pub fn pretty_ty(&self, ty: Ty) -> String { - rustc_internal::internal(self.tcx, ty).to_string() + ty.to_string() } pub fn requires_caller_location(&self, instance: Instance) -> bool { @@ -121,6 +122,7 @@ impl<'tcx> GotocCtx<'tcx> { instance_internal.def.requires_caller_location(self.tcx) } } + /// If given type is a Ref / Raw ref, return the pointee type. pub fn pointee_type(mir_type: Ty) -> Option { match mir_type.kind() { diff --git a/kani-compiler/src/kani_middle/abi.rs b/kani-compiler/src/kani_middle/abi.rs index 88068458a028..12fac6c8a43f 100644 --- a/kani-compiler/src/kani_middle/abi.rs +++ b/kani-compiler/src/kani_middle/abi.rs @@ -24,6 +24,11 @@ impl LayoutOf { self.layout.is_sized() } + /// Return whether this is a zero-sized type + pub fn is_zst(&self) -> bool { + self.is_sized() && self.layout.size.bytes() == 0 + } + /// Return whether the type is unsized and its tail is a foreign item. /// /// This will also return `true` if the type is foreign. diff --git a/kani-compiler/src/kani_middle/attributes.rs b/kani-compiler/src/kani_middle/attributes.rs index 3ee695e6f481..c699ced8a0ab 100644 --- a/kani-compiler/src/kani_middle/attributes.rs +++ b/kani-compiler/src/kani_middle/attributes.rs @@ -622,8 +622,9 @@ impl<'tcx> KaniAttributes<'tcx> { ), ); } else { - let instance = Instance::mono(tcx, self.item); - if !super::fn_abi(tcx, instance).args.is_empty() { + let instance = rustc_internal::stable(Instance::mono(tcx, self.item)); + let fn_abi = instance.fn_abi().unwrap(); + if !fn_abi.args.is_empty() { tcx.dcx().span_err(span, "functions used as harnesses cannot have any arguments"); } } diff --git a/kani-compiler/src/kani_middle/mod.rs b/kani-compiler/src/kani_middle/mod.rs index 62714bbe477c..dbdd421fcc36 100644 --- a/kani-compiler/src/kani_middle/mod.rs +++ b/kani-compiler/src/kani_middle/mod.rs @@ -7,17 +7,8 @@ use std::collections::HashSet; use crate::kani_queries::QueryDb; use rustc_hir::{def::DefKind, def_id::DefId as InternalDefId, def_id::LOCAL_CRATE}; -use rustc_middle::span_bug; -use rustc_middle::ty::layout::{ - FnAbiError, FnAbiOf, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError, - LayoutOfHelpers, TyAndLayout, -}; -use rustc_middle::ty::{self, Instance as InstanceInternal, Ty as TyInternal, TyCtxt}; +use rustc_middle::ty::TyCtxt; use rustc_smir::rustc_internal; -use rustc_span::Span; -use rustc_span::source_map::respan; -use rustc_target::abi::call::FnAbi; -use rustc_target::abi::{HasDataLayout, TargetDataLayout}; use stable_mir::CrateDef; use stable_mir::mir::mono::MonoItem; use stable_mir::ty::{FnDef, RigidTy, Span as SpanStable, Ty, TyKind}; @@ -155,80 +146,6 @@ impl SourceLocation { } } -/// Get the FnAbi of a given instance with no extra variadic arguments. -/// TODO: Get rid of this. Use instance.fn_abi() instead. -/// -pub fn fn_abi<'tcx>( - tcx: TyCtxt<'tcx>, - instance: InstanceInternal<'tcx>, -) -> &'tcx FnAbi<'tcx, TyInternal<'tcx>> { - let helper = CompilerHelpers { tcx }; - helper.fn_abi_of_instance(instance, ty::List::empty()) -} - -struct CompilerHelpers<'tcx> { - tcx: TyCtxt<'tcx>, -} - -impl<'tcx> HasParamEnv<'tcx> for CompilerHelpers<'tcx> { - fn param_env(&self) -> ty::ParamEnv<'tcx> { - ty::ParamEnv::reveal_all() - } -} - -impl<'tcx> HasTyCtxt<'tcx> for CompilerHelpers<'tcx> { - fn tcx(&self) -> TyCtxt<'tcx> { - self.tcx - } -} - -impl HasDataLayout for CompilerHelpers<'_> { - fn data_layout(&self) -> &TargetDataLayout { - self.tcx.data_layout() - } -} - -impl<'tcx> LayoutOfHelpers<'tcx> for CompilerHelpers<'tcx> { - type LayoutOfResult = TyAndLayout<'tcx>; - - #[inline] - fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: TyInternal<'tcx>) -> ! { - span_bug!(span, "failed to get layout for `{}`: {}", ty, err) - } -} - -/// Implement error handling for extracting function ABI information. -impl<'tcx> FnAbiOfHelpers<'tcx> for CompilerHelpers<'tcx> { - type FnAbiOfResult = &'tcx FnAbi<'tcx, TyInternal<'tcx>>; - - #[inline] - fn handle_fn_abi_err( - &self, - err: FnAbiError<'tcx>, - span: Span, - fn_abi_request: FnAbiRequest<'tcx>, - ) -> ! { - if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err { - self.tcx.dcx().emit_fatal(respan(span, err)) - } else { - match fn_abi_request { - FnAbiRequest::OfFnPtr { sig, extra_args } => { - span_bug!( - span, - "Error: {err:?}\n while running `fn_abi_of_fn_ptr. ({sig}, {extra_args:?})`", - ); - } - FnAbiRequest::OfInstance { instance, extra_args } => { - span_bug!( - span, - "Error: {err:?}\n while running `fn_abi_of_instance. ({instance}, {extra_args:?})`", - ); - } - } - } - } -} - /// Try to convert an internal `DefId` to a `FnDef`. pub fn stable_fn_def(tcx: TyCtxt, def_id: InternalDefId) -> Option { if let TyKind::RigidTy(RigidTy::FnDef(def, _)) =