@@ -502,8 +502,14 @@ impl<T: ?Sized> NonNull<T> {
502
502
#[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
503
503
#[ stable( feature = "non_null_convenience" , since = "1.80.0" ) ]
504
504
#[ rustc_const_stable( feature = "non_null_convenience" , since = "1.80.0" ) ]
505
- // TODO: add a require to check whether two pointer points to the same allocated object with `same_allocation`
506
- #[ ensures( |result: & Self | {
505
+ #[ kani:: requires(
506
+ if ( count >= 0 ) {
507
+ kani:: mem:: same_allocation( self . as_ptr( ) as * const ( ) , self . as_ptr( ) . byte_add( count as usize ) as * const ( ) )
508
+ } else {
509
+ kani:: mem:: same_allocation( self . as_ptr( ) as * const ( ) , self . as_ptr( ) . byte_sub( -count as usize ) as * const ( ) )
510
+ }
511
+ ) ]
512
+ #[ kani:: ensures( |result: & Self | {
507
513
if ( count >= 0 ) {
508
514
let offset_ptr = self . as_ptr( ) . byte_add( count as usize ) as * mut T ;
509
515
result. as_ptr( ) == offset_ptr
@@ -589,8 +595,11 @@ impl<T: ?Sized> NonNull<T> {
589
595
#[ rustc_allow_const_fn_unstable( set_ptr_value) ]
590
596
#[ stable( feature = "non_null_convenience" , since = "1.80.0" ) ]
591
597
#[ rustc_const_stable( feature = "non_null_convenience" , since = "1.80.0" ) ]
592
- // TODO: add a require to check whether two pointer points to the same allocated object with `same_allocation`
593
- #[ ensures(
598
+ #[ kani:: requires( kani:: mem:: same_allocation(
599
+ self . as_ptr( ) as * const ( ) ,
600
+ ( ( self . as_ptr( ) as * const ( ) as usize ) + count) as * const ( )
601
+ ) ) ]
602
+ #[ kani:: ensures(
594
603
|result: & NonNull <T >|
595
604
( result. as_ptr( ) as * const ( ) as usize ) == ( ( self . as_ptr( ) as * const ( ) as usize ) + count)
596
605
) ]
@@ -798,8 +807,8 @@ impl<T: ?Sized> NonNull<T> {
798
807
#[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
799
808
#[ stable( feature = "non_null_convenience" , since = "1.80.0" ) ]
800
809
#[ rustc_const_stable( feature = "non_null_convenience" , since = "1.80.0" ) ]
801
- // TODO: add a require to check whether two pointer points to the same allocated object with `same_allocation`
802
- #[ ensures(
810
+ # [ kani :: requires ( kani :: mem :: same_allocation ( self . as_ptr ( ) as * const ( ) , origin . as_ptr ( ) as * const ( ) ) ) ]
811
+ #[ kani :: ensures(
803
812
|result: & isize |
804
813
* result == ( self . as_ptr( ) as * const u8 ) . offset_from( origin. as_ptr( ) as * const u8 )
805
814
) ]
@@ -1837,7 +1846,6 @@ mod verify {
1837
1846
kani:: assume ( count < usize:: MAX ) ;
1838
1847
kani:: assume ( count. checked_mul ( mem:: size_of :: < i32 > ( ) ) . is_some ( ) ) ;
1839
1848
kani:: assume ( count * mem:: size_of :: < i32 > ( ) <= ( isize:: MAX as usize ) ) ;
1840
- kani:: assume ( count < ARR_SIZE - offset) ;
1841
1849
1842
1850
unsafe {
1843
1851
let result = ptr. byte_add ( count) ;
@@ -1857,8 +1865,6 @@ mod verify {
1857
1865
kani:: assume ( count <= isize:: MAX ) ;
1858
1866
kani:: assume ( count. checked_mul ( mem:: size_of :: < i32 > ( ) as isize ) . is_some ( ) ) ;
1859
1867
kani:: assume ( count * ( mem:: size_of :: < i32 > ( ) as isize ) <= ( isize:: MAX as isize ) ) ;
1860
- kani:: assume ( ( offset as isize + count) < ( ARR_SIZE as isize ) ) ;
1861
-
1862
1868
unsafe {
1863
1869
let result = ptr. byte_offset ( count) ;
1864
1870
}
0 commit comments