Skip to content

Commit cd6be0c

Browse files
committed
Add assume statements in NonZero library types.
1 parent c2354aa commit cd6be0c

6 files changed

+174
-13
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
@@ -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_assume, 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

+42-3
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) {
@@ -105,10 +128,26 @@
105128
StorageDead(_15);
106129
StorageDead(_12);
107130
StorageDead(_6);
131+
StorageLive(_20);
108132
StorageLive(_18);
109-
_18 = (_5.0: *const [u8]);
110-
_4 = move _18 as *mut [u8] (PtrToPtr);
133+
StorageLive(_19);
134+
_20 = (_5.0: *const [u8]);
135+
StorageLive(_21);
136+
_21 = _20 as *const u8 (PtrToPtr);
137+
StorageLive(_22);
138+
StorageLive(_23);
139+
_23 = _21 as *const () (PtrToPtr);
140+
_22 = move _23 as usize (Transmute);
141+
StorageDead(_23);
142+
_19 = Eq(move _22, const 0_usize);
143+
StorageDead(_22);
144+
StorageDead(_21);
145+
_18 = Not(move _19);
146+
StorageDead(_19);
147+
assume(move _18);
111148
StorageDead(_18);
149+
_4 = _20 as *mut [u8] (PtrToPtr);
150+
StorageDead(_20);
112151
StorageDead(_5);
113152
_3 = move _4 as *mut u8 (PtrToPtr);
114153
StorageDead(_4);

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

+42-3
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

+42-3
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) {
@@ -105,10 +128,26 @@
105128
StorageDead(_15);
106129
StorageDead(_12);
107130
StorageDead(_6);
131+
StorageLive(_20);
108132
StorageLive(_18);
109-
_18 = (_5.0: *const [u8]);
110-
_4 = move _18 as *mut [u8] (PtrToPtr);
133+
StorageLive(_19);
134+
_20 = (_5.0: *const [u8]);
135+
StorageLive(_21);
136+
_21 = _20 as *const u8 (PtrToPtr);
137+
StorageLive(_22);
138+
StorageLive(_23);
139+
_23 = _21 as *const () (PtrToPtr);
140+
_22 = move _23 as usize (Transmute);
141+
StorageDead(_23);
142+
_19 = Eq(move _22, const 0_usize);
143+
StorageDead(_22);
144+
StorageDead(_21);
145+
_18 = Not(move _19);
146+
StorageDead(_19);
147+
assume(move _18);
111148
StorageDead(_18);
149+
_4 = _20 as *mut [u8] (PtrToPtr);
150+
StorageDead(_20);
112151
StorageDead(_5);
113152
_3 = move _4 as *mut u8 (PtrToPtr);
114153
StorageDead(_4);

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

+42-3
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)