Skip to content

Commit 1f0828b

Browse files
committed
refactor is_anon_static into function in reachability.rs
1 parent 6ae6f9d commit 1f0828b

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
use crate::codegen_cprover_gotoc::GotocCtx;
44
use crate::codegen_cprover_gotoc::utils::slice_fat_ptr;
5+
use crate::kani_middle::is_anon_static;
56
use crate::unwrap_or_return_codegen_unimplemented;
67
use cbmc::goto_program::{DatatypeComponent, Expr, ExprValue, Location, Symbol, Type};
78
use rustc_middle::ty::Const as ConstInternal;
@@ -373,12 +374,7 @@ impl<'tcx> GotocCtx<'tcx> {
373374
self.codegen_func_expr(instance, loc).address_of()
374375
}
375376
GlobalAlloc::Static(def) => {
376-
let int_def_id = rustc_internal::internal(self.tcx, def.def_id());
377-
let anonymous = match self.tcx.def_kind(int_def_id) {
378-
rustc_hir::def::DefKind::Static { nested, .. } => nested,
379-
_ => false,
380-
};
381-
if anonymous {
377+
if is_anon_static(self.tcx, def.def_id()) {
382378
let alloc = def.eval_initializer().unwrap();
383379
let name = format!("{}::{alloc_id:?}", self.full_crate_name());
384380
self.codegen_nested_static_allocation(&alloc, Some(name), loc)

kani-compiler/src/kani_middle/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use crate::kani_queries::QueryDb;
99
use rustc_hir::{def::DefKind, def_id::DefId as InternalDefId, def_id::LOCAL_CRATE};
1010
use rustc_middle::ty::TyCtxt;
1111
use rustc_smir::rustc_internal;
12-
use stable_mir::CrateDef;
1312
use stable_mir::mir::mono::MonoItem;
1413
use stable_mir::ty::{FnDef, RigidTy, Span as SpanStable, Ty, TyKind};
1514
use stable_mir::visitor::{Visitable, Visitor as TyVisitor};
15+
use stable_mir::{CrateDef, DefId};
1616
use std::ops::ControlFlow;
1717

1818
use self::attributes::KaniAttributes;
@@ -146,6 +146,15 @@ impl SourceLocation {
146146
}
147147
}
148148

149+
/// Return whether `def_id` refers to a nested static allocation.
150+
pub fn is_anon_static(tcx: TyCtxt, def_id: DefId) -> bool {
151+
let int_def_id = rustc_internal::internal(tcx, def_id);
152+
match tcx.def_kind(int_def_id) {
153+
rustc_hir::def::DefKind::Static { nested, .. } => nested,
154+
_ => false,
155+
}
156+
}
157+
149158
/// Try to convert an internal `DefId` to a `FnDef`.
150159
pub fn stable_fn_def(tcx: TyCtxt, def_id: InternalDefId) -> Option<FnDef> {
151160
if let TyKind::RigidTy(RigidTy::FnDef(def, _)) =

kani-compiler/src/kani_middle/reachability.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use std::{
4242

4343
use crate::kani_middle::coercion;
4444
use crate::kani_middle::coercion::CoercionBase;
45+
use crate::kani_middle::is_anon_static;
4546
use crate::kani_middle::transform::BodyTransformation;
4647

4748
/// Collect all reachable items starting from the given starting points.
@@ -220,12 +221,7 @@ impl<'tcx, 'a> MonoItemsCollector<'tcx, 'a> {
220221
let mut next_items = vec![];
221222

222223
// Collect drop function, unless it's an anonymous static.
223-
let int_def_id = rustc_internal::internal(self.tcx, def.def_id());
224-
let anonymous = match self.tcx.def_kind(int_def_id) {
225-
rustc_hir::def::DefKind::Static { nested, .. } => nested,
226-
_ => false,
227-
};
228-
if !anonymous {
224+
if !is_anon_static(self.tcx, def.def_id()) {
229225
let static_ty = def.ty();
230226
let instance = Instance::resolve_drop_in_place(static_ty);
231227
next_items.push(CollectedItem {
@@ -535,12 +531,7 @@ fn collect_alloc_items(tcx: TyCtxt, alloc_id: AllocId) -> Vec<MonoItem> {
535531
let mut items = vec![];
536532
match GlobalAlloc::from(alloc_id) {
537533
GlobalAlloc::Static(def) => {
538-
let int_def_id = rustc_internal::internal(tcx, def.def_id());
539-
let anonymous = match tcx.def_kind(int_def_id) {
540-
rustc_hir::def::DefKind::Static { nested, .. } => nested,
541-
_ => false,
542-
};
543-
if anonymous {
534+
if is_anon_static(tcx, def.def_id()) {
544535
let alloc = def.eval_initializer().unwrap();
545536
items.extend(
546537
alloc

0 commit comments

Comments
 (0)