Skip to content

Commit 8d79ee5

Browse files
authored
Cleanup redundant logic (#2437)
There is no need for a different visitor for gathering unit tests.
1 parent e807fd7 commit 8d79ee5

File tree

2 files changed

+10
-57
lines changed

2 files changed

+10
-57
lines changed

kani-compiler/src/codegen_cprover_gotoc/compiler_interface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::kani_middle::check_crate_items;
1212
use crate::kani_middle::check_reachable_items;
1313
use crate::kani_middle::provide;
1414
use crate::kani_middle::reachability::{
15-
collect_reachable_items, filter_closures_in_const_crate_items, filter_crate_items,
15+
collect_reachable_items, filter_const_crate_items, filter_crate_items,
1616
};
1717
use cbmc::goto_program::Location;
1818
use cbmc::irep::goto_binary_serde::write_goto_binary_file;
@@ -408,7 +408,7 @@ fn collect_codegen_items<'tcx>(gcx: &GotocCtx<'tcx>) -> Vec<MonoItem<'tcx>> {
408408
ReachabilityType::Tests => {
409409
// We're iterating over crate items here, so what we have to codegen is the "test description" containing the
410410
// test closure that we want to execute
411-
let harnesses = filter_closures_in_const_crate_items(tcx, |_, def_id| {
411+
let harnesses = filter_const_crate_items(tcx, |_, def_id| {
412412
is_test_harness_description(gcx.tcx, def_id)
413413
});
414414
collect_reachable_items(tcx, &harnesses).into_iter().collect()

kani-compiler/src/kani_middle/reachability.rs

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,26 @@ where
9797
root_items.chain(impl_items).collect()
9898
}
9999

100-
/// Use a predicate to find `const` declarations, then extract all closures from those declarations
100+
/// Use a predicate to find `const` declarations, then extract all items reachable from them.
101101
///
102102
/// Probably only specifically useful with a predicate to find `TestDescAndFn` const declarations from
103103
/// tests and extract the closures from them.
104-
pub fn filter_closures_in_const_crate_items<F>(tcx: TyCtxt, mut predicate: F) -> Vec<MonoItem>
104+
pub fn filter_const_crate_items<F>(tcx: TyCtxt, mut predicate: F) -> Vec<MonoItem>
105105
where
106106
F: FnMut(TyCtxt, DefId) -> bool,
107107
{
108108
let mut roots = Vec::new();
109109
for hir_id in tcx.hir_crate_items(()).items() {
110110
let def_id = hir_id.owner_id.def_id.to_def_id();
111-
if predicate(tcx, def_id) {
112-
// The predicate should only ever apply to monomorphic items
111+
let def_kind = tcx.def_kind(def_id);
112+
if matches!(def_kind, DefKind::Const) && predicate(tcx, def_id) {
113113
let instance = Instance::mono(tcx, def_id);
114114
let body = tcx.instance_mir(InstanceDef::Item(WithOptConstParam::unknown(def_id)));
115-
let mut extrator =
116-
ConstMonoItemExtractor { tcx, body, instance, collected: FxHashSet::default() };
117-
extrator.visit_body(body);
115+
let mut collector =
116+
MonoItemsFnCollector { tcx, body, instance, collected: FxHashSet::default() };
117+
collector.visit_body(body);
118118

119-
roots.extend(extrator.collected);
119+
roots.extend(collector.collected);
120120
}
121121
}
122122
roots
@@ -602,53 +602,6 @@ fn collect_alloc_items(tcx: TyCtxt, alloc_id: AllocId) -> Vec<MonoItem> {
602602
items
603603
}
604604

605-
/// This MIR Visitor is intended for one specific purpose:
606-
/// Find the closure that exist inside a top-level const declaration generated by
607-
/// test declarations. This allows us to treat this closure instance as a root for
608-
/// the reachability analysis.
609-
///
610-
/// Entry into this visitor will be via `visit_body`
611-
struct ConstMonoItemExtractor<'a, 'tcx> {
612-
tcx: TyCtxt<'tcx>,
613-
collected: FxHashSet<MonoItem<'tcx>>,
614-
instance: Instance<'tcx>,
615-
body: &'a Body<'tcx>,
616-
}
617-
618-
impl<'a, 'tcx> MirVisitor<'tcx> for ConstMonoItemExtractor<'a, 'tcx> {
619-
#[allow(clippy::single_match)]
620-
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
621-
trace!(rvalue=?*rvalue, "visit_rvalue");
622-
623-
match *rvalue {
624-
Rvalue::Cast(CastKind::Pointer(PointerCast::ClosureFnPointer(_)), ref operand, _) => {
625-
let source_ty = operand.ty(self.body, self.tcx);
626-
let source_ty = self.instance.subst_mir_and_normalize_erasing_regions(
627-
self.tcx,
628-
ParamEnv::reveal_all(),
629-
source_ty,
630-
);
631-
match *source_ty.kind() {
632-
Closure(def_id, substs) => {
633-
let instance = Instance::resolve_closure(
634-
self.tcx,
635-
def_id,
636-
substs,
637-
ClosureKind::FnOnce,
638-
)
639-
.expect("failed to normalize and resolve closure during codegen");
640-
self.collected.insert(MonoItem::Fn(instance.polymorphize(self.tcx)));
641-
}
642-
_ => unreachable!("Unexpected type: {:?}", source_ty),
643-
}
644-
}
645-
_ => { /* not interesting */ }
646-
}
647-
648-
self.super_rvalue(rvalue, location);
649-
}
650-
}
651-
652605
#[cfg(debug_assertions)]
653606
mod debug {
654607
#![allow(dead_code)]

0 commit comments

Comments
 (0)