Skip to content

Commit 7603984

Browse files
committed
Add contract to from_mut_unchecked
1 parent fcfd000 commit 7603984

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

library/core/src/num/nonzero.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,12 @@ where
452452
#[unstable(feature = "nonzero_from_mut", issue = "106290")]
453453
#[must_use]
454454
#[inline]
455+
#[requires({
456+
let size = core::mem::size_of::<T>();
457+
let ptr = &n as *const T as *const u8;
458+
let slice = unsafe { core::slice::from_raw_parts(ptr, size) };
459+
!slice.iter().all(|&byte| byte == 0)
460+
})]
455461
pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self {
456462
match Self::from_mut(n) {
457463
Some(n) => n,
@@ -2425,10 +2431,9 @@ mod verify {
24252431

24262432
macro_rules! nonzero_check_from_mut_unchecked {
24272433
($t:ty, $nonzero_type:ty, $harness_name:ident) => {
2428-
#[kani::proof]
2434+
#[kani::proof_for_contract(NonZero::from_mut_unchecked)]
24292435
pub fn $harness_name() {
24302436
let mut x: $t = kani::any();
2431-
kani::assume(x != 0);
24322437
unsafe {
24332438
<$nonzero_type>::from_mut_unchecked(&mut x);
24342439
}

0 commit comments

Comments
 (0)