Skip to content

Commit b8aa51b

Browse files
fix error for NonNull::get_unchecked_mut
1 parent 7c34873 commit b8aa51b

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

library/core/src/ptr/non_null.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ impl<T: ?Sized> NonNull<T> {
373373
#[rustc_const_stable(feature = "const_nonnull_as_ref", since = "1.73.0")]
374374
#[must_use]
375375
#[inline(always)]
376-
#[requires(ub_checks::can_dereference(self))] // Ensure pointer is valid for shared reference
376+
#[requires(ub_checks::can_dereference(self) == true)] // Ensure pointer is valid for shared reference
377377
#[ensures(|result: &&T| core::ptr::eq(*result, self.as_ptr()))] // Ensure returned reference matches pointer
378378
pub const unsafe fn as_ref<'a>(&self) -> &'a T {
379379
// SAFETY: the caller must guarantee that `self` meets all the
@@ -1903,4 +1903,19 @@ mod verify {
19031903
let _ = ptr.as_uninit_slice_mut();
19041904
}
19051905
}
1906+
1907+
#[kani::proof_for_contract(NonNull::get_unchecked_mut)]
1908+
pub fn non_null_check_get_unchecked_mut() {
1909+
const ARR_SIZE: usize = 100000;
1910+
let mut arr: [i32; ARR_SIZE] = kani::any();
1911+
let raw_ptr = arr.as_mut_ptr();
1912+
let ptr = NonNull::slice_from_raw_parts(
1913+
NonNull::new(raw_ptr).unwrap(),
1914+
ARR_SIZE,
1915+
);
1916+
let index = kani::any_where(|x| *x < ARR_SIZE - 1);
1917+
unsafe {
1918+
let _ = ptr.get_unchecked_mut(index..index + 1);
1919+
}
1920+
}
19061921
}

0 commit comments

Comments
 (0)