Skip to content

Commit 17f4ff1

Browse files
authored
Cleanup a few internal compiler deps (#3739)
Some of this code was broken by the toolchain update. Instead of changing it, replace it by StableMIR APIs. Related to #3731 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent 0ebf089 commit 17f4ff1

File tree

4 files changed

+14
-89
lines changed

4 files changed

+14
-89
lines changed

kani-compiler/src/codegen_cprover_gotoc/codegen/ty_stable.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
//! `typ.rs`.
88
99
use crate::codegen_cprover_gotoc::GotocCtx;
10+
use crate::kani_middle::abi::LayoutOf;
1011
use cbmc::goto_program::Type;
11-
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
12+
use rustc_middle::ty::layout::{LayoutOf as _, TyAndLayout};
1213
use rustc_smir::rustc_internal;
1314
use stable_mir::mir::mono::Instance;
1415
use stable_mir::mir::{Local, Operand, Place, Rvalue};
@@ -36,7 +37,7 @@ impl<'tcx> GotocCtx<'tcx> {
3637
}
3738

3839
pub fn is_zst_stable(&self, ty: Ty) -> bool {
39-
self.is_zst(rustc_internal::internal(self.tcx, ty))
40+
LayoutOf::new(ty).is_zst()
4041
}
4142

4243
pub fn layout_of_stable(&self, ty: Ty) -> TyAndLayout<'tcx> {
@@ -113,14 +114,15 @@ impl<'tcx> GotocCtx<'tcx> {
113114
/// This should be replaced by StableMIR `pretty_ty()` after
114115
/// <https://github.com/rust-lang/rust/pull/118364> is merged.
115116
pub fn pretty_ty(&self, ty: Ty) -> String {
116-
rustc_internal::internal(self.tcx, ty).to_string()
117+
ty.to_string()
117118
}
118119

119120
pub fn requires_caller_location(&self, instance: Instance) -> bool {
120121
let instance_internal = rustc_internal::internal(self.tcx, instance);
121122
instance_internal.def.requires_caller_location(self.tcx)
122123
}
123124
}
125+
124126
/// If given type is a Ref / Raw ref, return the pointee type.
125127
pub fn pointee_type(mir_type: Ty) -> Option<Ty> {
126128
match mir_type.kind() {

kani-compiler/src/kani_middle/abi.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ impl LayoutOf {
2424
self.layout.is_sized()
2525
}
2626

27+
/// Return whether this is a zero-sized type
28+
pub fn is_zst(&self) -> bool {
29+
self.is_sized() && self.layout.size.bytes() == 0
30+
}
31+
2732
/// Return whether the type is unsized and its tail is a foreign item.
2833
///
2934
/// This will also return `true` if the type is foreign.

kani-compiler/src/kani_middle/attributes.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,9 @@ impl<'tcx> KaniAttributes<'tcx> {
622622
),
623623
);
624624
} else {
625-
let instance = Instance::mono(tcx, self.item);
626-
if !super::fn_abi(tcx, instance).args.is_empty() {
625+
let instance = rustc_internal::stable(Instance::mono(tcx, self.item));
626+
let fn_abi = instance.fn_abi().unwrap();
627+
if !fn_abi.args.is_empty() {
627628
tcx.dcx().span_err(span, "functions used as harnesses cannot have any arguments");
628629
}
629630
}

kani-compiler/src/kani_middle/mod.rs

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,8 @@ use std::collections::HashSet;
77

88
use crate::kani_queries::QueryDb;
99
use rustc_hir::{def::DefKind, def_id::DefId as InternalDefId, def_id::LOCAL_CRATE};
10-
use rustc_middle::span_bug;
11-
use rustc_middle::ty::layout::{
12-
FnAbiError, FnAbiOf, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError,
13-
LayoutOfHelpers, TyAndLayout,
14-
};
15-
use rustc_middle::ty::{self, Instance as InstanceInternal, Ty as TyInternal, TyCtxt};
10+
use rustc_middle::ty::TyCtxt;
1611
use rustc_smir::rustc_internal;
17-
use rustc_span::Span;
18-
use rustc_span::source_map::respan;
19-
use rustc_target::abi::call::FnAbi;
20-
use rustc_target::abi::{HasDataLayout, TargetDataLayout};
2112
use stable_mir::CrateDef;
2213
use stable_mir::mir::mono::MonoItem;
2314
use stable_mir::ty::{FnDef, RigidTy, Span as SpanStable, Ty, TyKind};
@@ -155,80 +146,6 @@ impl SourceLocation {
155146
}
156147
}
157148

158-
/// Get the FnAbi of a given instance with no extra variadic arguments.
159-
/// TODO: Get rid of this. Use instance.fn_abi() instead.
160-
/// <https://github.com/model-checking/kani/issues/1365>
161-
pub fn fn_abi<'tcx>(
162-
tcx: TyCtxt<'tcx>,
163-
instance: InstanceInternal<'tcx>,
164-
) -> &'tcx FnAbi<'tcx, TyInternal<'tcx>> {
165-
let helper = CompilerHelpers { tcx };
166-
helper.fn_abi_of_instance(instance, ty::List::empty())
167-
}
168-
169-
struct CompilerHelpers<'tcx> {
170-
tcx: TyCtxt<'tcx>,
171-
}
172-
173-
impl<'tcx> HasParamEnv<'tcx> for CompilerHelpers<'tcx> {
174-
fn param_env(&self) -> ty::ParamEnv<'tcx> {
175-
ty::ParamEnv::reveal_all()
176-
}
177-
}
178-
179-
impl<'tcx> HasTyCtxt<'tcx> for CompilerHelpers<'tcx> {
180-
fn tcx(&self) -> TyCtxt<'tcx> {
181-
self.tcx
182-
}
183-
}
184-
185-
impl HasDataLayout for CompilerHelpers<'_> {
186-
fn data_layout(&self) -> &TargetDataLayout {
187-
self.tcx.data_layout()
188-
}
189-
}
190-
191-
impl<'tcx> LayoutOfHelpers<'tcx> for CompilerHelpers<'tcx> {
192-
type LayoutOfResult = TyAndLayout<'tcx>;
193-
194-
#[inline]
195-
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: TyInternal<'tcx>) -> ! {
196-
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
197-
}
198-
}
199-
200-
/// Implement error handling for extracting function ABI information.
201-
impl<'tcx> FnAbiOfHelpers<'tcx> for CompilerHelpers<'tcx> {
202-
type FnAbiOfResult = &'tcx FnAbi<'tcx, TyInternal<'tcx>>;
203-
204-
#[inline]
205-
fn handle_fn_abi_err(
206-
&self,
207-
err: FnAbiError<'tcx>,
208-
span: Span,
209-
fn_abi_request: FnAbiRequest<'tcx>,
210-
) -> ! {
211-
if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err {
212-
self.tcx.dcx().emit_fatal(respan(span, err))
213-
} else {
214-
match fn_abi_request {
215-
FnAbiRequest::OfFnPtr { sig, extra_args } => {
216-
span_bug!(
217-
span,
218-
"Error: {err:?}\n while running `fn_abi_of_fn_ptr. ({sig}, {extra_args:?})`",
219-
);
220-
}
221-
FnAbiRequest::OfInstance { instance, extra_args } => {
222-
span_bug!(
223-
span,
224-
"Error: {err:?}\n while running `fn_abi_of_instance. ({instance}, {extra_args:?})`",
225-
);
226-
}
227-
}
228-
}
229-
}
230-
}
231-
232149
/// Try to convert an internal `DefId` to a `FnDef`.
233150
pub fn stable_fn_def(tcx: TyCtxt, def_id: InternalDefId) -> Option<FnDef> {
234151
if let TyKind::RigidTy(RigidTy::FnDef(def, _)) =

0 commit comments

Comments
 (0)