Skip to content

Commit ac932b7

Browse files
committed
Add assume statements in NonZero library types.
1 parent 25f8d01 commit ac932b7

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) {
@@ -111,10 +134,26 @@
111134
StorageDead(_15);
112135
StorageDead(_12);
113136
StorageDead(_6);
137+
StorageLive(_20);
114138
StorageLive(_18);
115-
_18 = (_5.0: *const [u8]);
116-
_4 = move _18 as *mut [u8] (PtrToPtr);
139+
StorageLive(_19);
140+
_20 = (_5.0: *const [u8]);
141+
StorageLive(_21);
142+
_21 = _20 as *const u8 (PtrToPtr);
143+
StorageLive(_22);
144+
StorageLive(_23);
145+
_23 = _21 as *const () (PtrToPtr);
146+
_22 = move _23 as usize (Transmute);
147+
StorageDead(_23);
148+
_19 = Eq(move _22, const 0_usize);
149+
StorageDead(_22);
150+
StorageDead(_21);
151+
_18 = Not(move _19);
152+
StorageDead(_19);
153+
assume(move _18);
117154
StorageDead(_18);
155+
_4 = _20 as *mut [u8] (PtrToPtr);
156+
StorageDead(_20);
118157
StorageDead(_5);
119158
_3 = move _4 as *mut u8 (PtrToPtr);
120159
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) {
@@ -50,10 +73,26 @@
5073

5174
bb1: {
5275
StorageDead(_6);
76+
StorageLive(_14);
5377
StorageLive(_12);
54-
_12 = (_5.0: *const [u8]);
55-
_4 = move _12 as *mut [u8] (PtrToPtr);
78+
StorageLive(_13);
79+
_14 = (_5.0: *const [u8]);
80+
StorageLive(_15);
81+
_15 = _14 as *const u8 (PtrToPtr);
82+
StorageLive(_16);
83+
StorageLive(_17);
84+
_17 = _15 as *const () (PtrToPtr);
85+
_16 = move _17 as usize (Transmute);
86+
StorageDead(_17);
87+
_13 = Eq(move _16, const 0_usize);
88+
StorageDead(_16);
89+
StorageDead(_15);
90+
_12 = Not(move _13);
91+
StorageDead(_13);
92+
assume(move _12);
5693
StorageDead(_12);
94+
_4 = _14 as *mut [u8] (PtrToPtr);
95+
StorageDead(_14);
5796
StorageDead(_5);
5897
_3 = move _4 as *mut u8 (PtrToPtr);
5998
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) {
@@ -111,10 +134,26 @@
111134
StorageDead(_15);
112135
StorageDead(_12);
113136
StorageDead(_6);
137+
StorageLive(_20);
114138
StorageLive(_18);
115-
_18 = (_5.0: *const [u8]);
116-
_4 = move _18 as *mut [u8] (PtrToPtr);
139+
StorageLive(_19);
140+
_20 = (_5.0: *const [u8]);
141+
StorageLive(_21);
142+
_21 = _20 as *const u8 (PtrToPtr);
143+
StorageLive(_22);
144+
StorageLive(_23);
145+
_23 = _21 as *const () (PtrToPtr);
146+
_22 = move _23 as usize (Transmute);
147+
StorageDead(_23);
148+
_19 = Eq(move _22, const 0_usize);
149+
StorageDead(_22);
150+
StorageDead(_21);
151+
_18 = Not(move _19);
152+
StorageDead(_19);
153+
assume(move _18);
117154
StorageDead(_18);
155+
_4 = _20 as *mut [u8] (PtrToPtr);
156+
StorageDead(_20);
118157
StorageDead(_5);
119158
_3 = move _4 as *mut u8 (PtrToPtr);
120159
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) {
@@ -50,10 +73,26 @@
5073

5174
bb1: {
5275
StorageDead(_6);
76+
StorageLive(_14);
5377
StorageLive(_12);
54-
_12 = (_5.0: *const [u8]);
55-
_4 = move _12 as *mut [u8] (PtrToPtr);
78+
StorageLive(_13);
79+
_14 = (_5.0: *const [u8]);
80+
StorageLive(_15);
81+
_15 = _14 as *const u8 (PtrToPtr);
82+
StorageLive(_16);
83+
StorageLive(_17);
84+
_17 = _15 as *const () (PtrToPtr);
85+
_16 = move _17 as usize (Transmute);
86+
StorageDead(_17);
87+
_13 = Eq(move _16, const 0_usize);
88+
StorageDead(_16);
89+
StorageDead(_15);
90+
_12 = Not(move _13);
91+
StorageDead(_13);
92+
assume(move _12);
5693
StorageDead(_12);
94+
_4 = _14 as *mut [u8] (PtrToPtr);
95+
StorageDead(_14);
5796
StorageDead(_5);
5897
_3 = move _4 as *mut u8 (PtrToPtr);
5998
StorageDead(_4);

0 commit comments

Comments
 (0)