Skip to content

Commit d515168

Browse files
Document unsafety in core::{panicking, alloc::layout, hint, iter::adapters::zip}
1 parent 413a129 commit d515168

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

src/libcore/alloc/layout.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// ignore-tidy-undocumented-unsafe
2-
31
use crate::cmp;
42
use crate::fmt;
53
use crate::mem;
@@ -77,6 +75,8 @@ impl Layout {
7775
return Err(LayoutErr { private: () });
7876
}
7977

78+
// SAFETY: the conditions for `from_size_align_unchecked` have been
79+
// checked above.
8080
unsafe { Ok(Layout::from_size_align_unchecked(size, align)) }
8181
}
8282

@@ -115,7 +115,7 @@ impl Layout {
115115
#[inline]
116116
pub const fn new<T>() -> Self {
117117
let (size, align) = size_align::<T>();
118-
// Note that the align is guaranteed by rustc to be a power of two and
118+
// SAFETY: the align is guaranteed by Rust to be a power of two and
119119
// the size+align combo is guaranteed to fit in our address space. As a
120120
// result use the unchecked constructor here to avoid inserting code
121121
// that panics if it isn't optimized well enough.
@@ -129,8 +129,8 @@ impl Layout {
129129
#[inline]
130130
pub fn for_value<T: ?Sized>(t: &T) -> Self {
131131
let (size, align) = (mem::size_of_val(t), mem::align_of_val(t));
132-
// See rationale in `new` for why this is using an unsafe variant below
133132
debug_assert!(Layout::from_size_align(size, align).is_ok());
133+
// SAFETY: see rationale in `new` for why this is using an unsafe variant below
134134
unsafe { Layout::from_size_align_unchecked(size, align) }
135135
}
136136

@@ -143,7 +143,7 @@ impl Layout {
143143
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
144144
#[inline]
145145
pub const fn dangling(&self) -> NonNull<u8> {
146-
// align is non-zero and a power of two
146+
// SAFETY: align is guaranteed to be non-zero
147147
unsafe { NonNull::new_unchecked(self.align() as *mut u8) }
148148
}
149149

@@ -249,11 +249,9 @@ impl Layout {
249249
let padded_size = self.size() + self.padding_needed_for(self.align());
250250
let alloc_size = padded_size.checked_mul(n).ok_or(LayoutErr { private: () })?;
251251

252-
unsafe {
253-
// self.align is already known to be valid and alloc_size has been
254-
// padded already.
255-
Ok((Layout::from_size_align_unchecked(alloc_size, self.align()), padded_size))
256-
}
252+
// SAFETY: self.align is already known to be valid and alloc_size has been
253+
// padded already.
254+
unsafe { Ok((Layout::from_size_align_unchecked(alloc_size, self.align()), padded_size)) }
257255
}
258256

259257
/// Creates a layout describing the record for `self` followed by

src/libcore/hint.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
//! Hints to compiler that affects how code should be emitted or optimized.
44
5-
// ignore-tidy-undocumented-unsafe
6-
75
use crate::intrinsics;
86

97
/// Informs the compiler that this point in the code is not reachable, enabling
@@ -68,11 +66,13 @@ pub fn spin_loop() {
6866
{
6967
#[cfg(target_arch = "x86")]
7068
{
69+
// SAFETY: the `cfg` attr ensures that we only execute this on x86 targets.
7170
unsafe { crate::arch::x86::_mm_pause() };
7271
}
7372

7473
#[cfg(target_arch = "x86_64")]
7574
{
75+
// SAFETY: the `cfg` attr ensures that we only execute this on x86_64 targets.
7676
unsafe { crate::arch::x86_64::_mm_pause() };
7777
}
7878
}
@@ -81,10 +81,13 @@ pub fn spin_loop() {
8181
{
8282
#[cfg(target_arch = "aarch64")]
8383
{
84+
// SAFETY: the `cfg` attr ensures that we only execute this on aarch64 targets.
8485
unsafe { crate::arch::aarch64::__yield() };
8586
}
8687
#[cfg(target_arch = "arm")]
8788
{
89+
// SAFETY: the `cfg` attr ensures that we only execute this on arm targets
90+
// with support for the v6 feature.
8891
unsafe { crate::arch::arm::__yield() };
8992
}
9093
}
@@ -112,6 +115,8 @@ pub fn black_box<T>(dummy: T) -> T {
112115
// this. LLVM's interpretation of inline assembly is that it's, well, a black
113116
// box. This isn't the greatest implementation since it probably deoptimizes
114117
// more than we want, but it's so far good enough.
118+
119+
// SAFETY: the inline assembly is a no-op.
115120
unsafe {
116121
llvm_asm!("" : : "r"(&dummy));
117122
dummy

src/libcore/iter/adapters/zip.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// ignore-tidy-undocumented-unsafe
2-
31
use crate::cmp;
42

53
use super::super::{DoubleEndedIterator, ExactSizeIterator, FusedIterator, Iterator, TrustedLen};
@@ -176,9 +174,11 @@ where
176174
if self.index < self.len {
177175
let i = self.index;
178176
self.index += 1;
177+
// SAFETY: `i` is smaller than `self.len`, thus smaller than `self.a.len()` and `self.b.len()`
179178
unsafe { Some((self.a.get_unchecked(i), self.b.get_unchecked(i))) }
180179
} else if A::may_have_side_effect() && self.index < self.a.len() {
181180
// match the base implementation's potential side effects
181+
// SAFETY: we just checked that `self.index` < `self.a.len()`
182182
unsafe {
183183
self.a.get_unchecked(self.index);
184184
}
@@ -203,11 +203,15 @@ where
203203
let i = self.index;
204204
self.index += 1;
205205
if A::may_have_side_effect() {
206+
// SAFETY: the usage of `cmp::min` to calculate `delta`
207+
// ensures that `end` is smaller than or equal to `self.len`,
208+
// so `i` is also smaller than `self.len`.
206209
unsafe {
207210
self.a.get_unchecked(i);
208211
}
209212
}
210213
if B::may_have_side_effect() {
214+
// SAFETY: same as above.
211215
unsafe {
212216
self.b.get_unchecked(i);
213217
}
@@ -243,6 +247,8 @@ where
243247
if self.index < self.len {
244248
self.len -= 1;
245249
let i = self.len;
250+
// SAFETY: `i` is smaller than the previous value of `self.len`,
251+
// which is also smaller than or equal to `self.a.len()` and `self.b.len()`
246252
unsafe { Some((self.a.get_unchecked(i), self.b.get_unchecked(i))) }
247253
} else {
248254
None

src/libcore/panicking.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
//! necessary lang items for the compiler. All panics are funneled through this
2020
//! one function. The actual symbol is declared through the `#[panic_handler]` attribute.
2121
22-
// ignore-tidy-undocumented-unsafe
23-
2422
#![allow(dead_code, missing_docs)]
2523
#![unstable(
2624
feature = "core_panic",
@@ -41,6 +39,7 @@ use crate::panic::{Location, PanicInfo};
4139
#[lang = "panic"] // needed by codegen for panic on overflow and other `Assert` MIR terminators
4240
pub fn panic(expr: &str) -> ! {
4341
if cfg!(feature = "panic_immediate_abort") {
42+
// SAFETY: the `abort` intrinsic has no requirements to be called.
4443
unsafe { super::intrinsics::abort() }
4544
}
4645

@@ -63,6 +62,7 @@ pub fn panic(expr: &str) -> ! {
6362
#[lang = "panic_bounds_check"] // needed by codegen for panic on OOB array/slice access
6463
fn panic_bounds_check(index: usize, len: usize) -> ! {
6564
if cfg!(feature = "panic_immediate_abort") {
65+
// SAFETY: the `abort` intrinsic has no requirements to be called.
6666
unsafe { super::intrinsics::abort() }
6767
}
6868

@@ -77,6 +77,7 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
7777
#[lang = "panic_bounds_check"] // needed by codegen for panic on OOB array/slice access
7878
fn panic_bounds_check(location: &Location<'_>, index: usize, len: usize) -> ! {
7979
if cfg!(feature = "panic_immediate_abort") {
80+
// SAFETY: the `abort` intrinsic has no requirements to be called.
8081
unsafe { super::intrinsics::abort() }
8182
}
8283

@@ -93,6 +94,7 @@ fn panic_bounds_check(location: &Location<'_>, index: usize, len: usize) -> ! {
9394
#[cfg_attr(not(bootstrap), track_caller)]
9495
pub fn panic_fmt(fmt: fmt::Arguments<'_>, #[cfg(bootstrap)] location: &Location<'_>) -> ! {
9596
if cfg!(feature = "panic_immediate_abort") {
97+
// SAFETY: the `abort` intrinsic has no requirements to be called.
9698
unsafe { super::intrinsics::abort() }
9799
}
98100

@@ -108,5 +110,6 @@ pub fn panic_fmt(fmt: fmt::Arguments<'_>, #[cfg(bootstrap)] location: &Location<
108110
#[cfg(not(bootstrap))]
109111
let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller());
110112

113+
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
111114
unsafe { panic_impl(&pi) }
112115
}

0 commit comments

Comments
 (0)