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

Implement kani::Arbitrary for Box<T> #2404

Merged
merged 5 commits into from
Apr 25, 2023
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
9 changes: 9 additions & 0 deletions library/kani/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,12 @@ impl Arbitrary for std::marker::PhantomPinned {
PhantomPinned
}
}

impl<T> Arbitrary for std::boxed::Box<T>
where
T: Arbitrary,
{
fn any() -> Self {
Box::new(T::any())
adpaco-aws marked this conversation as resolved.
Show resolved Hide resolved
}
}
11 changes: 11 additions & 0 deletions tests/expected/derive-arbitrary/box/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Status: SATISFIED\
Description: "cover condition: *foo.boxed == i32::MIN"
Status: SATISFIED\
Description: "cover condition: *foo.boxed == 0"
Status: SATISFIED\
Description: "cover condition: *foo.boxed == i32::MAX"
Status: UNSATISFIABLE\
Description: "cover condition: *foo.boxed < i32::MIN"
Status: UNSATISFIABLE\
Description: "cover condition: *foo.boxed > i32::MAX"
VERIFICATION:- SUCCESSFUL
20 changes: 20 additions & 0 deletions tests/expected/derive-arbitrary/box/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Check that Kani can automatically derive `Arbitrary` on a struct with a
//! member of type `Box<T>`

#[derive(kani::Arbitrary)]
struct Foo<T> {
boxed: Box<T>,
}

#[kani::proof]
fn main() {
let foo: Foo<i32> = kani::any();
kani::cover!(*foo.boxed == i32::MIN);
kani::cover!(*foo.boxed == 0);
kani::cover!(*foo.boxed == i32::MAX);
kani::cover!(*foo.boxed < i32::MIN); // <-- this condition should be `UNSATISFIABLE`
kani::cover!(*foo.boxed > i32::MAX); // <-- this condition should be `UNSATISFIABLE`
}