about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeón Orell Valerian Liehr <me@fmease.dev>2024-10-23 22:11:06 +0200
committerGitHub <noreply@github.com>2024-10-23 22:11:06 +0200
commitb0a8e4e030f10f3834bdf906938025e8718a2706 (patch)
tree379d36e7529c6e7f65e635d8ddacdb11e0181ce7
parent8b1141a5c3c5877c02c9c7b388970482fca0ac5d (diff)
parentf4c8ff33de0cd9ba5b465356c00908d817cb5c0e (diff)
Rollup merge of #132065 - tifv:dangling-docs, r=Noratrieb
Clarify documentation of `ptr::dangling()` function

Also fixes the safety comment in `NonNull::dangling()` function.

Fixes #132004.
-rw-r--r--library/core/src/ptr/mod.rs4
-rw-r--r--library/core/src/ptr/non_null.rs4
2 files changed, 3 insertions, 5 deletions
diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs
index f769b515877..ea185f0fbe5 100644
--- a/library/core/src/ptr/mod.rs
+++ b/library/core/src/ptr/mod.rs
@@ -602,7 +602,7 @@ pub const fn without_provenance<T>(addr: usize) -> *const T {
     unsafe { mem::transmute(addr) }
 }
 
-/// Creates a new pointer that is dangling, but well-aligned.
+/// Creates a new pointer that is dangling, but non-null and well-aligned.
 ///
 /// This is useful for initializing types which lazily allocate, like
 /// `Vec::new` does.
@@ -645,7 +645,7 @@ pub const fn without_provenance_mut<T>(addr: usize) -> *mut T {
     unsafe { mem::transmute(addr) }
 }
 
-/// Creates a new pointer that is dangling, but well-aligned.
+/// Creates a new pointer that is dangling, but non-null and well-aligned.
 ///
 /// This is useful for initializing types which lazily allocate, like
 /// `Vec::new` does.
diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs
index d91bbe1a5a1..d80e1e700aa 100644
--- a/library/core/src/ptr/non_null.rs
+++ b/library/core/src/ptr/non_null.rs
@@ -107,9 +107,7 @@ impl<T: Sized> NonNull<T> {
     #[must_use]
     #[inline]
     pub const fn dangling() -> Self {
-        // SAFETY: mem::align_of() returns a non-zero usize which is then casted
-        // to a *mut T. Therefore, `ptr` is not null and the conditions for
-        // calling new_unchecked() are respected.
+        // SAFETY: ptr::dangling_mut() returns a non-null well-aligned pointer.
         unsafe {
             let ptr = crate::ptr::dangling_mut::<T>();
             NonNull::new_unchecked(ptr)