Skip to content

Commit b1ec7aa

Browse files
matthiaskrgrgitbot
authored and
gitbot
committed
Rollup merge of rust-lang#134325 - theemathas:is_null-docs, r=RalfJung
Correctly document CTFE behavior of is_null and methods that call is_null. 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. This is beta-nominated since `const fn is_null` stabilization is in beta already but the docs there are wrong, and it seems better to have the docs be correct at the time of stabilization.
2 parents 5e7f3ec + 25401ef commit b1ec7aa

File tree

3 files changed

+89
-14
lines changed

3 files changed

+89
-14
lines changed

core/src/ptr/const_ptr.rs

+31-7
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
15+
/// # Panics 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 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
///
@@ -254,6 +257,13 @@ impl<T: ?Sized> *const T {
254257
/// When calling this method, you have to ensure that *either* the pointer is null *or*
255258
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
256259
///
260+
/// # Panics during const evaluation
261+
///
262+
/// This method will panic during const evaluation if the pointer cannot be
263+
/// determined to be null or not. See [`is_null`] for more information.
264+
///
265+
/// [`is_null`]: #method.is_null
266+
///
257267
/// # Examples
258268
///
259269
/// ```
@@ -331,6 +341,13 @@ impl<T: ?Sized> *const T {
331341
/// When calling this method, you have to ensure that *either* the pointer is null *or*
332342
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
333343
///
344+
/// # Panics during const evaluation
345+
///
346+
/// This method will panic during const evaluation if the pointer cannot be
347+
/// determined to be null or not. See [`is_null`] for more information.
348+
///
349+
/// [`is_null`]: #method.is_null
350+
///
334351
/// # Examples
335352
///
336353
/// ```
@@ -1607,6 +1624,13 @@ impl<T> *const [T] {
16071624
///
16081625
/// [valid]: crate::ptr#safety
16091626
/// [allocated object]: crate::ptr#allocated-object
1627+
///
1628+
/// # Panics during const evaluation
1629+
///
1630+
/// This method will panic during const evaluation if the pointer cannot be
1631+
/// determined to be null or not. See [`is_null`] for more information.
1632+
///
1633+
/// [`is_null`]: #method.is_null
16101634
#[inline]
16111635
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
16121636
pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {

core/src/ptr/mut_ptr.rs

+51-7
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
15+
/// # Panics 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 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
///
@@ -243,6 +246,13 @@ impl<T: ?Sized> *mut T {
243246
/// When calling this method, you have to ensure that *either* the pointer is null *or*
244247
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
245248
///
249+
/// # Panics during const evaluation
250+
///
251+
/// This method will panic during const evaluation if the pointer cannot be
252+
/// determined to be null or not. See [`is_null`] for more information.
253+
///
254+
/// [`is_null`]: #method.is_null-1
255+
///
246256
/// # Examples
247257
///
248258
/// ```
@@ -327,6 +337,13 @@ impl<T: ?Sized> *mut T {
327337
/// Note that because the created reference is to `MaybeUninit<T>`, the
328338
/// source pointer can point to uninitialized memory.
329339
///
340+
/// # Panics during const evaluation
341+
///
342+
/// This method will panic during const evaluation if the pointer cannot be
343+
/// determined to be null or not. See [`is_null`] for more information.
344+
///
345+
/// [`is_null`]: #method.is_null-1
346+
///
330347
/// # Examples
331348
///
332349
/// ```
@@ -590,6 +607,12 @@ impl<T: ?Sized> *mut T {
590607
/// the pointer is null *or*
591608
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
592609
///
610+
/// # Panics during const evaluation
611+
///
612+
/// This method will panic during const evaluation if the pointer cannot be
613+
/// determined to be null or not. See [`is_null`] for more information.
614+
///
615+
/// [`is_null`]: #method.is_null-1
593616
///
594617
/// # Examples
595618
///
@@ -673,6 +696,13 @@ impl<T: ?Sized> *mut T {
673696
///
674697
/// When calling this method, you have to ensure that *either* the pointer is null *or*
675698
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
699+
///
700+
/// # Panics during const evaluation
701+
///
702+
/// This method will panic during const evaluation if the pointer cannot be
703+
/// determined to be null or not. See [`is_null`] for more information.
704+
///
705+
/// [`is_null`]: #method.is_null-1
676706
#[inline]
677707
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
678708
pub const unsafe fn as_uninit_mut<'a>(self) -> Option<&'a mut MaybeUninit<T>>
@@ -1949,6 +1979,13 @@ impl<T> *mut [T] {
19491979
///
19501980
/// [valid]: crate::ptr#safety
19511981
/// [allocated object]: crate::ptr#allocated-object
1982+
///
1983+
/// # Panics during const evaluation
1984+
///
1985+
/// This method will panic during const evaluation if the pointer cannot be
1986+
/// determined to be null or not. See [`is_null`] for more information.
1987+
///
1988+
/// [`is_null`]: #method.is_null-1
19521989
#[inline]
19531990
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
19541991
pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
@@ -2000,6 +2037,13 @@ impl<T> *mut [T] {
20002037
///
20012038
/// [valid]: crate::ptr#safety
20022039
/// [allocated object]: crate::ptr#allocated-object
2040+
///
2041+
/// # Panics during const evaluation
2042+
///
2043+
/// This method will panic during const evaluation if the pointer cannot be
2044+
/// determined to be null or not. See [`is_null`] for more information.
2045+
///
2046+
/// [`is_null`]: #method.is_null-1
20032047
#[inline]
20042048
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
20052049
pub const unsafe fn as_uninit_slice_mut<'a>(self) -> Option<&'a mut [MaybeUninit<T>]> {

core/src/ptr/non_null.rs

+7
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ impl<T: ?Sized> NonNull<T> {
204204

205205
/// Creates a new `NonNull` if `ptr` is non-null.
206206
///
207+
/// # Panics during const evaluation
208+
///
209+
/// This method will panic during const evaluation if the pointer cannot be
210+
/// determined to be null or not. See [`is_null`] for more information.
211+
///
212+
/// [`is_null`]: ../primitive.pointer.html#method.is_null-1
213+
///
207214
/// # Examples
208215
///
209216
/// ```

0 commit comments

Comments
 (0)