Skip to content

Commit

Permalink
Use NonNull::without_provenance within the standard library
Browse files Browse the repository at this point in the history
This API removes the need for several `unsafe` blocks, and leads to
clearer code.
  • Loading branch information
samueltardieu authored and gitbot committed Feb 20, 2025
1 parent 5054a17 commit e158c6a
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 29 deletions.
1 change: 1 addition & 0 deletions alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
15 changes: 3 additions & 12 deletions alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -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 }
}
}

Expand All @@ -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 }
}
}

Expand Down
15 changes: 3 additions & 12 deletions alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 }
}
}

Expand All @@ -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 }
}
}

Expand Down
3 changes: 1 addition & 2 deletions core/src/alloc/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions std/src/io/error/repr_bitpacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit e158c6a

Please sign in to comment.