Rollup merge of #130107 - RalfJung:const-ptr-is-null, r=oli-obk · qinheping/verify-rust-std@e8d9b85 (original) (raw)

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -40,15 +40,17 @@ impl<T: ?Sized> *const T {
40 40
41 41 #[inline]
42 42 const fn const_impl(ptr: *const u8) -> bool {
43 -// Compare via a cast to a thin pointer, so fat pointers are only
44 -// considering their "data" part for null-ness.
45 43 match (ptr).guaranteed_eq(null_mut()) {
46 -None => false,
47 44 Some(res) => res,
45 +// To remain maximally convervative, we stop execution when we don't
46 +// know whether the pointer is null or not.
47 +// We can *not* return `false` here, that would be unsound in `NonNull::new`!
48 +None => panic!("null-ness of this pointer cannot be determined in const context"),
48 49 }
49 50 }
50 51
51 -#[allow(unused_unsafe)]
52 +// Compare via a cast to a thin pointer, so fat pointers are only
53 +// considering their "data" part for null-ness.
52 54 const_eval_select((self as *const u8,), const_impl, runtime_impl)
53 55 }
54 56
Original file line number Diff line number Diff line change
@@ -33,22 +33,7 @@ impl<T: ?Sized> *mut T {
33 33 #[rustc_diagnostic_item = "ptr_is_null"]
34 34 #[inline]
35 35 pub const fn is_null(self) -> bool {
36 -#[inline]
37 -fn runtime_impl(ptr: *mut u8) -> bool {
38 - ptr.addr() == 0
39 -}
40 -
41 -#[inline]
42 -const fn const_impl(ptr: *mut u8) -> bool {
43 -// Compare via a cast to a thin pointer, so fat pointers are only
44 -// considering their "data" part for null-ness.
45 -match (ptr).guaranteed_eq(null_mut()) {
46 -None => false,
47 -Some(res) => res,
48 -}
49 -}
50 -
51 -const_eval_select((self as *mut u8,), const_impl, runtime_impl)
36 +self.cast_const().is_null()
52 37 }
53 38
54 39 /// Casts to a pointer of another type.