Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,10 @@ impl<T: ?Sized> Box<T> {
#[stable(feature = "box_vec_non_null", since = "CURRENT_RUSTC_VERSION")]
#[inline]
pub fn into_non_null(b: Self) -> NonNull<T> {
// As of August 2026, we cannot utilize `Box::leak`
// because whether or not you can reconstruct the `Box`
// later using `Box::from_raw` or `Box::from_non_null` is
// an open question.
// SAFETY: `Box` is guaranteed to be non-null.
unsafe { NonNull::new_unchecked(Self::into_raw(b)) }
}
Expand Down
6 changes: 2 additions & 4 deletions library/alloc/src/io/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ fn custom_owner_from_box(
drop(unsafe { Box::from_raw(ptr) })
}

// SAFETY: the pointer returned by Box::into_raw is non-null.
let error = unsafe { core::ptr::NonNull::new_unchecked(Box::into_raw(error)) };
let error = Box::into_non_null(error);

// SAFETY:
// * `error` is valid up to a static lifetime, and owns its pointee.
Expand All @@ -269,8 +268,7 @@ fn custom_owner_from_box(
// and will be stored in a `CustomOwner`.
let custom = unsafe { Custom::from_raw(kind, error, drop_box_raw, drop_box_raw) };

// SAFETY: the pointer returned by Box::into_raw is non-null.
let custom = unsafe { core::ptr::NonNull::new_unchecked(Box::into_raw(Box::new(custom))) };
let custom = Box::into_non_null(Box::new(custom));

// SAFETY: the `outer_drop` provided to `custom` is valid for itself.
unsafe { CustomOwner::from_raw(custom) }
Expand Down
8 changes: 8 additions & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ impl<T: Sized> NonNull<T> {
impl<T: PointeeSized> NonNull<T> {
/// Creates a new `NonNull`.
///
/// Note that if you have an `&mut`, you can use the safe [`from_mut`] instead.
///
/// [`from_mut`]: NonNull::from_mut
///
/// # Safety
///
/// `ptr` must be non-null.
Expand Down Expand Up @@ -246,6 +250,10 @@ impl<T: PointeeSized> NonNull<T> {

/// Creates a new `NonNull` if `ptr` is non-null.
///
/// Note that if you have an `&mut`, you can use [`from_mut`] instead to avoid the `Option`.
///
/// [`from_mut`]: NonNull::from_mut
///
/// # Panics during const evaluation
///
/// This method will panic during const evaluation if the pointer cannot be
Expand Down
3 changes: 1 addition & 2 deletions library/std/src/sys/thread/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ impl Thread {
}
}

// Safety: `Box::into_raw` returns a non-null pointer
let p_inner = unsafe { NonNull::new_unchecked(Box::into_raw(inner)) };
let p_inner = Box::into_non_null(inner);

let new_task = ItronError::err_if_negative(unsafe {
abi::acre_tsk(&abi::T_CTSK {
Expand Down
Loading