Rollup merge of #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 #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 #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.
This commit is contained in:
commit
3aedae24a2
3 changed files with 89 additions and 14 deletions
|
@ -12,14 +12,17 @@ impl<T: ?Sized> *const T {
|
||||||
/// Therefore, two pointers that are null may still not compare equal to
|
/// Therefore, two pointers that are null may still not compare equal to
|
||||||
/// each other.
|
/// each other.
|
||||||
///
|
///
|
||||||
/// ## Behavior during const evaluation
|
/// # Panics during const evaluation
|
||||||
///
|
///
|
||||||
/// When this function is used during const evaluation, it may return `false` for pointers
|
/// If this method is used during const evaluation, and `self` is a pointer
|
||||||
/// that turn out to be null at runtime. Specifically, when a pointer to some memory
|
/// that is offset beyond the bounds of the memory it initially pointed to,
|
||||||
/// is offset beyond its bounds in such a way that the resulting pointer is null,
|
/// then there might not be enough information to determine whether the
|
||||||
/// the function will still return `false`. There is no way for CTFE to know
|
/// pointer is null. This is because the absolute address in memory is not
|
||||||
/// the absolute position of that memory, so we cannot tell if the pointer is
|
/// known at compile time. If the nullness of the pointer cannot be
|
||||||
/// null or not.
|
/// determined, this method will panic.
|
||||||
|
///
|
||||||
|
/// In-bounds pointers are never null, so the method will never panic for
|
||||||
|
/// such pointers.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
@ -254,6 +257,13 @@ impl<T: ?Sized> *const T {
|
||||||
/// When calling this method, you have to ensure that *either* the pointer is null *or*
|
/// When calling this method, you have to ensure that *either* the pointer is null *or*
|
||||||
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
|
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
|
||||||
///
|
///
|
||||||
|
/// # Panics during const evaluation
|
||||||
|
///
|
||||||
|
/// This method will panic during const evaluation if the pointer cannot be
|
||||||
|
/// determined to be null or not. See [`is_null`] for more information.
|
||||||
|
///
|
||||||
|
/// [`is_null`]: #method.is_null
|
||||||
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -331,6 +341,13 @@ impl<T: ?Sized> *const T {
|
||||||
/// When calling this method, you have to ensure that *either* the pointer is null *or*
|
/// When calling this method, you have to ensure that *either* the pointer is null *or*
|
||||||
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
|
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
|
||||||
///
|
///
|
||||||
|
/// # Panics during const evaluation
|
||||||
|
///
|
||||||
|
/// This method will panic during const evaluation if the pointer cannot be
|
||||||
|
/// determined to be null or not. See [`is_null`] for more information.
|
||||||
|
///
|
||||||
|
/// [`is_null`]: #method.is_null
|
||||||
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -1607,6 +1624,13 @@ impl<T> *const [T] {
|
||||||
///
|
///
|
||||||
/// [valid]: crate::ptr#safety
|
/// [valid]: crate::ptr#safety
|
||||||
/// [allocated object]: crate::ptr#allocated-object
|
/// [allocated object]: crate::ptr#allocated-object
|
||||||
|
///
|
||||||
|
/// # Panics during const evaluation
|
||||||
|
///
|
||||||
|
/// This method will panic during const evaluation if the pointer cannot be
|
||||||
|
/// determined to be null or not. See [`is_null`] for more information.
|
||||||
|
///
|
||||||
|
/// [`is_null`]: #method.is_null
|
||||||
#[inline]
|
#[inline]
|
||||||
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
|
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
|
||||||
pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
|
pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
|
||||||
|
|
|
@ -12,14 +12,17 @@ impl<T: ?Sized> *mut T {
|
||||||
/// Therefore, two pointers that are null may still not compare equal to
|
/// Therefore, two pointers that are null may still not compare equal to
|
||||||
/// each other.
|
/// each other.
|
||||||
///
|
///
|
||||||
/// ## Behavior during const evaluation
|
/// # Panics during const evaluation
|
||||||
///
|
///
|
||||||
/// When this function is used during const evaluation, it may return `false` for pointers
|
/// If this method is used during const evaluation, and `self` is a pointer
|
||||||
/// that turn out to be null at runtime. Specifically, when a pointer to some memory
|
/// that is offset beyond the bounds of the memory it initially pointed to,
|
||||||
/// is offset beyond its bounds in such a way that the resulting pointer is null,
|
/// then there might not be enough information to determine whether the
|
||||||
/// the function will still return `false`. There is no way for CTFE to know
|
/// pointer is null. This is because the absolute address in memory is not
|
||||||
/// the absolute position of that memory, so we cannot tell if the pointer is
|
/// known at compile time. If the nullness of the pointer cannot be
|
||||||
/// null or not.
|
/// determined, this method will panic.
|
||||||
|
///
|
||||||
|
/// In-bounds pointers are never null, so the method will never panic for
|
||||||
|
/// such pointers.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
@ -243,6 +246,13 @@ impl<T: ?Sized> *mut T {
|
||||||
/// When calling this method, you have to ensure that *either* the pointer is null *or*
|
/// When calling this method, you have to ensure that *either* the pointer is null *or*
|
||||||
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
|
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
|
||||||
///
|
///
|
||||||
|
/// # Panics during const evaluation
|
||||||
|
///
|
||||||
|
/// This method will panic during const evaluation if the pointer cannot be
|
||||||
|
/// determined to be null or not. See [`is_null`] for more information.
|
||||||
|
///
|
||||||
|
/// [`is_null`]: #method.is_null-1
|
||||||
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -327,6 +337,13 @@ impl<T: ?Sized> *mut T {
|
||||||
/// Note that because the created reference is to `MaybeUninit<T>`, the
|
/// Note that because the created reference is to `MaybeUninit<T>`, the
|
||||||
/// source pointer can point to uninitialized memory.
|
/// source pointer can point to uninitialized memory.
|
||||||
///
|
///
|
||||||
|
/// # Panics during const evaluation
|
||||||
|
///
|
||||||
|
/// This method will panic during const evaluation if the pointer cannot be
|
||||||
|
/// determined to be null or not. See [`is_null`] for more information.
|
||||||
|
///
|
||||||
|
/// [`is_null`]: #method.is_null-1
|
||||||
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -590,6 +607,12 @@ impl<T: ?Sized> *mut T {
|
||||||
/// the pointer is null *or*
|
/// the pointer is null *or*
|
||||||
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
|
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
|
||||||
///
|
///
|
||||||
|
/// # Panics during const evaluation
|
||||||
|
///
|
||||||
|
/// This method will panic during const evaluation if the pointer cannot be
|
||||||
|
/// determined to be null or not. See [`is_null`] for more information.
|
||||||
|
///
|
||||||
|
/// [`is_null`]: #method.is_null-1
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
@ -673,6 +696,13 @@ impl<T: ?Sized> *mut T {
|
||||||
///
|
///
|
||||||
/// When calling this method, you have to ensure that *either* the pointer is null *or*
|
/// When calling this method, you have to ensure that *either* the pointer is null *or*
|
||||||
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
|
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
|
||||||
|
///
|
||||||
|
/// # Panics during const evaluation
|
||||||
|
///
|
||||||
|
/// This method will panic during const evaluation if the pointer cannot be
|
||||||
|
/// determined to be null or not. See [`is_null`] for more information.
|
||||||
|
///
|
||||||
|
/// [`is_null`]: #method.is_null-1
|
||||||
#[inline]
|
#[inline]
|
||||||
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
|
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
|
||||||
pub const unsafe fn as_uninit_mut<'a>(self) -> Option<&'a mut MaybeUninit<T>>
|
pub const unsafe fn as_uninit_mut<'a>(self) -> Option<&'a mut MaybeUninit<T>>
|
||||||
|
@ -1949,6 +1979,13 @@ impl<T> *mut [T] {
|
||||||
///
|
///
|
||||||
/// [valid]: crate::ptr#safety
|
/// [valid]: crate::ptr#safety
|
||||||
/// [allocated object]: crate::ptr#allocated-object
|
/// [allocated object]: crate::ptr#allocated-object
|
||||||
|
///
|
||||||
|
/// # Panics during const evaluation
|
||||||
|
///
|
||||||
|
/// This method will panic during const evaluation if the pointer cannot be
|
||||||
|
/// determined to be null or not. See [`is_null`] for more information.
|
||||||
|
///
|
||||||
|
/// [`is_null`]: #method.is_null-1
|
||||||
#[inline]
|
#[inline]
|
||||||
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
|
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
|
||||||
pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
|
pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
|
||||||
|
@ -2000,6 +2037,13 @@ impl<T> *mut [T] {
|
||||||
///
|
///
|
||||||
/// [valid]: crate::ptr#safety
|
/// [valid]: crate::ptr#safety
|
||||||
/// [allocated object]: crate::ptr#allocated-object
|
/// [allocated object]: crate::ptr#allocated-object
|
||||||
|
///
|
||||||
|
/// # Panics during const evaluation
|
||||||
|
///
|
||||||
|
/// This method will panic during const evaluation if the pointer cannot be
|
||||||
|
/// determined to be null or not. See [`is_null`] for more information.
|
||||||
|
///
|
||||||
|
/// [`is_null`]: #method.is_null-1
|
||||||
#[inline]
|
#[inline]
|
||||||
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
|
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
|
||||||
pub const unsafe fn as_uninit_slice_mut<'a>(self) -> Option<&'a mut [MaybeUninit<T>]> {
|
pub const unsafe fn as_uninit_slice_mut<'a>(self) -> Option<&'a mut [MaybeUninit<T>]> {
|
||||||
|
|
|
@ -204,6 +204,13 @@ impl<T: ?Sized> NonNull<T> {
|
||||||
|
|
||||||
/// Creates a new `NonNull` if `ptr` is non-null.
|
/// Creates a new `NonNull` if `ptr` is non-null.
|
||||||
///
|
///
|
||||||
|
/// # Panics during const evaluation
|
||||||
|
///
|
||||||
|
/// This method will panic during const evaluation if the pointer cannot be
|
||||||
|
/// determined to be null or not. See [`is_null`] for more information.
|
||||||
|
///
|
||||||
|
/// [`is_null`]: ../primitive.pointer.html#method.is_null-1
|
||||||
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue