From 0af922b40490c1997ab365c4cd9f2f8adce28ae6 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Wed, 7 Dec 2022 19:38:25 -0500 Subject: [PATCH] Use const_eval_select in ptr::is_null --- library/core/src/ptr/const_ptr.rs | 20 ++++++++++++++++---- library/core/src/ptr/mut_ptr.rs | 20 ++++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index b6373beac5f76..327b8a8762c50 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -34,12 +34,24 @@ impl *const T { #[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")] #[inline] pub const fn is_null(self) -> bool { + #[inline] + const fn ct(ptr: *const u8) -> bool { + match (ptr).guaranteed_eq(null()) { + None => false, + Some(res) => res, + } + } + + #[inline] + fn rt(ptr: *const u8) -> bool { + ptr.addr() == 0 + } + // Compare via a cast to a thin pointer, so fat pointers are only // considering their "data" part for null-ness. - match (self as *const u8).guaranteed_eq(null()) { - None => false, - Some(res) => res, - } + + // SAFETY: NOYET + unsafe { intrinsics::const_eval_select((self as *const u8,), ct, rt) } } /// Casts to a pointer of another type. diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index 7a5d9a7059459..16beb1f77fe0a 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -33,12 +33,24 @@ impl *mut T { #[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")] #[inline] pub const fn is_null(self) -> bool { + #[inline] + const fn ct(ptr: *mut u8) -> bool { + match (ptr).guaranteed_eq(null_mut()) { + None => false, + Some(res) => res, + } + } + + #[inline] + fn rt(ptr: *mut u8) -> bool { + ptr.addr() == 0 + } + // Compare via a cast to a thin pointer, so fat pointers are only // considering their "data" part for null-ness. - match (self as *mut u8).guaranteed_eq(null_mut()) { - None => false, - Some(res) => res, - } + + // SAFETY: NOYET + unsafe { intrinsics::const_eval_select((self as *mut u8,), ct, rt) } } /// Casts to a pointer of another type.