Skip to content

Commit 02a77d8

Browse files
authored
Rollup merge of rust-lang#71507 - CohenArthur:document-unsafe-libcore-ptr, r=Mark-Simulacrum
Document unsafety in core::ptr Contributes to rust-lang#66219 I have yet to document all the `unsafe` blocks in the lib and would like to know if I'm headed in the right direction r? @steveklabnik
2 parents e91aebc + 8558ccd commit 02a77d8

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

src/libcore/ptr/mod.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@
6565
//! [`write_volatile`]: ./fn.write_volatile.html
6666
//! [`NonNull::dangling`]: ./struct.NonNull.html#method.dangling
6767
68-
// ignore-tidy-undocumented-unsafe
69-
7068
#![stable(feature = "rust1", since = "1.0.0")]
7169

7270
use crate::cmp::Ordering;
@@ -248,14 +246,17 @@ pub(crate) struct FatPtr<T> {
248246
///
249247
/// // create a slice pointer when starting out with a pointer to the first element
250248
/// let x = [5, 6, 7];
251-
/// let ptr = x.as_ptr();
252-
/// let slice = ptr::slice_from_raw_parts(ptr, 3);
249+
/// let raw_pointer = x.as_ptr();
250+
/// let slice = ptr::slice_from_raw_parts(raw_pointer, 3);
253251
/// assert_eq!(unsafe { &*slice }[2], 7);
254252
/// ```
255253
#[inline]
256254
#[stable(feature = "slice_from_raw_parts", since = "1.42.0")]
257255
#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
258256
pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
257+
// SAFETY: Accessing the value from the `Repr` union is safe since *const [T]
258+
// and FatPtr have the same memory layouts. Only std can make this
259+
// guarantee.
259260
unsafe { Repr { raw: FatPtr { data, len } }.rust }
260261
}
261262

@@ -269,10 +270,28 @@ pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
269270
///
270271
/// [`slice_from_raw_parts`]: fn.slice_from_raw_parts.html
271272
/// [`from_raw_parts_mut`]: ../../std/slice/fn.from_raw_parts_mut.html
273+
///
274+
/// # Examples
275+
///
276+
/// ```rust
277+
/// use std::ptr;
278+
///
279+
/// let x = &mut [5, 6, 7];
280+
/// let raw_pointer = x.as_mut_ptr();
281+
/// let slice = ptr::slice_from_raw_parts_mut(raw_pointer, 3);
282+
///
283+
/// unsafe {
284+
/// (*slice)[2] = 99; // assign a value at an index in the slice
285+
/// };
286+
///
287+
/// assert_eq!(unsafe { &*slice }[2], 99);
288+
/// ```
272289
#[inline]
273290
#[stable(feature = "slice_from_raw_parts", since = "1.42.0")]
274291
#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")]
275292
pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
293+
// SAFETY: Accessing the value from the `Repr` union is safe since *mut [T]
294+
// and FatPtr have the same memory layouts
276295
unsafe { Repr { raw: FatPtr { data, len } }.rust_mut }
277296
}
278297

src/libcore/ptr/non_null.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use crate::mem;
77
use crate::ops::{CoerceUnsized, DispatchFromDyn};
88
use crate::ptr::Unique;
99

10-
// ignore-tidy-undocumented-unsafe
11-
1210
/// `*mut T` but non-zero and covariant.
1311
///
1412
/// This is often the correct thing to use when building data structures using
@@ -69,6 +67,9 @@ impl<T: Sized> NonNull<T> {
6967
#[rustc_const_stable(feature = "const_nonnull_dangling", since = "1.32.0")]
7068
#[inline]
7169
pub const fn dangling() -> Self {
70+
// SAFETY: mem::align_of() returns a non-zero usize which is then casted
71+
// to a *mut T. Therefore, `ptr` is not null and the conditions for
72+
// calling new_unchecked() are respected.
7273
unsafe {
7374
let ptr = mem::align_of::<T>() as *mut T;
7475
NonNull::new_unchecked(ptr)
@@ -93,7 +94,12 @@ impl<T: ?Sized> NonNull<T> {
9394
#[stable(feature = "nonnull", since = "1.25.0")]
9495
#[inline]
9596
pub fn new(ptr: *mut T) -> Option<Self> {
96-
if !ptr.is_null() { Some(unsafe { Self::new_unchecked(ptr) }) } else { None }
97+
if !ptr.is_null() {
98+
// SAFETY: The pointer is already checked and is not null
99+
Some(unsafe { Self::new_unchecked(ptr) })
100+
} else {
101+
None
102+
}
97103
}
98104

99105
/// Acquires the underlying `*mut` pointer.
@@ -131,6 +137,7 @@ impl<T: ?Sized> NonNull<T> {
131137
#[rustc_const_stable(feature = "const_nonnull_cast", since = "1.32.0")]
132138
#[inline]
133139
pub const fn cast<U>(self) -> NonNull<U> {
140+
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
134141
unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) }
135142
}
136143
}
@@ -205,6 +212,8 @@ impl<T: ?Sized> hash::Hash for NonNull<T> {
205212
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
206213
#[inline]
207214
fn from(unique: Unique<T>) -> Self {
215+
// SAFETY: A Unique pointer cannot be null, so the conditions for
216+
// new_unchecked() are respected.
208217
unsafe { NonNull::new_unchecked(unique.as_ptr()) }
209218
}
210219
}
@@ -213,6 +222,7 @@ impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
213222
impl<T: ?Sized> From<&mut T> for NonNull<T> {
214223
#[inline]
215224
fn from(reference: &mut T) -> Self {
225+
// SAFETY: A mutable reference cannot be null.
216226
unsafe { NonNull { pointer: reference as *mut T } }
217227
}
218228
}
@@ -221,6 +231,8 @@ impl<T: ?Sized> From<&mut T> for NonNull<T> {
221231
impl<T: ?Sized> From<&T> for NonNull<T> {
222232
#[inline]
223233
fn from(reference: &T) -> Self {
234+
// SAFETY: A reference cannot be null, so the conditions for
235+
// new_unchecked() are respected.
224236
unsafe { NonNull { pointer: reference as *const T } }
225237
}
226238
}

src/libcore/ptr/unique.rs

+7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ impl<T: Sized> Unique<T> {
7373
// FIXME: rename to dangling() to match NonNull?
7474
#[inline]
7575
pub const fn empty() -> Self {
76+
// SAFETY: mem::align_of() returns a valid, non-null pointer. The
77+
// conditions to call new_unchecked() are thus respected.
7678
unsafe { Unique::new_unchecked(mem::align_of::<T>() as *mut T) }
7779
}
7880
}
@@ -93,6 +95,7 @@ impl<T: ?Sized> Unique<T> {
9395
#[inline]
9496
pub fn new(ptr: *mut T) -> Option<Self> {
9597
if !ptr.is_null() {
98+
// SAFETY: The pointer has already been checked and is not null.
9699
Some(unsafe { Unique { pointer: ptr as _, _marker: PhantomData } })
97100
} else {
98101
None
@@ -128,6 +131,9 @@ impl<T: ?Sized> Unique<T> {
128131
/// Casts to a pointer of another type.
129132
#[inline]
130133
pub const fn cast<U>(self) -> Unique<U> {
134+
// SAFETY: Unique::new_unchecked() creates a new unique and needs
135+
// the given pointer to not be null.
136+
// Since we are passing self as a pointer, it cannot be null.
131137
unsafe { Unique::new_unchecked(self.as_ptr() as *mut U) }
132138
}
133139
}
@@ -167,6 +173,7 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
167173
impl<T: ?Sized> From<&mut T> for Unique<T> {
168174
#[inline]
169175
fn from(reference: &mut T) -> Self {
176+
// SAFETY: A mutable reference cannot be null
170177
unsafe { Unique { pointer: reference as *mut T, _marker: PhantomData } }
171178
}
172179
}

0 commit comments

Comments
 (0)