Skip to content

Commit fd5c78b

Browse files
committed
Add assume statements in NonZero library types.
1 parent e1de04a commit fd5c78b

File tree

4 files changed

+226
-57
lines changed

4 files changed

+226
-57
lines changed

library/core/src/num/nonzero.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ macro_rules! nonzero_integers {
103103
#[$stability]
104104
#[inline]
105105
#[rustc_const_stable(feature = "const_nonzero_get", since = "1.34.0")]
106+
#[rustc_allow_const_fn_unstable(const_assume)]
106107
pub const fn get(self) -> $Int {
108+
// SAFETY: By definition of this type.
109+
unsafe { crate::intrinsics::assume(self.0 != 0) };
107110
self.0
108111
}
109-
110112
}
111113

112114
#[stable(feature = "from_nonzero", since = "1.31.0")]

library/core/src/ptr/non_null.rs

+3
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,13 @@ impl<T: ?Sized> NonNull<T> {
339339
/// ```
340340
#[stable(feature = "nonnull", since = "1.25.0")]
341341
#[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]
342+
#[rustc_allow_const_fn_unstable(const_assume, const_ptr_is_null)]
342343
#[rustc_never_returns_null_ptr]
343344
#[must_use]
344345
#[inline(always)]
345346
pub const fn as_ptr(self) -> *mut T {
347+
// SAFETY: By definition of this type.
348+
unsafe { crate::intrinsics::assume(!self.pointer.is_null()) };
346349
self.pointer as *mut T
347350
}
348351

tests/mir-opt/inline/inline_into_box_place.main.Inline.panic-abort.diff

+110-28
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,52 @@
4141
+ scope 20 {
4242
+ scope 21 (inlined NonNull::<[u8]>::as_ptr) {
4343
+ debug self => _11;
44-
+ let mut _18: *const [u8];
44+
+ let mut _18: bool;
45+
+ let mut _19: bool;
46+
+ let mut _20: *const [u8];
47+
+ let mut _21: *const [u8];
48+
+ scope 22 {
49+
+ scope 23 (inlined ptr::const_ptr::<impl *const [u8]>::is_null) {
50+
+ debug self => _20;
51+
+ let mut _22: *const u8;
52+
+ scope 24 {
53+
+ scope 25 (inlined ptr::const_ptr::<impl *const T>::is_null::runtime_impl) {
54+
+ debug ptr => _22;
55+
+ let mut _23: usize;
56+
+ scope 26 (inlined ptr::const_ptr::<impl *const u8>::addr) {
57+
+ debug self => _22;
58+
+ let mut _24: *const ();
59+
+ scope 27 {
60+
+ scope 28 (inlined ptr::const_ptr::<impl *const u8>::cast::<()>) {
61+
+ debug self => _22;
62+
+ }
63+
+ }
64+
+ }
65+
+ }
66+
+ }
67+
+ }
68+
+ }
4569
+ }
46-
+ scope 22 (inlined ptr::mut_ptr::<impl *mut [u8]>::as_mut_ptr) {
70+
+ scope 29 (inlined ptr::mut_ptr::<impl *mut [u8]>::as_mut_ptr) {
4771
+ debug self => _17;
4872
+ }
49-
+ scope 23 (inlined NonNull::<u8>::new_unchecked) {
73+
+ scope 30 (inlined NonNull::<u8>::new_unchecked) {
5074
+ debug ptr => _16;
51-
+ let mut _19: *const u8;
52-
+ scope 24 {
53-
+ scope 25 (inlined NonNull::<T>::new_unchecked::runtime::<u8>) {
75+
+ let mut _25: *const u8;
76+
+ scope 31 {
77+
+ scope 32 (inlined NonNull::<T>::new_unchecked::runtime::<u8>) {
5478
+ debug ptr => _16;
55-
+ scope 26 (inlined ptr::mut_ptr::<impl *mut u8>::is_null) {
79+
+ scope 33 (inlined ptr::mut_ptr::<impl *mut u8>::is_null) {
5680
+ debug self => _16;
57-
+ let mut _20: *mut u8;
58-
+ scope 27 {
59-
+ scope 28 (inlined ptr::mut_ptr::<impl *mut T>::is_null::runtime_impl) {
60-
+ debug ptr => _20;
61-
+ scope 29 (inlined ptr::mut_ptr::<impl *mut u8>::addr) {
62-
+ debug self => _20;
63-
+ scope 30 {
64-
+ scope 31 (inlined ptr::mut_ptr::<impl *mut u8>::cast::<()>) {
65-
+ debug self => _20;
81+
+ let mut _26: *mut u8;
82+
+ scope 34 {
83+
+ scope 35 (inlined ptr::mut_ptr::<impl *mut T>::is_null::runtime_impl) {
84+
+ debug ptr => _26;
85+
+ scope 36 (inlined ptr::mut_ptr::<impl *mut u8>::addr) {
86+
+ debug self => _26;
87+
+ scope 37 {
88+
+ scope 38 (inlined ptr::mut_ptr::<impl *mut u8>::cast::<()>) {
89+
+ debug self => _26;
6690
+ }
6791
+ }
6892
+ }
@@ -74,9 +98,32 @@
7498
+ }
7599
+ }
76100
+ }
77-
+ scope 32 (inlined NonNull::<u8>::as_ptr) {
101+
+ scope 39 (inlined NonNull::<u8>::as_ptr) {
78102
+ debug self => _15;
79-
+ let mut _21: *const u8;
103+
+ let mut _27: bool;
104+
+ let mut _28: bool;
105+
+ let mut _29: *const u8;
106+
+ let mut _30: *const u8;
107+
+ scope 40 {
108+
+ scope 41 (inlined ptr::const_ptr::<impl *const u8>::is_null) {
109+
+ debug self => _29;
110+
+ scope 42 {
111+
+ scope 43 (inlined ptr::const_ptr::<impl *const T>::is_null::runtime_impl) {
112+
+ debug ptr => _29;
113+
+ let mut _31: usize;
114+
+ scope 44 (inlined ptr::const_ptr::<impl *const u8>::addr) {
115+
+ debug self => _29;
116+
+ let mut _32: *const ();
117+
+ scope 45 {
118+
+ scope 46 (inlined ptr::const_ptr::<impl *const u8>::cast::<()>) {
119+
+ debug self => _29;
120+
+ }
121+
+ }
122+
+ }
123+
+ }
124+
+ }
125+
+ }
126+
+ }
80127
+ }
81128
+ }
82129
+ }
@@ -133,6 +180,7 @@
133180
+ StorageLive(_11);
134181
+ StorageLive(_12);
135182
+ StorageLive(_13);
183+
+ StorageLive(_29);
136184
+ StorageLive(_14);
137185
+ _14 = _5 as std::ptr::Alignment (Transmute);
138186
+ _8 = Layout { size: _4, align: move _14 };
@@ -162,24 +210,58 @@
162210
+ StorageLive(_16);
163211
+ StorageLive(_17);
164212
+ StorageLive(_18);
165-
+ _18 = (_11.0: *const [u8]);
166-
+ _17 = move _18 as *mut [u8] (PtrToPtr);
167-
+ StorageDead(_18);
168-
+ _16 = _17 as *mut u8 (PtrToPtr);
169-
+ StorageDead(_17);
170213
+ StorageLive(_19);
171214
+ StorageLive(_20);
172-
+ _19 = _16 as *const u8 (PointerCoercion(MutToConstPointer));
173-
+ _15 = NonNull::<u8> { pointer: _19 };
215+
+ _20 = (_11.0: *const [u8]);
216+
+ StorageLive(_22);
217+
+ _22 = _20 as *const u8 (PtrToPtr);
218+
+ StorageLive(_23);
219+
+ StorageLive(_24);
220+
+ _24 = _22 as *const () (PtrToPtr);
221+
+ _23 = move _24 as usize (Transmute);
222+
+ StorageDead(_24);
223+
+ _19 = Eq(move _23, const 0_usize);
224+
+ StorageDead(_23);
225+
+ StorageDead(_22);
174226
+ StorageDead(_20);
227+
+ _18 = Not(move _19);
175228
+ StorageDead(_19);
176-
+ StorageDead(_16);
229+
+ assume(move _18);
230+
+ StorageDead(_18);
177231
+ StorageLive(_21);
178-
+ _21 = (_15.0: *const u8);
179-
+ _6 = move _21 as *mut u8 (PtrToPtr);
232+
+ _21 = (_11.0: *const [u8]);
233+
+ _17 = move _21 as *mut [u8] (PtrToPtr);
180234
+ StorageDead(_21);
235+
+ _16 = _17 as *mut u8 (PtrToPtr);
236+
+ StorageDead(_17);
237+
+ StorageLive(_25);
238+
+ StorageLive(_26);
239+
+ _25 = _16 as *const u8 (PointerCoercion(MutToConstPointer));
240+
+ _15 = NonNull::<u8> { pointer: _25 };
241+
+ StorageDead(_26);
242+
+ StorageDead(_25);
243+
+ StorageDead(_16);
244+
+ StorageLive(_27);
245+
+ StorageLive(_28);
246+
+ _29 = (_15.0: *const u8);
247+
+ StorageLive(_31);
248+
+ StorageLive(_32);
249+
+ _32 = _29 as *const () (PtrToPtr);
250+
+ _31 = move _32 as usize (Transmute);
251+
+ StorageDead(_32);
252+
+ _28 = Eq(move _31, const 0_usize);
253+
+ StorageDead(_31);
254+
+ _27 = Not(move _28);
255+
+ StorageDead(_28);
256+
+ assume(move _27);
257+
+ StorageDead(_27);
258+
+ StorageLive(_30);
259+
+ _30 = (_15.0: *const u8);
260+
+ _6 = move _30 as *mut u8 (PtrToPtr);
261+
+ StorageDead(_30);
181262
+ StorageDead(_15);
182263
+ StorageDead(_9);
264+
+ StorageDead(_29);
183265
+ StorageDead(_13);
184266
+ StorageDead(_12);
185267
+ StorageDead(_11);

0 commit comments

Comments
 (0)