Skip to content

Commit b7ece82

Browse files
authored
Clean up some code before migrating rvalue (rust-lang#2928)
Clean up old unreachable check due to missing component which has been a long time by the MirLinker. Also remove an old TODO code to add the def name to virtual calls. This has regressed before, so the risk of regressing doesn't seem worth for debug purpose.
1 parent abd3690 commit b7ece82

File tree

5 files changed

+12
-55
lines changed

5 files changed

+12
-55
lines changed

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

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::kani_middle::coercion::{
1111
};
1212
use crate::unwrap_or_return_codegen_unimplemented;
1313
use cbmc::goto_program::{
14-
arithmetic_overflow_result_type, BinaryOperator, Expr, Location, Stmt, Symbol, Type,
14+
arithmetic_overflow_result_type, BinaryOperator, Expr, Location, Stmt, Type,
1515
ARITH_OVERFLOW_OVERFLOWED_FIELD, ARITH_OVERFLOW_RESULT_FIELD,
1616
};
1717
use cbmc::MachineModel;
@@ -1169,7 +1169,7 @@ impl<'tcx> GotocCtx<'tcx> {
11691169
idx: usize,
11701170
) -> Expr {
11711171
debug!(?instance, typ=?t, %idx, "codegen_vtable_method_field");
1172-
let vtable_field_name = self.vtable_field_name(instance.def_id(), idx);
1172+
let vtable_field_name = self.vtable_field_name(idx);
11731173
let vtable_type = Type::struct_tag(self.vtable_name(t));
11741174
let field_type =
11751175
vtable_type.lookup_field_type(vtable_field_name, &self.symbol_table).unwrap();
@@ -1232,34 +1232,10 @@ impl<'tcx> GotocCtx<'tcx> {
12321232
.address_of()
12331233
.cast_to(trait_fn_ty)
12341234
} else {
1235-
// We skip an entire submodule of the standard library, so drop is missing
1236-
// for it. Build and insert a function that just calls an unimplemented block
1237-
// to maintain soundness.
1238-
let drop_sym_name = format!("drop_unimplemented_{}", self.symbol_name(drop_instance));
1239-
let pretty_name =
1240-
format!("drop_unimplemented<{}>", self.readable_instance_name(drop_instance));
1241-
let drop_sym = self.ensure(&drop_sym_name, |ctx, name| {
1242-
// Function body
1243-
let unimplemented = ctx.codegen_unimplemented_stmt(
1244-
format!("drop_in_place for {drop_instance}").as_str(),
1245-
Location::none(),
1246-
"https://github.com/model-checking/kani/issues/281",
1247-
);
1248-
1249-
// Declare symbol for the single, self parameter
1250-
let param_typ = ctx.codegen_ty(trait_ty).to_pointer();
1251-
let param_sym = ctx.gen_function_parameter(0, &drop_sym_name, param_typ);
1252-
1253-
// Build and insert the function itself
1254-
Symbol::function(
1255-
name,
1256-
Type::code(vec![param_sym.to_function_parameter()], Type::empty()),
1257-
Some(Stmt::block(vec![unimplemented], Location::none())),
1258-
pretty_name,
1259-
Location::none(),
1260-
)
1261-
});
1262-
drop_sym.to_expr().address_of().cast_to(trait_fn_ty)
1235+
unreachable!(
1236+
"Missing drop implementation for {}",
1237+
self.readable_instance_name(drop_instance)
1238+
);
12631239
}
12641240
}
12651241

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use super::PropertyClass;
66
use crate::codegen_cprover_gotoc::{GotocCtx, VtableCtx};
77
use crate::unwrap_or_return_codegen_unimplemented_stmt;
88
use cbmc::goto_program::{Expr, Location, Stmt, Type};
9-
use rustc_hir::def_id::DefId;
109
use rustc_middle::mir;
1110
use rustc_middle::mir::{
1211
AssertKind, BasicBlock, NonDivergingIntrinsic, Operand, Place, Statement, StatementKind,
@@ -541,16 +540,9 @@ impl<'tcx> GotocCtx<'tcx> {
541540
return Stmt::goto(self.current_fn().find_label(&target.unwrap()), loc);
542541
}
543542
// Handle a virtual function call via a vtable lookup
544-
InstanceDef::Virtual(def_id, idx) => {
543+
InstanceDef::Virtual(_, idx) => {
545544
let self_ty = self.operand_ty(&args[0]);
546-
self.codegen_virtual_funcall(
547-
self_ty,
548-
def_id,
549-
idx,
550-
destination,
551-
&mut fargs,
552-
loc,
553-
)
545+
self.codegen_virtual_funcall(self_ty, idx, destination, &mut fargs, loc)
554546
}
555547
// Normal, non-virtual function calls
556548
InstanceDef::Item(..)
@@ -623,13 +615,12 @@ impl<'tcx> GotocCtx<'tcx> {
623615
fn codegen_virtual_funcall(
624616
&mut self,
625617
self_ty: Ty<'tcx>,
626-
def_id: DefId,
627618
idx: usize,
628619
place: &Place<'tcx>,
629620
fargs: &mut [Expr],
630621
loc: Location,
631622
) -> Vec<Stmt> {
632-
let vtable_field_name = self.vtable_field_name(def_id, idx);
623+
let vtable_field_name = self.vtable_field_name(idx);
633624
trace!(?self_ty, ?place, ?vtable_field_name, "codegen_virtual_funcall");
634625
debug!(?fargs, "codegen_virtual_funcall");
635626

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ impl<'tcx> GotocCtx<'tcx> {
481481
let fn_ptr = fn_ty.to_pointer();
482482

483483
// vtable field name, i.e., 3_vol (idx_method)
484-
let vtable_field_name = self.vtable_field_name(instance.def_id(), idx);
484+
let vtable_field_name = self.vtable_field_name(idx);
485485

486486
DatatypeComponent::field(vtable_field_name, fn_ptr)
487487
}

kani-compiler/src/codegen_cprover_gotoc/context/goto_ctx.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,6 @@ impl<'tcx> GotocCtx<'tcx> {
138138
self.gen_stack_variable(c, fname, "var", t, Location::none(), false)
139139
}
140140

141-
// Generate a Symbol Expression representing a function parameter from the MIR
142-
pub fn gen_function_parameter(&mut self, c: u64, fname: &str, t: Type) -> Symbol {
143-
self.gen_stack_variable(c, fname, "var", t, Location::none(), true)
144-
}
145-
146141
/// Given a counter `c` a function name `fname, and a prefix `prefix`, generates a new function local variable
147142
/// It is an error to reuse an existing `c`, `fname` `prefix` tuple.
148143
fn gen_stack_variable(

kani-compiler/src/codegen_cprover_gotoc/utils/names.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
66
use crate::codegen_cprover_gotoc::GotocCtx;
77
use cbmc::InternedString;
8-
use rustc_hir::def_id::DefId;
98
use rustc_hir::def_id::LOCAL_CRATE;
109
use rustc_middle::mir::mono::CodegenUnitNameBuilder;
1110
use rustc_middle::mir::Local;
@@ -83,12 +82,8 @@ impl<'tcx> GotocCtx<'tcx> {
8382
/// The name for the struct field on a vtable for a given function. Because generic
8483
/// functions can share the same name, we need to use the index of the entry in the
8584
/// vtable. This is the same index that will be passed in virtual function calls as
86-
/// InstanceDef::Virtual(def_id, idx). We could use solely the index as a key into
87-
/// the vtable struct, but we add the method name for debugging readability.
88-
/// Example: 3_vol
89-
pub fn vtable_field_name(&self, _def_id: DefId, idx: usize) -> InternedString {
90-
// format!("{}_{}", idx, with_no_trimmed_paths!(|| self.tcx.item_name(def_id)))
91-
// TODO: use def_id https://github.com/model-checking/kani/issues/364
85+
/// InstanceDef::Virtual(def_id, idx).
86+
pub fn vtable_field_name(&self, idx: usize) -> InternedString {
9287
idx.to_string().into()
9388
}
9489

0 commit comments

Comments
 (0)