Skip to content

Commit 35b3653

Browse files
Implement Arbitrary for PhantomData and PhantomPinned (rust-lang#2225)
Co-authored-by: Celina G. Val <[email protected]>
1 parent 375f821 commit 35b3653

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

library/kani/src/arbitrary.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
//! This module introduces the Arbitrary trait as well as implementation for primitive types and
55
//! other std containers.
6-
use std::num::*;
6+
use std::{
7+
marker::{PhantomData, PhantomPinned},
8+
num::*,
9+
};
710

811
/// This trait should be used to generate symbolic variables that represent any valid value of
912
/// its type.
@@ -120,3 +123,15 @@ where
120123
if bool::any() { Ok(T::any()) } else { Err(E::any()) }
121124
}
122125
}
126+
127+
impl<T: ?Sized> Arbitrary for std::marker::PhantomData<T> {
128+
fn any() -> Self {
129+
PhantomData
130+
}
131+
}
132+
133+
impl Arbitrary for std::marker::PhantomPinned {
134+
fn any() -> Self {
135+
PhantomPinned
136+
}
137+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VERIFICATION:- SUCCESSFUL
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
//! Check that Kani can automatically derive `Arbitrary` on a struct that has
5+
//! `std::marker::PhantomData`
6+
7+
#[derive(kani::Arbitrary)]
8+
struct Foo<T> {
9+
x: i32,
10+
_f: std::marker::PhantomData<T>,
11+
}
12+
13+
impl<T> Foo<T> {
14+
fn new(v: i32) -> Self {
15+
Self { x: v, _f: std::marker::PhantomData }
16+
}
17+
}
18+
19+
#[kani::proof]
20+
fn main() {
21+
let x = kani::any();
22+
let f: Foo<u16> = Foo::new(x);
23+
assert_eq!(f.x, x);
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VERIFICATION:- SUCCESSFUL
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
//! Check that Kani can automatically derive `Arbitrary` on a struct that has
5+
//! `std::marker::PhantomPinned`
6+
7+
#[derive(kani::Arbitrary)]
8+
struct Foo {
9+
x: i32,
10+
_f: std::marker::PhantomPinned,
11+
}
12+
13+
impl Foo {
14+
fn new(v: i32) -> Self {
15+
Self { x: v, _f: std::marker::PhantomPinned }
16+
}
17+
}
18+
19+
#[kani::proof]
20+
fn check_arbitrary_phantom_pinned() {
21+
let x = kani::any();
22+
let f: Foo = Foo::new(x);
23+
assert_eq!(f.x, x);
24+
}

0 commit comments

Comments
 (0)