Skip to content

Commit 5e5a3c5

Browse files
theemathasgitbot
authored and
gitbot
committed
Correctly document is_null CTFE behavior.
The "panic in const if CTFE doesn't know the answer" behavior was discussed to be the desired behavior in rust-lang#74939, and is currently how the function actually behaves. I intentionally wrote this documentation to allow for the possibility that a panic might not occur even if the pointer is out of bounds, because of rust-lang#133700 and other potential changes in the future.
1 parent 893b7ce commit 5e5a3c5

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

core/src/ptr/const_ptr.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ impl<T: ?Sized> *const 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.
15+
/// # Panics during const evaluation
16+
///
17+
/// If this method is used during const evaluation, and `self` is a pointer
18+
/// that is offset beyond the bounds of the memory it initially pointed to,
19+
/// then there might not be enough information to determine whether the
20+
/// pointer is null. This is because the absolute address in memory is not
21+
/// known at compile time. If the nullness of the pointer cannot be
22+
/// determined, this method will panic.
23+
///
24+
/// In-bounds pointers are never null, so the method will never panic for
25+
/// such pointers.
2326
///
2427
/// # Examples
2528
///

core/src/ptr/mut_ptr.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ 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.
15+
/// # Panics during const evaluation
16+
///
17+
/// If this method is used during const evaluation, and `self` is a pointer
18+
/// that is offset beyond the bounds of the memory it initially pointed to,
19+
/// then there might not be enough information to determine whether the
20+
/// pointer is null. This is because the absolute address in memory is not
21+
/// known at compile time. If the nullness of the pointer cannot be
22+
/// determined, this method will panic.
23+
///
24+
/// In-bounds pointers are never null, so the method will never panic for
25+
/// such pointers.
2326
///
2427
/// # Examples
2528
///

0 commit comments

Comments
 (0)