Skip to content

Commit 8be6719

Browse files
committed
Auto merge of rust-lang#74940 - oli-obk:const_is_null, r=RalfJung
Make `<*const T>::is_null` const fn r? @RalfJung cc @rust-lang/wg-const-eval tracking issue: rust-lang#74939
2 parents f67d3e0 + c7637d7 commit 8be6719

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

core/src/ptr/const_ptr.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ impl<T: ?Sized> *const T {
1313
/// Therefore, two pointers that are null may still not compare equal to
1414
/// each other.
1515
///
16+
/// ## Behavior during const evaluation
17+
///
18+
/// When this function is used during const evaluation, it may return `false` for pointers
19+
/// that turn out to be null at runtime. Specifically, when a pointer to some memory
20+
/// is offset beyond its bounds in such a way that the resulting pointer is null,
21+
/// the function will still return `false`. There is no way for CTFE to know
22+
/// the absolute position of that memory, so we cannot tell if the pointer is
23+
/// null or not.
24+
///
1625
/// # Examples
1726
///
1827
/// Basic usage:
@@ -23,11 +32,12 @@ impl<T: ?Sized> *const T {
2332
/// assert!(!ptr.is_null());
2433
/// ```
2534
#[stable(feature = "rust1", since = "1.0.0")]
35+
#[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
2636
#[inline]
27-
pub fn is_null(self) -> bool {
37+
pub const fn is_null(self) -> bool {
2838
// Compare via a cast to a thin pointer, so fat pointers are only
2939
// considering their "data" part for null-ness.
30-
(self as *const u8) == null()
40+
(self as *const u8).guaranteed_eq(null())
3141
}
3242

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

core/src/ptr/mut_ptr.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ impl<T: ?Sized> *mut T {
1212
/// Therefore, two pointers that are null may still not compare equal to
1313
/// each other.
1414
///
15+
/// ## Behavior during const evaluation
16+
///
17+
/// When this function is used during const evaluation, it may return `false` for pointers
18+
/// that turn out to be null at runtime. Specifically, when a pointer to some memory
19+
/// is offset beyond its bounds in such a way that the resulting pointer is null,
20+
/// the function will still return `false`. There is no way for CTFE to know
21+
/// the absolute position of that memory, so we cannot tell if the pointer is
22+
/// null or not.
23+
///
1524
/// # Examples
1625
///
1726
/// Basic usage:
@@ -22,11 +31,12 @@ impl<T: ?Sized> *mut T {
2231
/// assert!(!ptr.is_null());
2332
/// ```
2433
#[stable(feature = "rust1", since = "1.0.0")]
34+
#[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
2535
#[inline]
26-
pub fn is_null(self) -> bool {
36+
pub const fn is_null(self) -> bool {
2737
// Compare via a cast to a thin pointer, so fat pointers are only
2838
// considering their "data" part for null-ness.
29-
(self as *mut u8) == null_mut()
39+
(self as *mut u8).guaranteed_eq(null_mut())
3040
}
3141

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

0 commit comments

Comments
 (0)