Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use NonNull::without_provenance within the standard library #135347

Merged
merged 1 commit into from
Jan 11, 2025
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
1 change: 1 addition & 0 deletions library/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 library/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 library/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 library/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 library/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 library/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
Loading