Skip to content

Commit 0f7388c

Browse files
authored
Rollup merge of #103329 - saethlin:nonnull-precondition, r=thomcc
Add a forgotten check for NonNull::new_unchecked's precondition Looks like I forgot this function a while ago in rust-lang/rust#92686 r? ```@thomcc```
2 parents 72ad968 + 74a97e8 commit 0f7388c

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

core/src/ptr/non_null.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::cmp::Ordering;
22
use crate::convert::From;
33
use crate::fmt;
44
use crate::hash;
5+
use crate::intrinsics::assert_unsafe_precondition;
56
use crate::marker::Unsize;
67
use crate::mem::{self, MaybeUninit};
78
use crate::num::NonZeroUsize;
@@ -195,7 +196,10 @@ impl<T: ?Sized> NonNull<T> {
195196
#[inline]
196197
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
197198
// SAFETY: the caller must guarantee that `ptr` is non-null.
198-
unsafe { NonNull { pointer: ptr as _ } }
199+
unsafe {
200+
assert_unsafe_precondition!([T: ?Sized](ptr: *mut T) => !ptr.is_null());
201+
NonNull { pointer: ptr as _ }
202+
}
199203
}
200204

201205
/// Creates a new `NonNull` if `ptr` is non-null.

0 commit comments

Comments
 (0)