Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 50855fe

Browse files
committed
Add assume statements in NonZero library types.
1 parent 284cb71 commit 50855fe

5 files changed

+171
-12
lines changed

library/core/src/ptr/non_null.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,13 @@ impl<T: ?Sized> NonNull<T> {
341341
/// ```
342342
#[stable(feature = "nonnull", since = "1.25.0")]
343343
#[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]
344+
#[rustc_allow_const_fn_unstable(const_ptr_is_null)]
344345
#[rustc_never_returns_null_ptr]
345346
#[must_use]
346347
#[inline(always)]
347348
pub const fn as_ptr(self) -> *mut T {
349+
// SAFETY: By definition of this type.
350+
unsafe { crate::intrinsics::assume(!self.pointer.is_null()) };
348351
self.pointer as *mut T
349352
}
350353

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-abort.diff

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,30 @@
3838
}
3939
scope 9 (inlined NonNull::<[u8]>::as_ptr) {
4040
debug self => _5;
41-
let mut _18: *const [u8];
41+
let mut _18: bool;
42+
let mut _19: bool;
43+
let mut _20: *const [u8];
44+
scope 10 {
45+
scope 11 (inlined std::ptr::const_ptr::<impl *const [u8]>::is_null) {
46+
debug self => _20;
47+
let mut _21: *const u8;
48+
scope 12 {
49+
scope 13 (inlined std::ptr::const_ptr::<impl *const T>::is_null::runtime_impl) {
50+
debug ptr => _21;
51+
let mut _22: usize;
52+
scope 14 (inlined std::ptr::const_ptr::<impl *const u8>::addr) {
53+
debug self => _21;
54+
let mut _23: *const ();
55+
scope 15 {
56+
scope 16 (inlined std::ptr::const_ptr::<impl *const u8>::cast::<()>) {
57+
debug self => _21;
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
4265
}
4366
}
4467
scope 3 (inlined #[track_caller] Option::<Layout>::unwrap) {
@@ -101,10 +124,26 @@
101124
StorageDead(_15);
102125
StorageDead(_12);
103126
StorageDead(_6);
127+
StorageLive(_20);
104128
StorageLive(_18);
105-
_18 = (_5.0: *const [u8]);
106-
_4 = move _18 as *mut [u8] (PtrToPtr);
129+
StorageLive(_19);
130+
_20 = (_5.0: *const [u8]);
131+
StorageLive(_21);
132+
_21 = _20 as *const u8 (PtrToPtr);
133+
StorageLive(_22);
134+
StorageLive(_23);
135+
_23 = _21 as *const () (PtrToPtr);
136+
_22 = move _23 as usize (Transmute);
137+
StorageDead(_23);
138+
_19 = Eq(move _22, const 0_usize);
139+
StorageDead(_22);
140+
StorageDead(_21);
141+
_18 = Not(move _19);
142+
StorageDead(_19);
143+
assume(move _18);
107144
StorageDead(_18);
145+
_4 = _20 as *mut [u8] (PtrToPtr);
146+
StorageDead(_20);
108147
StorageDead(_5);
109148
_3 = move _4 as *mut u8 (PtrToPtr);
110149
StorageDead(_4);

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.32bit.panic-unwind.diff

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,30 @@
2323
}
2424
scope 6 (inlined NonNull::<[u8]>::as_ptr) {
2525
debug self => _5;
26-
let mut _12: *const [u8];
26+
let mut _12: bool;
27+
let mut _13: bool;
28+
let mut _14: *const [u8];
29+
scope 7 {
30+
scope 8 (inlined std::ptr::const_ptr::<impl *const [u8]>::is_null) {
31+
debug self => _14;
32+
let mut _15: *const u8;
33+
scope 9 {
34+
scope 10 (inlined std::ptr::const_ptr::<impl *const T>::is_null::runtime_impl) {
35+
debug ptr => _15;
36+
let mut _16: usize;
37+
scope 11 (inlined std::ptr::const_ptr::<impl *const u8>::addr) {
38+
debug self => _15;
39+
let mut _17: *const ();
40+
scope 12 {
41+
scope 13 (inlined std::ptr::const_ptr::<impl *const u8>::cast::<()>) {
42+
debug self => _15;
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
2750
}
2851
}
2952
scope 3 (inlined #[track_caller] Option::<Layout>::unwrap) {
@@ -48,10 +71,26 @@
4871

4972
bb1: {
5073
StorageDead(_6);
74+
StorageLive(_14);
5175
StorageLive(_12);
52-
_12 = (_5.0: *const [u8]);
53-
_4 = move _12 as *mut [u8] (PtrToPtr);
76+
StorageLive(_13);
77+
_14 = (_5.0: *const [u8]);
78+
StorageLive(_15);
79+
_15 = _14 as *const u8 (PtrToPtr);
80+
StorageLive(_16);
81+
StorageLive(_17);
82+
_17 = _15 as *const () (PtrToPtr);
83+
_16 = move _17 as usize (Transmute);
84+
StorageDead(_17);
85+
_13 = Eq(move _16, const 0_usize);
86+
StorageDead(_16);
87+
StorageDead(_15);
88+
_12 = Not(move _13);
89+
StorageDead(_13);
90+
assume(move _12);
5491
StorageDead(_12);
92+
_4 = _14 as *mut [u8] (PtrToPtr);
93+
StorageDead(_14);
5594
StorageDead(_5);
5695
_3 = move _4 as *mut u8 (PtrToPtr);
5796
StorageDead(_4);

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-abort.diff

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,30 @@
3838
}
3939
scope 9 (inlined NonNull::<[u8]>::as_ptr) {
4040
debug self => _5;
41-
let mut _18: *const [u8];
41+
let mut _18: bool;
42+
let mut _19: bool;
43+
let mut _20: *const [u8];
44+
scope 10 {
45+
scope 11 (inlined std::ptr::const_ptr::<impl *const [u8]>::is_null) {
46+
debug self => _20;
47+
let mut _21: *const u8;
48+
scope 12 {
49+
scope 13 (inlined std::ptr::const_ptr::<impl *const T>::is_null::runtime_impl) {
50+
debug ptr => _21;
51+
let mut _22: usize;
52+
scope 14 (inlined std::ptr::const_ptr::<impl *const u8>::addr) {
53+
debug self => _21;
54+
let mut _23: *const ();
55+
scope 15 {
56+
scope 16 (inlined std::ptr::const_ptr::<impl *const u8>::cast::<()>) {
57+
debug self => _21;
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
4265
}
4366
}
4467
scope 3 (inlined #[track_caller] Option::<Layout>::unwrap) {
@@ -101,10 +124,26 @@
101124
StorageDead(_15);
102125
StorageDead(_12);
103126
StorageDead(_6);
127+
StorageLive(_20);
104128
StorageLive(_18);
105-
_18 = (_5.0: *const [u8]);
106-
_4 = move _18 as *mut [u8] (PtrToPtr);
129+
StorageLive(_19);
130+
_20 = (_5.0: *const [u8]);
131+
StorageLive(_21);
132+
_21 = _20 as *const u8 (PtrToPtr);
133+
StorageLive(_22);
134+
StorageLive(_23);
135+
_23 = _21 as *const () (PtrToPtr);
136+
_22 = move _23 as usize (Transmute);
137+
StorageDead(_23);
138+
_19 = Eq(move _22, const 0_usize);
139+
StorageDead(_22);
140+
StorageDead(_21);
141+
_18 = Not(move _19);
142+
StorageDead(_19);
143+
assume(move _18);
107144
StorageDead(_18);
145+
_4 = _20 as *mut [u8] (PtrToPtr);
146+
StorageDead(_20);
108147
StorageDead(_5);
109148
_3 = move _4 as *mut u8 (PtrToPtr);
110149
StorageDead(_4);

tests/mir-opt/pre-codegen/issue_117368_print_invalid_constant.main.GVN.64bit.panic-unwind.diff

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,30 @@
2323
}
2424
scope 6 (inlined NonNull::<[u8]>::as_ptr) {
2525
debug self => _5;
26-
let mut _12: *const [u8];
26+
let mut _12: bool;
27+
let mut _13: bool;
28+
let mut _14: *const [u8];
29+
scope 7 {
30+
scope 8 (inlined std::ptr::const_ptr::<impl *const [u8]>::is_null) {
31+
debug self => _14;
32+
let mut _15: *const u8;
33+
scope 9 {
34+
scope 10 (inlined std::ptr::const_ptr::<impl *const T>::is_null::runtime_impl) {
35+
debug ptr => _15;
36+
let mut _16: usize;
37+
scope 11 (inlined std::ptr::const_ptr::<impl *const u8>::addr) {
38+
debug self => _15;
39+
let mut _17: *const ();
40+
scope 12 {
41+
scope 13 (inlined std::ptr::const_ptr::<impl *const u8>::cast::<()>) {
42+
debug self => _15;
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
2750
}
2851
}
2952
scope 3 (inlined #[track_caller] Option::<Layout>::unwrap) {
@@ -48,10 +71,26 @@
4871

4972
bb1: {
5073
StorageDead(_6);
74+
StorageLive(_14);
5175
StorageLive(_12);
52-
_12 = (_5.0: *const [u8]);
53-
_4 = move _12 as *mut [u8] (PtrToPtr);
76+
StorageLive(_13);
77+
_14 = (_5.0: *const [u8]);
78+
StorageLive(_15);
79+
_15 = _14 as *const u8 (PtrToPtr);
80+
StorageLive(_16);
81+
StorageLive(_17);
82+
_17 = _15 as *const () (PtrToPtr);
83+
_16 = move _17 as usize (Transmute);
84+
StorageDead(_17);
85+
_13 = Eq(move _16, const 0_usize);
86+
StorageDead(_16);
87+
StorageDead(_15);
88+
_12 = Not(move _13);
89+
StorageDead(_13);
90+
assume(move _12);
5491
StorageDead(_12);
92+
_4 = _14 as *mut [u8] (PtrToPtr);
93+
StorageDead(_14);
5594
StorageDead(_5);
5695
_3 = move _4 as *mut u8 (PtrToPtr);
5796
StorageDead(_4);

0 commit comments

Comments
 (0)