1
Fork 0

Rollup merge of #135347 - samueltardieu:push-qvyxtxsqyxyr, r=jhpratt

Use `NonNull::without_provenance` within the standard library

This API removes the need for several `unsafe` blocks, and leads to clearer code. It uses feature `nonnull_provenance` (#135243).

Close #135343
This commit is contained in:
Jacob Pratt 2025-01-11 01:55:09 -05:00 committed by GitHub
commit 46222ce6f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 13 additions and 29 deletions

View file

@ -126,6 +126,7 @@
#![feature(local_waker)]
#![feature(maybe_uninit_slice)]
#![feature(maybe_uninit_uninit_array_transpose)]
#![feature(nonnull_provenance)]
#![feature(panic_internals)]
#![feature(pattern)]
#![feature(pin_coerce_unsized_trait)]

View file

@ -252,6 +252,7 @@ use core::intrinsics::abort;
use core::iter;
use core::marker::{PhantomData, Unsize};
use core::mem::{self, ManuallyDrop, align_of_val_raw};
use core::num::NonZeroUsize;
use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, LegacyReceiver};
use core::panic::{RefUnwindSafe, UnwindSafe};
#[cfg(not(no_global_oom_handling))]
@ -3027,12 +3028,7 @@ impl<T> Weak<T> {
#[rustc_const_stable(feature = "const_weak_new", since = "1.73.0")]
#[must_use]
pub const fn new() -> Weak<T> {
Weak {
ptr: unsafe {
NonNull::new_unchecked(ptr::without_provenance_mut::<RcInner<T>>(usize::MAX))
},
alloc: Global,
}
Weak { ptr: NonNull::without_provenance(NonZeroUsize::MAX), alloc: Global }
}
}
@ -3054,12 +3050,7 @@ impl<T, A: Allocator> Weak<T, A> {
#[inline]
#[unstable(feature = "allocator_api", issue = "32838")]
pub fn new_in(alloc: A) -> Weak<T, A> {
Weak {
ptr: unsafe {
NonNull::new_unchecked(ptr::without_provenance_mut::<RcInner<T>>(usize::MAX))
},
alloc,
}
Weak { ptr: NonNull::without_provenance(NonZeroUsize::MAX), alloc }
}
}

View file

@ -18,6 +18,7 @@ use core::intrinsics::abort;
use core::iter;
use core::marker::{PhantomData, Unsize};
use core::mem::{self, ManuallyDrop, align_of_val_raw};
use core::num::NonZeroUsize;
use core::ops::{CoerceUnsized, Deref, DerefPure, DispatchFromDyn, LegacyReceiver};
use core::panic::{RefUnwindSafe, UnwindSafe};
use core::pin::{Pin, PinCoerceUnsized};
@ -2687,12 +2688,7 @@ impl<T> Weak<T> {
#[rustc_const_stable(feature = "const_weak_new", since = "1.73.0")]
#[must_use]
pub const fn new() -> Weak<T> {
Weak {
ptr: unsafe {
NonNull::new_unchecked(ptr::without_provenance_mut::<ArcInner<T>>(usize::MAX))
},
alloc: Global,
}
Weak { ptr: NonNull::without_provenance(NonZeroUsize::MAX), alloc: Global }
}
}
@ -2717,12 +2713,7 @@ impl<T, A: Allocator> Weak<T, A> {
#[inline]
#[unstable(feature = "allocator_api", issue = "32838")]
pub fn new_in(alloc: A) -> Weak<T, A> {
Weak {
ptr: unsafe {
NonNull::new_unchecked(ptr::without_provenance_mut::<ArcInner<T>>(usize::MAX))
},
alloc,
}
Weak { ptr: NonNull::without_provenance(NonZeroUsize::MAX), alloc }
}
}

View file

@ -233,8 +233,7 @@ impl Layout {
#[must_use]
#[inline]
pub const fn dangling(&self) -> NonNull<u8> {
// SAFETY: align is guaranteed to be non-zero
unsafe { NonNull::new_unchecked(crate::ptr::without_provenance_mut::<u8>(self.align())) }
NonNull::without_provenance(self.align.as_nonzero())
}
/// Creates a layout describing the record that can hold a value

View file

@ -103,7 +103,8 @@
//! the time.
use core::marker::PhantomData;
use core::ptr::{self, NonNull};
use core::num::NonZeroUsize;
use core::ptr::NonNull;
use super::{Custom, ErrorData, ErrorKind, RawOsError, SimpleMessage};
@ -176,7 +177,7 @@ impl Repr {
let utagged = ((code as usize) << 32) | TAG_OS;
// Safety: `TAG_OS` is not zero, so the result of the `|` is not 0.
let res = Self(
unsafe { NonNull::new_unchecked(ptr::without_provenance_mut(utagged)) },
NonNull::without_provenance(unsafe { NonZeroUsize::new_unchecked(utagged) }),
PhantomData,
);
// quickly smoke-check we encoded the right thing (This generally will
@ -193,7 +194,7 @@ impl Repr {
let utagged = ((kind as usize) << 32) | TAG_SIMPLE;
// Safety: `TAG_SIMPLE` is not zero, so the result of the `|` is not 0.
let res = Self(
unsafe { NonNull::new_unchecked(ptr::without_provenance_mut(utagged)) },
NonNull::without_provenance(unsafe { NonZeroUsize::new_unchecked(utagged) }),
PhantomData,
);
// quickly smoke-check we encoded the right thing (This generally will

View file

@ -342,6 +342,7 @@
#![feature(lazy_get)]
#![feature(maybe_uninit_slice)]
#![feature(maybe_uninit_write_slice)]
#![feature(nonnull_provenance)]
#![feature(panic_can_unwind)]
#![feature(panic_internals)]
#![feature(pin_coerce_unsized_trait)]