Skip to content

Commit e8c6d13

Browse files
committed
Prepare NonNull for pattern types
1 parent 8387095 commit e8c6d13

29 files changed

Lines changed: 913 additions & 928 deletions

File tree

library/core/src/ptr/non_null.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::clone::TrivialClone;
22
use crate::cmp::Ordering;
33
use crate::marker::{Destruct, PointeeSized, Unsize};
4-
use crate::mem::{MaybeUninit, SizedTypeProperties};
4+
use crate::mem::{MaybeUninit, SizedTypeProperties, transmute};
55
use crate::num::NonZero;
66
use crate::ops::{CoerceUnsized, DispatchFromDyn};
77
use crate::pin::PinCoerceUnsized;
@@ -100,9 +100,8 @@ impl<T: Sized> NonNull<T> {
100100
#[must_use]
101101
#[inline]
102102
pub const fn without_provenance(addr: NonZero<usize>) -> Self {
103-
let pointer = crate::ptr::without_provenance(addr.get());
104-
// SAFETY: we know `addr` is non-zero.
105-
unsafe { NonNull { pointer } }
103+
// SAFETY: we know `addr` is non-zero and all nonzero integers are valid raw pointers.
104+
unsafe { transmute(addr) }
106105
}
107106

108107
/// Creates a new `NonNull` that is dangling, but well-aligned.
@@ -239,7 +238,7 @@ impl<T: PointeeSized> NonNull<T> {
239238
"NonNull::new_unchecked requires that the pointer is non-null",
240239
(ptr: *mut () = ptr as *mut ()) => !ptr.is_null()
241240
);
242-
NonNull { pointer: ptr as _ }
241+
transmute(ptr)
243242
}
244243
}
245244

@@ -282,7 +281,7 @@ impl<T: PointeeSized> NonNull<T> {
282281
#[inline]
283282
pub const fn from_ref(r: &T) -> Self {
284283
// SAFETY: A reference cannot be null.
285-
unsafe { NonNull { pointer: r as *const T } }
284+
unsafe { transmute(r as *const T) }
286285
}
287286

288287
/// Converts a mutable reference to a `NonNull` pointer.
@@ -291,7 +290,7 @@ impl<T: PointeeSized> NonNull<T> {
291290
#[inline]
292291
pub const fn from_mut(r: &mut T) -> Self {
293292
// SAFETY: A mutable reference cannot be null.
294-
unsafe { NonNull { pointer: r as *mut T } }
293+
unsafe { transmute(r as *mut T) }
295294
}
296295

297296
/// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
@@ -502,7 +501,7 @@ impl<T: PointeeSized> NonNull<T> {
502501
#[inline]
503502
pub const fn cast<U>(self) -> NonNull<U> {
504503
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
505-
unsafe { NonNull { pointer: self.as_ptr() as *mut U } }
504+
unsafe { transmute(self.as_ptr() as *mut U) }
506505
}
507506

508507
/// Try to cast to a pointer of another type by checking alignment.
@@ -581,7 +580,7 @@ impl<T: PointeeSized> NonNull<T> {
581580
// Additionally safety contract of `offset` guarantees that the resulting pointer is
582581
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
583582
// construct `NonNull`.
584-
unsafe { NonNull { pointer: intrinsics::offset(self.as_ptr(), count) } }
583+
unsafe { transmute(intrinsics::offset(self.as_ptr(), count)) }
585584
}
586585

587586
/// Calculates the offset from a pointer in bytes.
@@ -605,7 +604,7 @@ impl<T: PointeeSized> NonNull<T> {
605604
// Additionally safety contract of `offset` guarantees that the resulting pointer is
606605
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
607606
// construct `NonNull`.
608-
unsafe { NonNull { pointer: self.as_ptr().byte_offset(count) } }
607+
unsafe { transmute(self.as_ptr().byte_offset(count)) }
609608
}
610609

611610
/// Adds an offset to a pointer (convenience for `.offset(count as isize)`).
@@ -657,7 +656,7 @@ impl<T: PointeeSized> NonNull<T> {
657656
// Additionally safety contract of `offset` guarantees that the resulting pointer is
658657
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
659658
// construct `NonNull`.
660-
unsafe { NonNull { pointer: intrinsics::offset(self.as_ptr(), count) } }
659+
unsafe { transmute(intrinsics::offset(self.as_ptr(), count)) }
661660
}
662661

663662
/// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
@@ -681,7 +680,7 @@ impl<T: PointeeSized> NonNull<T> {
681680
// Additionally safety contract of `add` guarantees that the resulting pointer is pointing
682681
// to an allocation, there can't be an allocation at null, thus it's safe to construct
683682
// `NonNull`.
684-
unsafe { NonNull { pointer: self.as_ptr().byte_add(count) } }
683+
unsafe { transmute(self.as_ptr().byte_add(count)) }
685684
}
686685

687686
/// Subtracts an offset from a pointer (convenience for
@@ -763,7 +762,7 @@ impl<T: PointeeSized> NonNull<T> {
763762
// Additionally safety contract of `sub` guarantees that the resulting pointer is pointing
764763
// to an allocation, there can't be an allocation at null, thus it's safe to construct
765764
// `NonNull`.
766-
unsafe { NonNull { pointer: self.as_ptr().byte_sub(count) } }
765+
unsafe { transmute(self.as_ptr().byte_sub(count)) }
767766
}
768767

769768
/// Calculates the distance between two pointers within the same allocation. The returned value is in

tests/codegen-llvm/loads.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn load_raw_pointer<'a>(x: &*const i32) -> *const i32 {
5858
// CHECK-LABEL: @load_box
5959
#[no_mangle]
6060
pub fn load_box<'a>(x: Box<Box<i32>>) -> Box<i32> {
61-
// CHECK: load ptr, ptr %{{.*}}, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !align ![[ALIGN_4_META]], !noundef !{{[0-9]+}}
61+
// CHECK: load ptr, ptr %{{.*}}, align [[PTR_ALIGNMENT]], !nonnull !{{[0-9]+}}, !noundef !{{[0-9]+}}
6262
*x
6363
}
6464

tests/codegen-units/item-collection/opaque-return-impls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@ pub fn foo3() -> Box<dyn Iterator<Item = usize>> {
8787
//~ MONO_ITEM fn std::boxed::Box::<Counter>::new
8888
//~ MONO_ITEM fn Counter::new
8989
//~ MONO_ITEM fn std::fmt::Arguments::<'_>::from_str
90+
//~ MONO_ITEM fn std::boxed::box_new_uninit

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.DataflowConstProp.32bit.panic-abort.diff

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020
scope 8 (inlined std::ptr::Alignment::as_nonzero) {
2121
}
2222
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
23-
let _6: *const [bool; 0];
24-
scope 10 {
25-
}
26-
scope 11 (inlined NonZero::<usize>::get) {
27-
}
28-
scope 12 (inlined std::ptr::without_provenance::<[bool; 0]>) {
29-
scope 13 (inlined without_provenance_mut::<[bool; 0]>) {
30-
}
31-
}
3223
}
3324
}
3425
scope 7 (inlined std::ptr::Alignment::of::<[bool; 0]>) {
@@ -43,10 +34,7 @@
4334
StorageLive(_3);
4435
StorageLive(_4);
4536
StorageLive(_5);
46-
StorageLive(_6);
47-
_6 = const {0x1 as *const [bool; 0]};
4837
_5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
49-
StorageDead(_6);
5038
_4 = const std::ptr::Unique::<[bool; 0]> {{ pointer: NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }}, _marker: PhantomData::<[bool; 0]> }};
5139
StorageDead(_5);
5240
_3 = const std::ptr::Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }};

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.DataflowConstProp.32bit.panic-unwind.diff

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020
scope 8 (inlined std::ptr::Alignment::as_nonzero) {
2121
}
2222
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
23-
let _6: *const [bool; 0];
24-
scope 10 {
25-
}
26-
scope 11 (inlined NonZero::<usize>::get) {
27-
}
28-
scope 12 (inlined std::ptr::without_provenance::<[bool; 0]>) {
29-
scope 13 (inlined without_provenance_mut::<[bool; 0]>) {
30-
}
31-
}
3223
}
3324
}
3425
scope 7 (inlined std::ptr::Alignment::of::<[bool; 0]>) {
@@ -43,10 +34,7 @@
4334
StorageLive(_3);
4435
StorageLive(_4);
4536
StorageLive(_5);
46-
StorageLive(_6);
47-
_6 = const {0x1 as *const [bool; 0]};
4837
_5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
49-
StorageDead(_6);
5038
_4 = const std::ptr::Unique::<[bool; 0]> {{ pointer: NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }}, _marker: PhantomData::<[bool; 0]> }};
5139
StorageDead(_5);
5240
_3 = const std::ptr::Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }};

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.DataflowConstProp.64bit.panic-abort.diff

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020
scope 8 (inlined std::ptr::Alignment::as_nonzero) {
2121
}
2222
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
23-
let _6: *const [bool; 0];
24-
scope 10 {
25-
}
26-
scope 11 (inlined NonZero::<usize>::get) {
27-
}
28-
scope 12 (inlined std::ptr::without_provenance::<[bool; 0]>) {
29-
scope 13 (inlined without_provenance_mut::<[bool; 0]>) {
30-
}
31-
}
3223
}
3324
}
3425
scope 7 (inlined std::ptr::Alignment::of::<[bool; 0]>) {
@@ -43,10 +34,7 @@
4334
StorageLive(_3);
4435
StorageLive(_4);
4536
StorageLive(_5);
46-
StorageLive(_6);
47-
_6 = const {0x1 as *const [bool; 0]};
4837
_5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
49-
StorageDead(_6);
5038
_4 = const std::ptr::Unique::<[bool; 0]> {{ pointer: NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }}, _marker: PhantomData::<[bool; 0]> }};
5139
StorageDead(_5);
5240
_3 = const std::ptr::Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }};

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.DataflowConstProp.64bit.panic-unwind.diff

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020
scope 8 (inlined std::ptr::Alignment::as_nonzero) {
2121
}
2222
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
23-
let _6: *const [bool; 0];
24-
scope 10 {
25-
}
26-
scope 11 (inlined NonZero::<usize>::get) {
27-
}
28-
scope 12 (inlined std::ptr::without_provenance::<[bool; 0]>) {
29-
scope 13 (inlined without_provenance_mut::<[bool; 0]>) {
30-
}
31-
}
3223
}
3324
}
3425
scope 7 (inlined std::ptr::Alignment::of::<[bool; 0]>) {
@@ -43,10 +34,7 @@
4334
StorageLive(_3);
4435
StorageLive(_4);
4536
StorageLive(_5);
46-
StorageLive(_6);
47-
_6 = const {0x1 as *const [bool; 0]};
4837
_5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
49-
StorageDead(_6);
5038
_4 = const std::ptr::Unique::<[bool; 0]> {{ pointer: NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }}, _marker: PhantomData::<[bool; 0]> }};
5139
StorageDead(_5);
5240
_3 = const std::ptr::Unique::<[bool]> {{ pointer: NonNull::<[bool]> {{ pointer: Indirect { alloc_id: ALLOC0, offset: Size(0 bytes) }: *const [bool] }}, _marker: PhantomData::<[bool]> }};

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.GVN.32bit.panic-abort.diff

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020
scope 8 (inlined std::ptr::Alignment::as_nonzero) {
2121
}
2222
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
23-
let _6: *const [bool; 0];
24-
scope 10 {
25-
}
26-
scope 11 (inlined NonZero::<usize>::get) {
27-
}
28-
scope 12 (inlined std::ptr::without_provenance::<[bool; 0]>) {
29-
scope 13 (inlined without_provenance_mut::<[bool; 0]>) {
30-
}
31-
}
3223
}
3324
}
3425
scope 7 (inlined std::ptr::Alignment::of::<[bool; 0]>) {
@@ -43,13 +34,9 @@
4334
StorageLive(_3);
4435
StorageLive(_4);
4536
StorageLive(_5);
46-
StorageLive(_6);
47-
- _6 = const <[bool; 0] as std::mem::SizedTypeProperties>::ALIGNMENT as *const [bool; 0] (Transmute);
48-
- _5 = NonNull::<[bool; 0]> { pointer: copy _6 };
49-
+ _6 = const {0x1 as *const [bool; 0]};
50-
+ _5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
51-
StorageDead(_6);
37+
- _5 = const <[bool; 0] as std::mem::SizedTypeProperties>::ALIGNMENT as std::ptr::NonNull<[bool; 0]> (Transmute);
5238
- _4 = std::ptr::Unique::<[bool; 0]> { pointer: move _5, _marker: const PhantomData::<[bool; 0]> };
39+
+ _5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
5340
+ _4 = const std::ptr::Unique::<[bool; 0]> {{ pointer: NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }}, _marker: PhantomData::<[bool; 0]> }};
5441
StorageDead(_5);
5542
- _3 = move _4 as std::ptr::Unique<[bool]> (PointerCoercion(Unsize, Implicit));

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.GVN.32bit.panic-unwind.diff

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020
scope 8 (inlined std::ptr::Alignment::as_nonzero) {
2121
}
2222
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
23-
let _6: *const [bool; 0];
24-
scope 10 {
25-
}
26-
scope 11 (inlined NonZero::<usize>::get) {
27-
}
28-
scope 12 (inlined std::ptr::without_provenance::<[bool; 0]>) {
29-
scope 13 (inlined without_provenance_mut::<[bool; 0]>) {
30-
}
31-
}
3223
}
3324
}
3425
scope 7 (inlined std::ptr::Alignment::of::<[bool; 0]>) {
@@ -43,13 +34,9 @@
4334
StorageLive(_3);
4435
StorageLive(_4);
4536
StorageLive(_5);
46-
StorageLive(_6);
47-
- _6 = const <[bool; 0] as std::mem::SizedTypeProperties>::ALIGNMENT as *const [bool; 0] (Transmute);
48-
- _5 = NonNull::<[bool; 0]> { pointer: copy _6 };
49-
+ _6 = const {0x1 as *const [bool; 0]};
50-
+ _5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
51-
StorageDead(_6);
37+
- _5 = const <[bool; 0] as std::mem::SizedTypeProperties>::ALIGNMENT as std::ptr::NonNull<[bool; 0]> (Transmute);
5238
- _4 = std::ptr::Unique::<[bool; 0]> { pointer: move _5, _marker: const PhantomData::<[bool; 0]> };
39+
+ _5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
5340
+ _4 = const std::ptr::Unique::<[bool; 0]> {{ pointer: NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }}, _marker: PhantomData::<[bool; 0]> }};
5441
StorageDead(_5);
5542
- _3 = move _4 as std::ptr::Unique<[bool]> (PointerCoercion(Unsize, Implicit));

tests/mir-opt/dataflow-const-prop/default_boxed_slice.main.GVN.64bit.panic-abort.diff

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020
scope 8 (inlined std::ptr::Alignment::as_nonzero) {
2121
}
2222
scope 9 (inlined NonNull::<[bool; 0]>::without_provenance) {
23-
let _6: *const [bool; 0];
24-
scope 10 {
25-
}
26-
scope 11 (inlined NonZero::<usize>::get) {
27-
}
28-
scope 12 (inlined std::ptr::without_provenance::<[bool; 0]>) {
29-
scope 13 (inlined without_provenance_mut::<[bool; 0]>) {
30-
}
31-
}
3223
}
3324
}
3425
scope 7 (inlined std::ptr::Alignment::of::<[bool; 0]>) {
@@ -43,13 +34,9 @@
4334
StorageLive(_3);
4435
StorageLive(_4);
4536
StorageLive(_5);
46-
StorageLive(_6);
47-
- _6 = const <[bool; 0] as std::mem::SizedTypeProperties>::ALIGNMENT as *const [bool; 0] (Transmute);
48-
- _5 = NonNull::<[bool; 0]> { pointer: copy _6 };
49-
+ _6 = const {0x1 as *const [bool; 0]};
50-
+ _5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
51-
StorageDead(_6);
37+
- _5 = const <[bool; 0] as std::mem::SizedTypeProperties>::ALIGNMENT as std::ptr::NonNull<[bool; 0]> (Transmute);
5238
- _4 = std::ptr::Unique::<[bool; 0]> { pointer: move _5, _marker: const PhantomData::<[bool; 0]> };
39+
+ _5 = const NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }};
5340
+ _4 = const std::ptr::Unique::<[bool; 0]> {{ pointer: NonNull::<[bool; 0]> {{ pointer: {0x1 as *const [bool; 0]} }}, _marker: PhantomData::<[bool; 0]> }};
5441
StorageDead(_5);
5542
- _3 = move _4 as std::ptr::Unique<[bool]> (PointerCoercion(Unsize, Implicit));

0 commit comments

Comments
 (0)