@@ -2787,7 +2787,10 @@ impl<T> [T] {
27872787 // returns Equal. We want the number of loop iterations to depend *only*
27882788 // on the size of the input slice so that the CPU can reliably predict
27892789 // the loop count.
2790- #[ cfg_attr( kani, kani:: loop_invariant( size <= self . len( ) && size >= 1 && base <= self . len( ) && size+base <= self . len( ) ) ) ]
2790+ #[ safety:: loop_invariant( size <= self . len( )
2791+ && size >= 1
2792+ && base <= self . len( )
2793+ && size+base <= self . len( ) ) ]
27912794 while size > 1 {
27922795 let half = size / 2 ;
27932796 let mid = base + half;
@@ -4925,29 +4928,27 @@ impl<const N: usize> fmt::Display for GetManyMutError<N> {
49254928pub mod verify {
49264929 use super :: * ;
49274930
4928- // Copied from https://github.com/model-checking/kani/blob/main/library/kani/src/slice.rs
4929- // should be removed when these functions are moved to `kani_core`
4930- pub fn any_slice_of_array < T , const LENGTH : usize > ( arr : & [ T ; LENGTH ] ) -> & [ T ] {
4931- let ( from, to) = any_range :: < LENGTH > ( ) ;
4932- & arr[ from..to]
4933- }
4934-
4935- fn any_range < const LENGTH : usize > ( ) -> ( usize , usize ) {
4936- let from: usize = kani:: any ( ) ;
4937- let to: usize = kani:: any ( ) ;
4938- kani:: assume ( to <= LENGTH ) ;
4939- kani:: assume ( from <= to) ;
4940- ( from, to)
4941- }
4942-
49434931 #[ kani:: proof]
49444932 pub fn check_binary_search_by ( ) {
4945- const ARR_SIZE : usize = 1000 ;
4946- let x: [ u8 ; ARR_SIZE ] = kani:: any ( ) ;
4947- let xs = any_slice_of_array ( & x) ;
49484933 let key: u8 = kani:: any ( ) ;
4949- unsafe {
4950- xs. binary_search_by ( |p| p. cmp ( & key) ) ;
4934+ if kani:: any ( ) {
4935+ // TODO: ARR_SIZE can be `std::usize::MAX` with cbmc argument
4936+ // `--arrays-uf-always`
4937+ const ARR_SIZE : usize = 1000 ;
4938+ let x: [ u8 ; ARR_SIZE ] = kani:: any ( ) ;
4939+ let xs = kani:: slice:: any_slice_of_array ( & x) ;
4940+ unsafe {
4941+ xs. binary_search_by ( |p| p. cmp ( & key) ) ;
4942+ }
4943+ } else {
4944+ let ptr = kani:: any_where :: < usize , _ > ( |val| * val != 0 ) as * const u8 ;
4945+ kani:: assume ( ptr. is_aligned ( ) ) ;
4946+ unsafe {
4947+ assert_eq ! (
4948+ crate :: slice:: from_raw_parts( ptr, 0 ) . binary_search_by( |p| p. cmp( & key) ) ,
4949+ Err ( 0 )
4950+ ) ;
4951+ }
49514952 }
49524953 }
49534954}
0 commit comments