Skip to content

Commit f5d0592

Browse files
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 7caf35b commit f5d0592

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

library/core/src/ptr/const_ptr.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ impl<T: ?Sized> *const T {
1414
///
1515
/// ## Behavior during const evaluation
1616
///
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.
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 this method might or might not panic. Otherwise, it will not panic.
20+
///
21+
/// This is because CTFE only knows `self`'s offset relative to the memory
22+
/// it initially pointed to, and not its absolute address. This might or
23+
/// might not be sufficient to determine nullness.
2324
///
2425
/// # Examples
2526
///

library/core/src/ptr/mut_ptr.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ impl<T: ?Sized> *mut T {
1414
///
1515
/// ## Behavior during const evaluation
1616
///
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.
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 this method might or might not panic. Otherwise, it will not panic.
20+
///
21+
/// This is because CTFE only knows `self`'s offset relative to the memory
22+
/// it initially pointed to, and not its absolute address. This might or
23+
/// might not be sufficient to determine nullness.
2324
///
2425
/// # Examples
2526
///

0 commit comments

Comments
 (0)