diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2022-10-22 16:28:08 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-22 16:28:08 +0530 |
| commit | 3f49f9506f124097b9f773d08248432eca625f1c (patch) | |
| tree | 6c5c41e25c56ea0f7429c4c7bbb06fbcac8e0cd6 /library/core | |
| parent | 141478b40f834a58c91749a547c734b67f4fe5e3 (diff) | |
| parent | 9b6791078a580acdd05246f00f900cbc079bb95e (diff) | |
| download | rust-3f49f9506f124097b9f773d08248432eca625f1c.tar.gz rust-3f49f9506f124097b9f773d08248432eca625f1c.zip | |
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 https://github.com/rust-lang/rust/pull/92686 r? ```@thomcc```
Diffstat (limited to 'library/core')
| -rw-r--r-- | library/core/src/ptr/non_null.rs | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index f3ef094cbcc..7264d57ba6a 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -2,6 +2,7 @@ use crate::cmp::Ordering; use crate::convert::From; use crate::fmt; use crate::hash; +use crate::intrinsics::assert_unsafe_precondition; use crate::marker::Unsize; use crate::mem::{self, MaybeUninit}; use crate::num::NonZeroUsize; @@ -195,7 +196,10 @@ impl<T: ?Sized> NonNull<T> { #[inline] pub const unsafe fn new_unchecked(ptr: *mut T) -> Self { // SAFETY: the caller must guarantee that `ptr` is non-null. - unsafe { NonNull { pointer: ptr as _ } } + unsafe { + assert_unsafe_precondition!([T: ?Sized](ptr: *mut T) => !ptr.is_null()); + NonNull { pointer: ptr as _ } + } } /// Creates a new `NonNull` if `ptr` is non-null. |
