Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing function declaration issue #3862

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions kani-compiler/src/kani_middle/reachability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,22 @@ impl MirVisitor for MonoItemsFnCollector<'_, '_> {

self.super_terminator(terminator, location);
}

/// Collect any function definition that may occur as a type.
///
/// The codegen stage will require the definition to be available.
/// This is a conservative approach, since there are cases where the function is never
/// actually used, so we don't need the body.
///
/// Another alternative would be to lazily declare functions, but it would require a bigger
/// change to codegen.
fn visit_ty(&mut self, ty: &Ty, _: Location) {
if let TyKind::RigidTy(RigidTy::FnDef(def, args)) = ty.kind() {
let instance = Instance::resolve(def, &args).unwrap();
self.collect_instance(instance, true);
}
self.super_ty(ty);
}
}

fn extract_unsize_coercion(tcx: TyCtxt, orig_ty: Ty, dst_trait: Ty) -> (Ty, Ty) {
Expand Down
27 changes: 27 additions & 0 deletions tests/kani/CodegenMisc/missing_mut_fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Ensure Kani can codegen code with a pointer to a function that is never used
//! See <https://github.com/model-checking/kani/issues/3799> for more details.
fn foo<F: Fn()>(_func: &mut F) {}
fn foo_dyn(_func: &mut dyn Fn()) {}

#[kani::proof]
fn check_foo() {
fn f() {}

foo(&mut f);
}

#[kani::proof]
fn check_foo_dyn() {
fn f() {}

foo_dyn(&mut f);
}

#[kani::proof]
fn check_foo_unused() {
fn f() {}

let ptr = &mut f;
}
Loading