@@ -503,10 +503,15 @@ impl<T: ?Sized> NonNull<T> {
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
505
// TODO: add a require to check whether two pointer points to the same allocated object with `same_allocation`
506
- #[ ensures(
507
- |result: & isize |
508
- * result == ( self . as_ptr( ) as * const u8 ) . offset_from( origin. as_ptr( ) as * const u8 )
509
- ) ]
506
+ #[ ensures( |result: & Self | {
507
+ if ( count >= 0 ) {
508
+ let offset_ptr = self . as_ptr( ) . byte_add( count as usize ) as * mut T ;
509
+ result. as_ptr( ) == offset_ptr
510
+ } else {
511
+ let offset_ptr = self . as_ptr( ) . byte_sub( -count as usize ) as * mut T ;
512
+ result. as_ptr( ) == offset_ptr
513
+ }
514
+ } ) ]
510
515
pub const unsafe fn byte_offset ( self , count : isize ) -> Self {
511
516
// SAFETY: the caller must uphold the safety contract for `offset` and `byte_offset` has
512
517
// the same safety contract.
@@ -1800,6 +1805,7 @@ impl<T: ?Sized> From<&T> for NonNull<T> {
1800
1805
mod verify {
1801
1806
use super :: * ;
1802
1807
use crate :: ptr:: null_mut;
1808
+ use crate :: mem;
1803
1809
1804
1810
// pub const unsafe fn new_unchecked(ptr: *mut T) -> Self
1805
1811
#[ kani:: proof_for_contract( NonNull :: new_unchecked) ]
@@ -1864,8 +1870,8 @@ mod verify {
1864
1870
let arr: [ i32 ; ARR_SIZE ] = kani:: any ( ) ;
1865
1871
1866
1872
// Randomly generate offsets for the pointers
1867
- let offset = kani:: any_where ( |x| * x < ARR_SIZE ) ;
1868
- let origin_offset = kani:: any_where ( |x| * x < ARR_SIZE ) ;
1873
+ let offset = kani:: any_where ( |x| * x <= ARR_SIZE ) ;
1874
+ let origin_offset = kani:: any_where ( |x| * x <= ARR_SIZE ) ;
1869
1875
1870
1876
let raw_ptr: * mut i32 = arr. as_ptr ( ) as * mut i32 ;
1871
1877
let origin_ptr: * mut i32 = arr. as_ptr ( ) as * mut i32 ;
0 commit comments