File tree Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,15 @@ impl<T: ?Sized> *const T {
13
13
/// Therefore, two pointers that are null may still not compare equal to
14
14
/// each other.
15
15
///
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
+ ///
16
25
/// # Examples
17
26
///
18
27
/// Basic usage:
@@ -23,11 +32,12 @@ impl<T: ?Sized> *const T {
23
32
/// assert!(!ptr.is_null());
24
33
/// ```
25
34
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
35
+ #[ rustc_const_unstable( feature = "const_ptr_is_null" , issue = "74939" ) ]
26
36
#[ inline]
27
- pub fn is_null ( self ) -> bool {
37
+ pub const fn is_null ( self ) -> bool {
28
38
// Compare via a cast to a thin pointer, so fat pointers are only
29
39
// considering their "data" part for null-ness.
30
- ( self as * const u8 ) == null ( )
40
+ ( self as * const u8 ) . guaranteed_eq ( null ( ) )
31
41
}
32
42
33
43
/// Casts to a pointer of another type.
Original file line number Diff line number Diff line change @@ -12,6 +12,15 @@ impl<T: ?Sized> *mut T {
12
12
/// Therefore, two pointers that are null may still not compare equal to
13
13
/// each other.
14
14
///
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
+ ///
15
24
/// # Examples
16
25
///
17
26
/// Basic usage:
@@ -22,11 +31,12 @@ impl<T: ?Sized> *mut T {
22
31
/// assert!(!ptr.is_null());
23
32
/// ```
24
33
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
34
+ #[ rustc_const_unstable( feature = "const_ptr_is_null" , issue = "74939" ) ]
25
35
#[ inline]
26
- pub fn is_null ( self ) -> bool {
36
+ pub const fn is_null ( self ) -> bool {
27
37
// Compare via a cast to a thin pointer, so fat pointers are only
28
38
// considering their "data" part for null-ness.
29
- ( self as * mut u8 ) == null_mut ( )
39
+ ( self as * mut u8 ) . guaranteed_eq ( null_mut ( ) )
30
40
}
31
41
32
42
/// Casts to a pointer of another type.
You can’t perform that action at this time.
0 commit comments