Skip to content

Commit d5e3009

Browse files
authored
Rollup merge of #72431 - RalfJung:ub-warning, r=shepmaster
add warning sign to UB examples Just to make it less likely that people miss the fact that these are examples for how to *not* do it.
2 parents b24030f + 1c9b96b commit d5e3009

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/libcore/mem/maybe_uninit.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ use crate::mem::ManuallyDrop;
2020
/// # #![allow(invalid_value)]
2121
/// use std::mem::{self, MaybeUninit};
2222
///
23-
/// let x: &i32 = unsafe { mem::zeroed() }; // undefined behavior!
23+
/// let x: &i32 = unsafe { mem::zeroed() }; // undefined behavior! ⚠️
2424
/// // The equivalent code with `MaybeUninit<&i32>`:
25-
/// let x: &i32 = unsafe { MaybeUninit::zeroed().assume_init() }; // undefined behavior!
25+
/// let x: &i32 = unsafe { MaybeUninit::zeroed().assume_init() }; // undefined behavior! ⚠️
2626
/// ```
2727
///
2828
/// This is exploited by the compiler for various optimizations, such as eliding
@@ -35,9 +35,9 @@ use crate::mem::ManuallyDrop;
3535
/// # #![allow(invalid_value)]
3636
/// use std::mem::{self, MaybeUninit};
3737
///
38-
/// let b: bool = unsafe { mem::uninitialized() }; // undefined behavior!
38+
/// let b: bool = unsafe { mem::uninitialized() }; // undefined behavior! ⚠️
3939
/// // The equivalent code with `MaybeUninit<bool>`:
40-
/// let b: bool = unsafe { MaybeUninit::uninit().assume_init() }; // undefined behavior!
40+
/// let b: bool = unsafe { MaybeUninit::uninit().assume_init() }; // undefined behavior! ⚠️
4141
/// ```
4242
///
4343
/// Moreover, uninitialized memory is special in that the compiler knows that
@@ -49,9 +49,9 @@ use crate::mem::ManuallyDrop;
4949
/// # #![allow(invalid_value)]
5050
/// use std::mem::{self, MaybeUninit};
5151
///
52-
/// let x: i32 = unsafe { mem::uninitialized() }; // undefined behavior!
52+
/// let x: i32 = unsafe { mem::uninitialized() }; // undefined behavior! ⚠️
5353
/// // The equivalent code with `MaybeUninit<i32>`:
54-
/// let x: i32 = unsafe { MaybeUninit::uninit().assume_init() }; // undefined behavior!
54+
/// let x: i32 = unsafe { MaybeUninit::uninit().assume_init() }; // undefined behavior! ⚠️
5555
/// ```
5656
/// (Notice that the rules around uninitialized integers are not finalized yet, but
5757
/// until they are, it is advisable to avoid them.)
@@ -348,7 +348,7 @@ impl<T> MaybeUninit<T> {
348348
/// let x = MaybeUninit::<(u8, NotZero)>::zeroed();
349349
/// let x = unsafe { x.assume_init() };
350350
/// // Inside a pair, we create a `NotZero` that does not have a valid discriminant.
351-
/// // This is undefined behavior.
351+
/// // This is undefined behavior. ⚠️
352352
/// ```
353353
#[stable(feature = "maybe_uninit", since = "1.36.0")]
354354
#[inline]
@@ -400,7 +400,7 @@ impl<T> MaybeUninit<T> {
400400
///
401401
/// let x = MaybeUninit::<Vec<u32>>::uninit();
402402
/// let x_vec = unsafe { &*x.as_ptr() };
403-
/// // We have created a reference to an uninitialized vector! This is undefined behavior.
403+
/// // We have created a reference to an uninitialized vector! This is undefined behavior. ⚠️
404404
/// ```
405405
///
406406
/// (Notice that the rules around references to uninitialized data are not finalized yet, but
@@ -437,7 +437,7 @@ impl<T> MaybeUninit<T> {
437437
///
438438
/// let mut x = MaybeUninit::<Vec<u32>>::uninit();
439439
/// let x_vec = unsafe { &mut *x.as_mut_ptr() };
440-
/// // We have created a reference to an uninitialized vector! This is undefined behavior.
440+
/// // We have created a reference to an uninitialized vector! This is undefined behavior. ⚠️
441441
/// ```
442442
///
443443
/// (Notice that the rules around references to uninitialized data are not finalized yet, but
@@ -489,7 +489,7 @@ impl<T> MaybeUninit<T> {
489489
///
490490
/// let x = MaybeUninit::<Vec<u32>>::uninit();
491491
/// let x_init = unsafe { x.assume_init() };
492-
/// // `x` had not been initialized yet, so this last line caused undefined behavior.
492+
/// // `x` had not been initialized yet, so this last line caused undefined behavior. ⚠️
493493
/// ```
494494
#[stable(feature = "maybe_uninit", since = "1.36.0")]
495495
#[inline(always)]
@@ -553,7 +553,7 @@ impl<T> MaybeUninit<T> {
553553
/// x.write(Some(vec![0,1,2]));
554554
/// let x1 = unsafe { x.read() };
555555
/// let x2 = unsafe { x.read() };
556-
/// // We now created two copies of the same vector, leading to a double-free when
556+
/// // We now created two copies of the same vector, leading to a double-free ⚠️ when
557557
/// // they both get dropped!
558558
/// ```
559559
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
@@ -603,7 +603,7 @@ impl<T> MaybeUninit<T> {
603603
///
604604
/// let x = MaybeUninit::<Vec<u32>>::uninit();
605605
/// let x_vec: &Vec<u32> = unsafe { x.get_ref() };
606-
/// // We have created a reference to an uninitialized vector! This is undefined behavior.
606+
/// // We have created a reference to an uninitialized vector! This is undefined behavior. ⚠️
607607
/// ```
608608
///
609609
/// ```rust,no_run
@@ -686,7 +686,7 @@ impl<T> MaybeUninit<T> {
686686
/// unsafe {
687687
/// *b.get_mut() = true;
688688
/// // We have created a (mutable) reference to an uninitialized `bool`!
689-
/// // This is undefined behavior.
689+
/// // This is undefined behavior. ⚠️
690690
/// }
691691
/// ```
692692
///

0 commit comments

Comments
 (0)