Skip to content

Commit 0af922b

Browse files
committed
Use const_eval_select in ptr::is_null
1 parent d0dc9ef commit 0af922b

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

library/core/src/ptr/const_ptr.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,24 @@ impl<T: ?Sized> *const T {
3434
#[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
3535
#[inline]
3636
pub const fn is_null(self) -> bool {
37+
#[inline]
38+
const fn ct(ptr: *const u8) -> bool {
39+
match (ptr).guaranteed_eq(null()) {
40+
None => false,
41+
Some(res) => res,
42+
}
43+
}
44+
45+
#[inline]
46+
fn rt(ptr: *const u8) -> bool {
47+
ptr.addr() == 0
48+
}
49+
3750
// Compare via a cast to a thin pointer, so fat pointers are only
3851
// considering their "data" part for null-ness.
39-
match (self as *const u8).guaranteed_eq(null()) {
40-
None => false,
41-
Some(res) => res,
42-
}
52+
53+
// SAFETY: NOYET
54+
unsafe { intrinsics::const_eval_select((self as *const u8,), ct, rt) }
4355
}
4456

4557
/// Casts to a pointer of another type.

library/core/src/ptr/mut_ptr.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,24 @@ impl<T: ?Sized> *mut T {
3333
#[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
3434
#[inline]
3535
pub const fn is_null(self) -> bool {
36+
#[inline]
37+
const fn ct(ptr: *mut u8) -> bool {
38+
match (ptr).guaranteed_eq(null_mut()) {
39+
None => false,
40+
Some(res) => res,
41+
}
42+
}
43+
44+
#[inline]
45+
fn rt(ptr: *mut u8) -> bool {
46+
ptr.addr() == 0
47+
}
48+
3649
// Compare via a cast to a thin pointer, so fat pointers are only
3750
// considering their "data" part for null-ness.
38-
match (self as *mut u8).guaranteed_eq(null_mut()) {
39-
None => false,
40-
Some(res) => res,
41-
}
51+
52+
// SAFETY: NOYET
53+
unsafe { intrinsics::const_eval_select((self as *mut u8,), ct, rt) }
4254
}
4355

4456
/// Casts to a pointer of another type.

0 commit comments

Comments
 (0)