8
8
//!
9
9
//! TODO: Use this inside kani library so that we dont have to maintain two copies of the same proc macro for arbitrary.
10
10
#[ macro_export]
11
+ #[ allow( clippy:: crate_in_macro_def) ]
11
12
macro_rules! generate_arbitrary {
12
13
( $core: path) => {
13
14
use core_path:: marker:: { PhantomData , PhantomPinned } ;
@@ -18,13 +19,7 @@ macro_rules! generate_arbitrary {
18
19
Self : Sized ,
19
20
{
20
21
fn any( ) -> Self ;
21
- #[ cfg( kani_sysroot) ]
22
- fn any_array<const MAX_ARRAY_LENGTH : usize >( ) -> [ Self ; MAX_ARRAY_LENGTH ]
23
- // the requirement defined in the where clause must appear on the `impl`'s method `any_array`
24
- // but also on the corresponding trait's method
25
- where
26
- [ ( ) ; core_path:: mem:: size_of:: <[ Self ; MAX_ARRAY_LENGTH ] >( ) ] : ,
27
- {
22
+ fn any_array<const MAX_ARRAY_LENGTH : usize >( ) -> [ Self ; MAX_ARRAY_LENGTH ] {
28
23
[ ( ) ; MAX_ARRAY_LENGTH ] . map( |_| Self :: any( ) )
29
24
}
30
25
}
@@ -36,22 +31,10 @@ macro_rules! generate_arbitrary {
36
31
#[ inline( always) ]
37
32
fn any( ) -> Self {
38
33
// This size_of call does not use generic_const_exprs feature. It's inside a macro, and Self isn't generic.
39
- unsafe { any_raw_internal :: < Self , { core_path :: mem :: size_of :: <Self > ( ) } >( ) }
34
+ unsafe { crate :: kani :: any_raw_internal :: <Self >( ) }
40
35
}
41
- // Disable this for standard library since we cannot enable generic constant expr.
42
- #[ cfg( kani_sysroot) ]
43
- fn any_array<const MAX_ARRAY_LENGTH : usize >( ) -> [ Self ; MAX_ARRAY_LENGTH ]
44
- where
45
- // `generic_const_exprs` requires all potential errors to be reflected in the signature/header.
46
- // We must repeat the expression in the header, to make sure that if the body can fail the header will also fail.
47
- [ ( ) ; { core_path:: mem:: size_of:: <[ $type; MAX_ARRAY_LENGTH ] >( ) } ] : ,
48
- {
49
- unsafe {
50
- any_raw_internal:: <
51
- [ Self ; MAX_ARRAY_LENGTH ] ,
52
- { core_path:: mem:: size_of:: <[ Self ; MAX_ARRAY_LENGTH ] >( ) } ,
53
- >( )
54
- }
36
+ fn any_array<const MAX_ARRAY_LENGTH : usize >( ) -> [ Self ; MAX_ARRAY_LENGTH ] {
37
+ unsafe { crate :: kani:: any_raw_internal:: <[ Self ; MAX_ARRAY_LENGTH ] >( ) }
55
38
}
56
39
}
57
40
} ;
@@ -134,6 +117,15 @@ macro_rules! generate_arbitrary {
134
117
}
135
118
}
136
119
120
+ impl <T , const N : usize > Arbitrary for [ T ; N ]
121
+ where
122
+ T : Arbitrary ,
123
+ {
124
+ fn any( ) -> Self {
125
+ T :: any_array:: <N >( )
126
+ }
127
+ }
128
+
137
129
impl <T > Arbitrary for Option <T >
138
130
where
139
131
T : Arbitrary ,
@@ -165,15 +157,33 @@ macro_rules! generate_arbitrary {
165
157
}
166
158
}
167
159
168
- #[ cfg( kani_sysroot) ]
169
- impl <T , const N : usize > Arbitrary for [ T ; N ]
170
- where
171
- T : Arbitrary ,
172
- [ ( ) ; core_path:: mem:: size_of:: <[ T ; N ] >( ) ] : ,
173
- {
160
+ arbitrary_tuple!( A ) ;
161
+ arbitrary_tuple!( A , B ) ;
162
+ arbitrary_tuple!( A , B , C ) ;
163
+ arbitrary_tuple!( A , B , C , D ) ;
164
+ arbitrary_tuple!( A , B , C , D , E ) ;
165
+ arbitrary_tuple!( A , B , C , D , E , F ) ;
166
+ arbitrary_tuple!( A , B , C , D , E , F , G ) ;
167
+ arbitrary_tuple!( A , B , C , D , E , F , G , H ) ;
168
+ arbitrary_tuple!( A , B , C , D , E , F , G , H , I ) ;
169
+ arbitrary_tuple!( A , B , C , D , E , F , G , H , I , J ) ;
170
+ arbitrary_tuple!( A , B , C , D , E , F , G , H , I , J , K ) ;
171
+ arbitrary_tuple!( A , B , C , D , E , F , G , H , I , J , K , L ) ;
172
+ } ;
173
+ }
174
+
175
+ /// This macro implements `kani::Arbitrary` on a tuple whose elements
176
+ /// already implement `kani::Arbitrary` by running `kani::any()` on
177
+ /// each index of the tuple.
178
+ #[ allow( clippy:: crate_in_macro_def) ]
179
+ #[ macro_export]
180
+ macro_rules! arbitrary_tuple {
181
+ ( $( $type: ident) ,* ) => {
182
+ impl <$( $type : Arbitrary ) ,* > Arbitrary for ( $( $type, ) * ) {
183
+ #[ inline( always) ]
174
184
fn any( ) -> Self {
175
- T :: any_array ( )
185
+ ( $ ( crate :: kani :: any :: <$type> ( ) , ) * )
176
186
}
177
187
}
178
- } ;
188
+ }
179
189
}
0 commit comments