Skip to content

Commit 05b1991

Browse files
authored
Rollup merge of rust-lang#71760 - LeSeulArtichaut:document-unsafety, r=Mark-Simulacrum
Document unsafety for `*const T` and `*mut T` Helps with rust-lang#66219 r? @Mark-Simulacrum
2 parents 8aad12b + d61deba commit 05b1991

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/libcore/ptr/const_ptr.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::cmp::Ordering::{self, Equal, Greater, Less};
33
use crate::intrinsics;
44
use crate::mem;
55

6-
// ignore-tidy-undocumented-unsafe
7-
86
#[lang = "const_ptr"]
97
impl<T: ?Sized> *const T {
108
/// Returns `true` if the pointer is null.
@@ -215,6 +213,7 @@ impl<T: ?Sized> *const T {
215213
where
216214
T: Sized,
217215
{
216+
// SAFETY: the `arith_offset` intrinsic has no prerequisites to be called.
218217
unsafe { intrinsics::arith_offset(self, count) }
219218
}
220219

@@ -702,6 +701,7 @@ impl<T: ?Sized> *const T {
702701
if !align.is_power_of_two() {
703702
panic!("align_offset: align is not a power-of-two");
704703
}
704+
// SAFETY: `align` has been checked to be a power of 2 above
705705
unsafe { align_offset(self, align) }
706706
}
707707
}
@@ -729,6 +729,8 @@ impl<T> *const [T] {
729729
#[unstable(feature = "slice_ptr_len", issue = "71146")]
730730
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
731731
pub const fn len(self) -> usize {
732+
// SAFETY: this is safe because `*const [T]` and `FatPtr<T>` have the same layout.
733+
// Only `std` can make this guarantee.
732734
unsafe { Repr { rust: self }.raw }.len
733735
}
734736
}

src/libcore/ptr/mut_ptr.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use super::*;
22
use crate::cmp::Ordering::{self, Equal, Greater, Less};
33
use crate::intrinsics;
44

5-
// ignore-tidy-undocumented-unsafe
6-
75
#[lang = "mut_ptr"]
86
impl<T: ?Sized> *mut T {
97
/// Returns `true` if the pointer is null.
@@ -208,6 +206,7 @@ impl<T: ?Sized> *mut T {
208206
where
209207
T: Sized,
210208
{
209+
// SAFETY: the `arith_offset` intrinsic has no prerequisites to be called.
211210
unsafe { intrinsics::arith_offset(self, count) as *mut T }
212211
}
213212

@@ -890,6 +889,7 @@ impl<T: ?Sized> *mut T {
890889
if !align.is_power_of_two() {
891890
panic!("align_offset: align is not a power-of-two");
892891
}
892+
// SAFETY: `align` has been checked to be a power of 2 above
893893
unsafe { align_offset(self, align) }
894894
}
895895
}
@@ -917,6 +917,8 @@ impl<T> *mut [T] {
917917
#[unstable(feature = "slice_ptr_len", issue = "71146")]
918918
#[rustc_const_unstable(feature = "const_slice_ptr_len", issue = "71146")]
919919
pub const fn len(self) -> usize {
920+
// SAFETY: this is safe because `*const [T]` and `FatPtr<T>` have the same layout.
921+
// Only `std` can make this guarantee.
920922
unsafe { Repr { rust_mut: self }.raw }.len
921923
}
922924
}

0 commit comments

Comments
 (0)