Skip to content

Commit 453a0d0

Browse files
jhprattgitbot
authored and
gitbot
committed
Rollup merge of rust-lang#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` (rust-lang#135243). Close rust-lang#135343
2 parents 21d679a + 755dfe3 commit 453a0d0

File tree

6 files changed

+13
-29
lines changed

6 files changed

+13
-29
lines changed

alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
#![feature(local_waker)]
127127
#![feature(maybe_uninit_slice)]
128128
#![feature(maybe_uninit_uninit_array_transpose)]
129+
#![feature(nonnull_provenance)]
129130
#![feature(panic_internals)]
130131
#![feature(pattern)]
131132
#![feature(pin_coerce_unsized_trait)]

alloc/src/rc.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ use core::intrinsics::abort;
252252
use core::iter;
253253
use core::marker::{PhantomData, Unsize};
254254
use core::mem::{self, ManuallyDrop, align_of_val_raw};
255+
use core::num::NonZeroUsize;
255256
use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, LegacyReceiver};
256257
use core::panic::{RefUnwindSafe, UnwindSafe};
257258
#[cfg(not(no_global_oom_handling))]
@@ -3027,12 +3028,7 @@ impl<T> Weak<T> {
30273028
#[rustc_const_stable(feature = "const_weak_new", since = "1.73.0")]
30283029
#[must_use]
30293030
pub const fn new() -> Weak<T> {
3030-
Weak {
3031-
ptr: unsafe {
3032-
NonNull::new_unchecked(ptr::without_provenance_mut::<RcInner<T>>(usize::MAX))
3033-
},
3034-
alloc: Global,
3035-
}
3031+
Weak { ptr: NonNull::without_provenance(NonZeroUsize::MAX), alloc: Global }
30363032
}
30373033
}
30383034

@@ -3054,12 +3050,7 @@ impl<T, A: Allocator> Weak<T, A> {
30543050
#[inline]
30553051
#[unstable(feature = "allocator_api", issue = "32838")]
30563052
pub fn new_in(alloc: A) -> Weak<T, A> {
3057-
Weak {
3058-
ptr: unsafe {
3059-
NonNull::new_unchecked(ptr::without_provenance_mut::<RcInner<T>>(usize::MAX))
3060-
},
3061-
alloc,
3062-
}
3053+
Weak { ptr: NonNull::without_provenance(NonZeroUsize::MAX), alloc }
30633054
}
30643055
}
30653056

alloc/src/sync.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use core::intrinsics::abort;
1818
use core::iter;
1919
use core::marker::{PhantomData, Unsize};
2020
use core::mem::{self, ManuallyDrop, align_of_val_raw};
21+
use core::num::NonZeroUsize;
2122
use core::ops::{CoerceUnsized, Deref, DerefPure, DispatchFromDyn, LegacyReceiver};
2223
use core::panic::{RefUnwindSafe, UnwindSafe};
2324
use core::pin::{Pin, PinCoerceUnsized};
@@ -2687,12 +2688,7 @@ impl<T> Weak<T> {
26872688
#[rustc_const_stable(feature = "const_weak_new", since = "1.73.0")]
26882689
#[must_use]
26892690
pub const fn new() -> Weak<T> {
2690-
Weak {
2691-
ptr: unsafe {
2692-
NonNull::new_unchecked(ptr::without_provenance_mut::<ArcInner<T>>(usize::MAX))
2693-
},
2694-
alloc: Global,
2695-
}
2691+
Weak { ptr: NonNull::without_provenance(NonZeroUsize::MAX), alloc: Global }
26962692
}
26972693
}
26982694

@@ -2717,12 +2713,7 @@ impl<T, A: Allocator> Weak<T, A> {
27172713
#[inline]
27182714
#[unstable(feature = "allocator_api", issue = "32838")]
27192715
pub fn new_in(alloc: A) -> Weak<T, A> {
2720-
Weak {
2721-
ptr: unsafe {
2722-
NonNull::new_unchecked(ptr::without_provenance_mut::<ArcInner<T>>(usize::MAX))
2723-
},
2724-
alloc,
2725-
}
2716+
Weak { ptr: NonNull::without_provenance(NonZeroUsize::MAX), alloc }
27262717
}
27272718
}
27282719

core/src/alloc/layout.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ impl Layout {
233233
#[must_use]
234234
#[inline]
235235
pub const fn dangling(&self) -> NonNull<u8> {
236-
// SAFETY: align is guaranteed to be non-zero
237-
unsafe { NonNull::new_unchecked(crate::ptr::without_provenance_mut::<u8>(self.align())) }
236+
NonNull::without_provenance(self.align.as_nonzero())
238237
}
239238

240239
/// Creates a layout describing the record that can hold a value

std/src/io/error/repr_bitpacked.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@
103103
//! the time.
104104
105105
use core::marker::PhantomData;
106-
use core::ptr::{self, NonNull};
106+
use core::num::NonZeroUsize;
107+
use core::ptr::NonNull;
107108

108109
use super::{Custom, ErrorData, ErrorKind, RawOsError, SimpleMessage};
109110

@@ -176,7 +177,7 @@ impl Repr {
176177
let utagged = ((code as usize) << 32) | TAG_OS;
177178
// Safety: `TAG_OS` is not zero, so the result of the `|` is not 0.
178179
let res = Self(
179-
unsafe { NonNull::new_unchecked(ptr::without_provenance_mut(utagged)) },
180+
NonNull::without_provenance(unsafe { NonZeroUsize::new_unchecked(utagged) }),
180181
PhantomData,
181182
);
182183
// quickly smoke-check we encoded the right thing (This generally will
@@ -193,7 +194,7 @@ impl Repr {
193194
let utagged = ((kind as usize) << 32) | TAG_SIMPLE;
194195
// Safety: `TAG_SIMPLE` is not zero, so the result of the `|` is not 0.
195196
let res = Self(
196-
unsafe { NonNull::new_unchecked(ptr::without_provenance_mut(utagged)) },
197+
NonNull::without_provenance(unsafe { NonZeroUsize::new_unchecked(utagged) }),
197198
PhantomData,
198199
);
199200
// quickly smoke-check we encoded the right thing (This generally will

std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@
342342
#![feature(lazy_get)]
343343
#![feature(maybe_uninit_slice)]
344344
#![feature(maybe_uninit_write_slice)]
345+
#![feature(nonnull_provenance)]
345346
#![feature(panic_can_unwind)]
346347
#![feature(panic_internals)]
347348
#![feature(pin_coerce_unsized_trait)]

0 commit comments

Comments
 (0)