Skip to content

Commit

Permalink
contract and harness for new
Browse files Browse the repository at this point in the history
  • Loading branch information
QinyuanWu committed Sep 18, 2024
1 parent de1088c commit 5a4eaae
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::*;
use crate::cmp::Ordering;
use crate::marker::Unsize;
use crate::mem::{MaybeUninit, SizedTypeProperties};
Expand Down Expand Up @@ -227,6 +228,8 @@ impl<T: ?Sized> NonNull<T> {
#[stable(feature = "nonnull", since = "1.25.0")]
#[rustc_const_unstable(feature = "const_nonnull_new", issue = "93235")]
#[inline]
#[ensures(|result| result.is_some() == !ptr.is_null())]
#[ensures(|result| result.is_none() || result.expect("ptr is null!").as_ptr() == ptr)]
pub const fn new(ptr: *mut T) -> Option<Self> {
if !ptr.is_null() {
// SAFETY: The pointer is already checked and is not null
Expand Down Expand Up @@ -1790,4 +1793,16 @@ mod verify {
let _ = NonNull::new_unchecked(xptr as *mut i32);
}
}

// pub const unsafe fn new(ptr: *mut T) -> Option<Self>
#[kani::proof_for_contract(NonNull::new)]
pub fn non_null_check_new() {
let mut x : i32 = kani::any();
let xptr = &mut x;
unsafe {
let nonnull_ptr = NonNull::new(xptr as *mut i32);
let null_ptr = NonNull::<u32>::new(null_mut());
assert!(null_ptr.is_none());
}
}
}

0 comments on commit 5a4eaae

Please sign in to comment.