-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable contracts for const generic functions (#3726)
This PR adds dummy assignments of the input variables to local variables in contracts-replace-closure to avoid may-drop checks in const generic functions. Resolves #3667 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
- Loading branch information
Showing
4 changed files
with
55 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
tests/expected/function-contract/const_generic_function.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
** 1 of | ||
Failed Checks: Check that *dst is assignable |
27 changes: 27 additions & 0 deletions
27
tests/expected/function-contract/const_generic_function.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// kani-flags: -Zfunction-contracts | ||
|
||
//! Check that Kani contract can be applied to a constant generic function. | ||
//! <https://github.com/model-checking/kani/issues/3667> | ||
|
||
struct Foo<T> { | ||
ptr: *const T, | ||
} | ||
|
||
impl<T: Sized> Foo<T> { | ||
#[kani::requires(true)] | ||
pub const unsafe fn bar(self, v: T) | ||
where | ||
T: Sized, | ||
{ | ||
unsafe { core::ptr::write(self.ptr as *mut T, v) }; | ||
} | ||
} | ||
|
||
#[kani::proof_for_contract(Foo::bar)] | ||
fn check_const_generic_function() { | ||
let x: u8 = kani::any(); | ||
let foo: Foo<u8> = Foo { ptr: &x }; | ||
unsafe { foo.bar(kani::any::<u8>()) }; | ||
} |