Skip to content

Commit 3f0f2bf

Browse files
committed
Auto merge of #66189 - Centril:rollup-3bsf45s, r=Centril
Rollup of 5 pull requests Successful merges: - #63793 (Have tidy ensure that we document all `unsafe` blocks in libcore) - #64696 ([rustdoc] add sub settings) - #65916 (syntax: move stuff around) - #66087 (Update some build-pass ui tests to use check-pass where applicable) - #66182 (invalid_value lint: fix help text) Failed merges: r? @ghost
2 parents 50f8aad + 333899a commit 3f0f2bf

File tree

443 files changed

+837
-580
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

443 files changed

+837
-580
lines changed

src/libcore/alloc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Memory allocation APIs
22
3+
// ignore-tidy-undocumented-unsafe
4+
35
#![stable(feature = "alloc_module", since = "1.28.0")]
46

57
use crate::cmp;

src/libcore/any.rs

+6
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ impl dyn Any {
182182
#[inline]
183183
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
184184
if self.is::<T>() {
185+
// SAFETY: just checked whether we are pointing to the correct type
185186
unsafe {
186187
Some(&*(self as *const dyn Any as *const T))
187188
}
@@ -217,6 +218,7 @@ impl dyn Any {
217218
#[inline]
218219
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
219220
if self.is::<T>() {
221+
// SAFETY: just checked whether we are pointing to the correct type
220222
unsafe {
221223
Some(&mut *(self as *mut dyn Any as *mut T))
222224
}
@@ -424,7 +426,11 @@ impl TypeId {
424426
#[rustc_const_unstable(feature="const_type_id")]
425427
pub const fn of<T: ?Sized + 'static>() -> TypeId {
426428
TypeId {
429+
#[cfg(bootstrap)]
430+
// SAFETY: going away soon
427431
t: unsafe { intrinsics::type_id::<T>() },
432+
#[cfg(not(bootstrap))]
433+
t: intrinsics::type_id::<T>(),
428434
}
429435
}
430436
}

src/libcore/array/iter.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ where
5151
/// iterator (either via `IntoIterator` for arrays or via another way).
5252
#[unstable(feature = "array_value_iter", issue = "65798")]
5353
pub fn new(array: [T; N]) -> Self {
54-
// The transmute here is actually safe. The docs of `MaybeUninit`
54+
// SAFETY: The transmute here is actually safe. The docs of `MaybeUninit`
5555
// promise:
5656
//
5757
// > `MaybeUninit<T>` is guaranteed to have the same size and alignment
@@ -84,10 +84,10 @@ where
8484
/// Returns an immutable slice of all elements that have not been yielded
8585
/// yet.
8686
fn as_slice(&self) -> &[T] {
87-
// This transmute is safe. As mentioned in `new`, `MaybeUninit` retains
87+
let slice = &self.data[self.alive.clone()];
88+
// SAFETY: This transmute is safe. As mentioned in `new`, `MaybeUninit` retains
8889
// the size and alignment of `T`. Furthermore, we know that all
8990
// elements within `alive` are properly initialized.
90-
let slice = &self.data[self.alive.clone()];
9191
unsafe {
9292
mem::transmute::<&[MaybeUninit<T>], &[T]>(slice)
9393
}
@@ -117,7 +117,8 @@ where
117117
let idx = self.alive.start;
118118
self.alive.start += 1;
119119

120-
// Read the element from the array. This is safe: `idx` is an index
120+
// Read the element from the array.
121+
// SAFETY: This is safe: `idx` is an index
121122
// into the "alive" region of the array. Reading this element means
122123
// that `data[idx]` is regarded as dead now (i.e. do not touch). As
123124
// `idx` was the start of the alive-zone, the alive zone is now
@@ -163,7 +164,8 @@ where
163164
// + 1]`.
164165
self.alive.end -= 1;
165166

166-
// Read the element from the array. This is safe: `alive.end` is an
167+
// Read the element from the array.
168+
// SAFETY: This is safe: `alive.end` is an
167169
// index into the "alive" region of the array. Compare the previous
168170
// comment that states that the alive region is
169171
// `data[alive.start..alive.end + 1]`. Reading this element means that
@@ -226,6 +228,7 @@ where
226228
[T; N]: LengthAtMost32,
227229
{
228230
fn clone(&self) -> Self {
231+
// SAFETY: each point of unsafety is documented inside the unsafe block
229232
unsafe {
230233
// This creates a new uninitialized array. Note that the `assume_init`
231234
// refers to the array, not the individual elements. And it is Ok if

src/libcore/array/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ where
156156
fn try_from(slice: &[T]) -> Result<&[T; N], TryFromSliceError> {
157157
if slice.len() == N {
158158
let ptr = slice.as_ptr() as *const [T; N];
159+
// SAFETY: ok because we just checked that the length fits
159160
unsafe { Ok(&*ptr) }
160161
} else {
161162
Err(TryFromSliceError(()))
@@ -173,6 +174,7 @@ where
173174
fn try_from(slice: &mut [T]) -> Result<&mut [T; N], TryFromSliceError> {
174175
if slice.len() == N {
175176
let ptr = slice.as_mut_ptr() as *mut [T; N];
177+
// SAFETY: ok because we just checked that the length fits
176178
unsafe { Ok(&mut *ptr) }
177179
} else {
178180
Err(TryFromSliceError(()))

src/libcore/ascii.rs

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ impl FusedIterator for EscapeDefault {}
135135
#[stable(feature = "ascii_escape_display", since = "1.39.0")]
136136
impl fmt::Display for EscapeDefault {
137137
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138+
// SAFETY: ok because `escape_default` created only valid utf-8 data
138139
f.write_str(unsafe { from_utf8_unchecked(&self.data[self.range.clone()]) })
139140
}
140141
}

src/libcore/benches/ascii.rs

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ benches! {
118118
}
119119

120120
fn case07_fake_simd_u32(bytes: &mut [u8]) {
121+
// SAFETY: transmuting a sequence of `u8` to `u32` is always fine
121122
let (before, aligned, after) = unsafe {
122123
bytes.align_to_mut::<u32>()
123124
};
@@ -142,6 +143,7 @@ benches! {
142143
}
143144

144145
fn case08_fake_simd_u64(bytes: &mut [u8]) {
146+
// SAFETY: transmuting a sequence of `u8` to `u64` is always fine
145147
let (before, aligned, after) = unsafe {
146148
bytes.align_to_mut::<u64>()
147149
};

src/libcore/cell.rs

+2
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@
187187
//! ```
188188
//!
189189
190+
// ignore-tidy-undocumented-unsafe
191+
190192
#![stable(feature = "rust1", since = "1.0.0")]
191193

192194
use crate::cmp::Ordering;

src/libcore/char/convert.rs

+1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ impl TryFrom<u32> for char {
224224
if (i > MAX as u32) || (i >= 0xD800 && i <= 0xDFFF) {
225225
Err(CharTryFromError(()))
226226
} else {
227+
// SAFETY: checked that it's a legal unicode value
227228
Ok(unsafe { from_u32_unchecked(i) })
228229
}
229230
}

src/libcore/char/decode.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> {
8787
};
8888

8989
if u < 0xD800 || 0xDFFF < u {
90-
// not a surrogate
90+
// SAFETY: not a surrogate
9191
Some(Ok(unsafe { from_u32_unchecked(u as u32) }))
9292
} else if u >= 0xDC00 {
9393
// a trailing surrogate
@@ -107,6 +107,7 @@ impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> {
107107

108108
// all ok, so lets decode it.
109109
let c = (((u - 0xD800) as u32) << 10 | (u2 - 0xDC00) as u32) + 0x1_0000;
110+
// SAFETY: we checked that it's a legal unicode value
110111
Some(Ok(unsafe { from_u32_unchecked(c) }))
111112
}
112113
}

src/libcore/char/methods.rs

+2
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ impl char {
438438
#[inline]
439439
pub fn encode_utf8(self, dst: &mut [u8]) -> &mut str {
440440
let code = self as u32;
441+
// SAFETY: each arm checks the size of the slice and only uses `get_unchecked` unsafe ops
441442
unsafe {
442443
let len = if code < MAX_ONE_B && !dst.is_empty() {
443444
*dst.get_unchecked_mut(0) = code as u8;
@@ -507,6 +508,7 @@ impl char {
507508
#[inline]
508509
pub fn encode_utf16(self, dst: &mut [u16]) -> &mut [u16] {
509510
let mut code = self as u32;
511+
// SAFETY: each arm checks whether there are enough bits to write into
510512
unsafe {
511513
if (code & 0xFFFF) == code && !dst.is_empty() {
512514
// The BMP falls through (assuming non-surrogate, as it should)

src/libcore/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ impl<'f> Clone for VaListImpl<'f> {
315315
#[inline]
316316
fn clone(&self) -> Self {
317317
let mut dest = crate::mem::MaybeUninit::uninit();
318+
// SAFETY: we write to the `MaybeUninit`, thus it is initialized and `assume_init` is legal
318319
unsafe {
319320
va_copy(dest.as_mut_ptr(), self);
320321
dest.assume_init()

src/libcore/fmt/float.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::fmt::{Formatter, Result, LowerExp, UpperExp, Display, Debug};
22
use crate::mem::MaybeUninit;
33
use crate::num::flt2dec;
44

5+
// ignore-tidy-undocumented-unsafe
6+
57
// Don't inline this so callers don't use the stack space this function
68
// requires unless they have to.
79
#[inline(never)]

src/libcore/fmt/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Utilities for formatting and printing strings.
22
3+
// ignore-tidy-undocumented-unsafe
4+
35
#![stable(feature = "rust1", since = "1.0.0")]
46

57
use crate::cell::{UnsafeCell, Cell, RefCell, Ref, RefMut};

src/libcore/fmt/num.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Integer and floating-point number formatting
22
3+
// ignore-tidy-undocumented-unsafe
4+
35

46
use crate::fmt;
57
use crate::ops::{Div, Rem, Sub};

src/libcore/hash/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
//! }
8080
//! ```
8181
82+
// ignore-tidy-undocumented-unsafe
83+
8284
#![stable(feature = "rust1", since = "1.0.0")]
8385

8486
use crate::fmt;

src/libcore/hash/sip.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! An implementation of SipHash.
22
3+
// ignore-tidy-undocumented-unsafe
4+
35
#![allow(deprecated)] // the types in this module are deprecated
46

57
use crate::marker::PhantomData;

src/libcore/hint.rs

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

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

79
/// Informs the compiler that this point in the code is not reachable, enabling

src/libcore/iter/adapters/mod.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,18 @@ impl<I> Iterator for StepBy<I> where I: Iterator {
517517
// overflow handling
518518
loop {
519519
let mul = n.checked_mul(step);
520-
if unsafe { intrinsics::likely(mul.is_some()) } {
521-
return self.iter.nth(mul.unwrap() - 1);
520+
#[cfg(bootstrap)]
521+
{
522+
// SAFETY: going away soon
523+
if unsafe { intrinsics::likely(mul.is_some()) } {
524+
return self.iter.nth(mul.unwrap() - 1);
525+
}
526+
}
527+
#[cfg(not(bootstrap))]
528+
{
529+
if intrinsics::likely(mul.is_some()) {
530+
return self.iter.nth(mul.unwrap() - 1);
531+
}
522532
}
523533
let div_n = usize::MAX / n;
524534
let div_step = usize::MAX / step;

src/libcore/iter/adapters/zip.rs

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

35
use super::super::{Iterator, DoubleEndedIterator, ExactSizeIterator, FusedIterator, TrustedLen};

src/libcore/mem/maybe_uninit.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::intrinsics;
22
use crate::mem::ManuallyDrop;
33

4+
// ignore-tidy-undocumented-unsafe
5+
46
/// A wrapper type to construct uninitialized instances of `T`.
57
///
68
/// # Initialization invariant

src/libcore/mem/mod.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ pub fn forget<T>(t: T) {
9393
#[inline]
9494
#[unstable(feature = "forget_unsized", issue = "0")]
9595
pub fn forget_unsized<T: ?Sized>(t: T) {
96+
// SAFETY: the forget intrinsic could be safe, but there's no point in making it safe since
97+
// we'll be implementing this function soon via `ManuallyDrop`
9698
unsafe { intrinsics::forget(t) }
9799
}
98100

@@ -266,7 +268,11 @@ pub const fn size_of<T>() -> usize {
266268
#[inline]
267269
#[stable(feature = "rust1", since = "1.0.0")]
268270
pub fn size_of_val<T: ?Sized>(val: &T) -> usize {
271+
#[cfg(bootstrap)]
272+
// SAFETY: going away soon
269273
unsafe { intrinsics::size_of_val(val) }
274+
#[cfg(not(bootstrap))]
275+
intrinsics::size_of_val(val)
270276
}
271277

272278
/// Returns the [ABI]-required minimum alignment of a type.
@@ -310,7 +316,11 @@ pub fn min_align_of<T>() -> usize {
310316
#[stable(feature = "rust1", since = "1.0.0")]
311317
#[rustc_deprecated(reason = "use `align_of_val` instead", since = "1.2.0")]
312318
pub fn min_align_of_val<T: ?Sized>(val: &T) -> usize {
319+
#[cfg(bootstrap)]
320+
// SAFETY: going away soon
313321
unsafe { intrinsics::min_align_of_val(val) }
322+
#[cfg(not(bootstrap))]
323+
intrinsics::min_align_of_val(val)
314324
}
315325

316326
/// Returns the [ABI]-required minimum alignment of a type.
@@ -350,8 +360,9 @@ pub const fn align_of<T>() -> usize {
350360
/// ```
351361
#[inline]
352362
#[stable(feature = "rust1", since = "1.0.0")]
363+
#[allow(deprecated)]
353364
pub fn align_of_val<T: ?Sized>(val: &T) -> usize {
354-
unsafe { intrinsics::min_align_of_val(val) }
365+
min_align_of_val(val)
355366
}
356367

357368
/// Returns `true` if dropping values of type `T` matters.
@@ -508,6 +519,8 @@ pub unsafe fn uninitialized<T>() -> T {
508519
#[inline]
509520
#[stable(feature = "rust1", since = "1.0.0")]
510521
pub fn swap<T>(x: &mut T, y: &mut T) {
522+
// SAFETY: the raw pointers have been created from safe mutable references satisfying all the
523+
// constraints on `ptr::swap_nonoverlapping_one`
511524
unsafe {
512525
ptr::swap_nonoverlapping_one(x, y);
513526
}
@@ -822,7 +835,11 @@ impl<T> fmt::Debug for Discriminant<T> {
822835
/// ```
823836
#[stable(feature = "discriminant_value", since = "1.21.0")]
824837
pub fn discriminant<T>(v: &T) -> Discriminant<T> {
838+
#[cfg(bootstrap)]
839+
// SAFETY: going away soon
825840
unsafe {
826841
Discriminant(intrinsics::discriminant_value(v), PhantomData)
827842
}
843+
#[cfg(not(bootstrap))]
844+
Discriminant(intrinsics::discriminant_value(v), PhantomData)
828845
}

src/libcore/num/dec2flt/algorithm.rs

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ mod fpu_precision {
5858
pub struct FPUControlWord(u16);
5959

6060
fn set_cw(cw: u16) {
61+
// SAFETY: the `fldcw` instruction has been audited to be able to work correctly with
62+
// any `u16`
6163
unsafe { asm!("fldcw $0" :: "m" (cw) :: "volatile") }
6264
}
6365

@@ -74,6 +76,8 @@ mod fpu_precision {
7476

7577
// Get the original value of the control word to restore it later, when the
7678
// `FPUControlWord` structure is dropped
79+
// SAFETY: the `fnstcw` instruction has been audited to be able to work correctly with
80+
// any `u16`
7781
unsafe { asm!("fnstcw $0" : "=*m" (&cw) ::: "volatile") }
7882

7983
// Set the control word to the desired precision. This is achieved by masking away the old

src/libcore/num/f32.rs

+2
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ impl f32 {
414414
#[stable(feature = "float_bits_conv", since = "1.20.0")]
415415
#[inline]
416416
pub fn to_bits(self) -> u32 {
417+
// SAFETY: `u32` is a plain old datatype so we can always transmute to it
417418
unsafe { mem::transmute(self) }
418419
}
419420

@@ -456,6 +457,7 @@ impl f32 {
456457
#[stable(feature = "float_bits_conv", since = "1.20.0")]
457458
#[inline]
458459
pub fn from_bits(v: u32) -> Self {
460+
// SAFETY: `u32` is a plain old datatype so we can always transmute from it
459461
// It turns out the safety issues with sNaN were overblown! Hooray!
460462
unsafe { mem::transmute(v) }
461463
}

src/libcore/num/f64.rs

+2
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ impl f64 {
427427
#[stable(feature = "float_bits_conv", since = "1.20.0")]
428428
#[inline]
429429
pub fn to_bits(self) -> u64 {
430+
// SAFETY: `u64` is a plain old datatype so we can always transmute to it
430431
unsafe { mem::transmute(self) }
431432
}
432433

@@ -469,6 +470,7 @@ impl f64 {
469470
#[stable(feature = "float_bits_conv", since = "1.20.0")]
470471
#[inline]
471472
pub fn from_bits(v: u64) -> Self {
473+
// SAFETY: `u64` is a plain old datatype so we can always transmute from it
472474
// It turns out the safety issues with sNaN were overblown! Hooray!
473475
unsafe { mem::transmute(v) }
474476
}

0 commit comments

Comments
 (0)