Skip to content

Commit ac20308

Browse files
committed
rust-lang#66219 documented unsafe in core::ptr
1 parent d74823a commit ac20308

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

Diff for: src/libcore/ptr/mod.rs

+6-2
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::intrinsics;
@@ -251,6 +249,7 @@ pub(crate) struct FatPtr<T> {
251249
#[inline]
252250
#[unstable(feature = "slice_from_raw_parts", reason = "recently added", issue = "36925")]
253251
pub fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
252+
// SAFETY: FatPtr.data and Repr.rust are both usize in the same location
254253
unsafe { Repr { raw: FatPtr { data, len } }.rust }
255254
}
256255

@@ -267,6 +266,7 @@ pub fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
267266
#[inline]
268267
#[unstable(feature = "slice_from_raw_parts", reason = "recently added", issue = "36925")]
269268
pub fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
269+
// SAFETY: FatPtr.data and Repr.rust_mut are both usize in the same location
270270
unsafe { Repr { raw: FatPtr { data, len } }.rust_mut }
271271
}
272272

@@ -1233,6 +1233,7 @@ impl<T: ?Sized> *const T {
12331233
#[stable(feature = "ptr_wrapping_offset", since = "1.16.0")]
12341234
#[inline]
12351235
pub fn wrapping_offset(self, count: isize) -> *const T where T: Sized {
1236+
// SAFETY: see documentation
12361237
unsafe {
12371238
intrinsics::arith_offset(self, count)
12381239
}
@@ -1723,6 +1724,7 @@ impl<T: ?Sized> *const T {
17231724
if !align.is_power_of_two() {
17241725
panic!("align_offset: align is not a power-of-two");
17251726
}
1727+
// SAFETY: align is a power of two
17261728
unsafe {
17271729
align_offset(self, align)
17281730
}
@@ -1931,6 +1933,7 @@ impl<T: ?Sized> *mut T {
19311933
#[stable(feature = "ptr_wrapping_offset", since = "1.16.0")]
19321934
#[inline]
19331935
pub fn wrapping_offset(self, count: isize) -> *mut T where T: Sized {
1936+
// SAFETY: see documentation
19341937
unsafe {
19351938
intrinsics::arith_offset(self, count) as *mut T
19361939
}
@@ -2574,6 +2577,7 @@ impl<T: ?Sized> *mut T {
25742577
if !align.is_power_of_two() {
25752578
panic!("align_offset: align is not a power-of-two");
25762579
}
2580+
// SAFETY: align is a power of two
25772581
unsafe {
25782582
align_offset(self, align)
25792583
}

Diff for: src/libcore/ptr/non_null.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use crate::mem;
77
use crate::ptr::Unique;
88
use crate::cmp::Ordering;
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
@@ -68,6 +66,7 @@ impl<T: Sized> NonNull<T> {
6866
#[stable(feature = "nonnull", since = "1.25.0")]
6967
#[inline]
7068
pub const fn dangling() -> Self {
69+
// SAFETY: must not be dereferenced, but mem::align_of::<T>() > 0 if T is sized
7170
unsafe {
7271
let ptr = mem::align_of::<T>() as *mut T;
7372
NonNull::new_unchecked(ptr)
@@ -92,6 +91,7 @@ impl<T: ?Sized> NonNull<T> {
9291
#[inline]
9392
pub fn new(ptr: *mut T) -> Option<Self> {
9493
if !ptr.is_null() {
94+
// SAFETY: just checked that ptr > 0
9595
Some(unsafe { Self::new_unchecked(ptr) })
9696
} else {
9797
None
@@ -131,6 +131,7 @@ impl<T: ?Sized> NonNull<T> {
131131
#[stable(feature = "nonnull_cast", since = "1.27.0")]
132132
#[inline]
133133
pub const fn cast<U>(self) -> NonNull<U> {
134+
// SAFETY: self.pointer is non-null
134135
unsafe {
135136
NonNull::new_unchecked(self.as_ptr() as *mut U)
136137
}
@@ -207,6 +208,7 @@ impl<T: ?Sized> hash::Hash for NonNull<T> {
207208
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
208209
#[inline]
209210
fn from(unique: Unique<T>) -> Self {
211+
// SAFETY: Unique::as_ptr() can't be null
210212
unsafe { NonNull::new_unchecked(unique.as_ptr()) }
211213
}
212214
}
@@ -215,6 +217,7 @@ impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
215217
impl<T: ?Sized> From<&mut T> for NonNull<T> {
216218
#[inline]
217219
fn from(reference: &mut T) -> Self {
220+
// SAFETY: references can't be null
218221
unsafe { NonNull { pointer: reference as *mut T } }
219222
}
220223
}
@@ -223,6 +226,7 @@ impl<T: ?Sized> From<&mut T> for NonNull<T> {
223226
impl<T: ?Sized> From<&T> for NonNull<T> {
224227
#[inline]
225228
fn from(reference: &T) -> Self {
229+
// SAFETY: references can't be null
226230
unsafe { NonNull { pointer: reference as *const T } }
227231
}
228232
}

Diff for: src/libcore/ptr/unique.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use crate::marker::{PhantomData, Unsize};
55
use crate::mem;
66
use crate::ptr::NonNull;
77

8-
// ignore-tidy-undocumented-unsafe
9-
108
/// A wrapper around a raw non-null `*mut T` that indicates that the possessor
119
/// of this wrapper owns the referent. Useful for building abstractions like
1210
/// `Box<T>`, `Vec<T>`, `String`, and `HashMap<K, V>`.
@@ -71,6 +69,7 @@ impl<T: Sized> Unique<T> {
7169
// FIXME: rename to dangling() to match NonNull?
7270
#[inline]
7371
pub const fn empty() -> Self {
72+
// SAFETY: must not be dereferenced, but mem::align_of::<T>() > 0 if T is sized
7473
unsafe {
7574
Unique::new_unchecked(mem::align_of::<T>() as *mut T)
7675
}
@@ -93,6 +92,7 @@ impl<T: ?Sized> Unique<T> {
9392
#[inline]
9493
pub fn new(ptr: *mut T) -> Option<Self> {
9594
if !ptr.is_null() {
95+
// SAFETY: just checked that ptr > 0
9696
Some(unsafe { Unique { pointer: ptr as _, _marker: PhantomData } })
9797
} else {
9898
None
@@ -128,6 +128,7 @@ impl<T: ?Sized> Unique<T> {
128128
/// Casts to a pointer of another type.
129129
#[inline]
130130
pub const fn cast<U>(self) -> Unique<U> {
131+
// SAFETY: self.pointer is non-null
131132
unsafe {
132133
Unique::new_unchecked(self.as_ptr() as *mut U)
133134
}
@@ -169,6 +170,7 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
169170
impl<T: ?Sized> From<&mut T> for Unique<T> {
170171
#[inline]
171172
fn from(reference: &mut T) -> Self {
173+
// SAFETY: references can't be null
172174
unsafe { Unique { pointer: reference as *mut T, _marker: PhantomData } }
173175
}
174176
}
@@ -177,6 +179,7 @@ impl<T: ?Sized> From<&mut T> for Unique<T> {
177179
impl<T: ?Sized> From<&T> for Unique<T> {
178180
#[inline]
179181
fn from(reference: &T) -> Self {
182+
// SAFETY: references can't be null
180183
unsafe { Unique { pointer: reference as *const T, _marker: PhantomData } }
181184
}
182185
}
@@ -185,6 +188,7 @@ impl<T: ?Sized> From<&T> for Unique<T> {
185188
impl<T: ?Sized> From<NonNull<T>> for Unique<T> {
186189
#[inline]
187190
fn from(p: NonNull<T>) -> Self {
191+
// SAFETY: NonNull::as_ptr() can't be null
188192
unsafe { Unique::new_unchecked(p.as_ptr()) }
189193
}
190194
}

0 commit comments

Comments
 (0)