Skip to content

Commit 10f511d

Browse files
committed
misc tweaks
1 parent dc570fb commit 10f511d

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/libcore/mem.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,12 +1056,22 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
10561056
/// This is exploited by the compiler for various optimizations, such as eliding
10571057
/// run-time checks and optimizing `enum` layout.
10581058
///
1059-
/// Not initializing memory at all (instead of zero-initializing it) causes the same
1060-
/// issue: after all, the initial value of the variable might just happen to be
1061-
/// one that violates the invariant. Moreover, uninitialized memory is special
1062-
/// in that the compiler knows that it does not have a fixed value. This makes
1063-
/// it undefined behavior to have uninitialized data in a variable even if that
1064-
/// variable has otherwise no restrictions about which values are valid:
1059+
/// Similarly, entirely uninitialized memory may have any content, while a `bool` must
1060+
/// always be `true` or `false`. Hence, creating an uninitialized `bool` is undefined behavior:
1061+
///
1062+
/// ```rust,no_run
1063+
/// #![feature(maybe_uninit)]
1064+
/// use std::mem::{self, MaybeUninit};
1065+
///
1066+
/// let b: bool = unsafe { mem::uninitialized() }; // undefined behavior!
1067+
/// // equivalent code with `MaybeUninit`
1068+
/// let b: bool = unsafe { MaybeUninit::uninitialized().into_initialized() }; // undefined behavior!
1069+
/// ```
1070+
///
1071+
/// Moreover, uninitialized memory is special in that the compiler knows that
1072+
/// it does not have a fixed value. This makes it undefined behavior to have
1073+
/// uninitialized data in a variable even if that variable has integer type,
1074+
/// which otherwise can hold any bit pattern:
10651075
///
10661076
/// ```rust,no_run
10671077
/// #![feature(maybe_uninit)]
@@ -1074,8 +1084,8 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
10741084
/// (Notice that the rules around uninitialized integers are not finalized yet, but
10751085
/// until they are, it is advisable to avoid them.)
10761086
///
1077-
/// `MaybeUninit` serves to enable unsafe code to deal with uninitialized data:
1078-
/// it is a signal to the compiler indicating that the data here might *not*
1087+
/// `MaybeUninit` serves to enable unsafe code to deal with uninitialized data.
1088+
/// It is a signal to the compiler indicating that the data here might *not*
10791089
/// be initialized:
10801090
///
10811091
/// ```rust
@@ -1092,11 +1102,11 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
10921102
/// let x = unsafe { x.into_initialized() };
10931103
/// ```
10941104
///
1095-
/// The compiler then knows to not optimize this code.
1105+
/// The compiler then knows to not make any incorrect assumptions or optimizations on this code.
10961106
// FIXME before stabilizing, explain how to initialize a struct field-by-field.
10971107
#[allow(missing_debug_implementations)]
10981108
#[unstable(feature = "maybe_uninit", issue = "53491")]
1099-
// NOTE after stabilizing `MaybeUninit` proceed to deprecate `mem::{uninitialized,zeroed}`
1109+
// NOTE after stabilizing `MaybeUninit` proceed to deprecate `mem::uninitialized`
11001110
pub union MaybeUninit<T> {
11011111
uninit: (),
11021112
value: ManuallyDrop<T>,
@@ -1154,15 +1164,15 @@ impl<T> MaybeUninit<T> {
11541164
}
11551165

11561166
/// Gets a pointer to the contained value. Reading from this pointer or turning it
1157-
/// into a reference will be undefined behavior unless the `MaybeUninit` is initialized.
1167+
/// into a reference is undefined behavior unless the `MaybeUninit` is initialized.
11581168
#[unstable(feature = "maybe_uninit", issue = "53491")]
11591169
#[inline(always)]
11601170
pub fn as_ptr(&self) -> *const T {
11611171
unsafe { &*self.value as *const T }
11621172
}
11631173

11641174
/// Gets a mutable pointer to the contained value. Reading from this pointer or turning it
1165-
/// into a reference will be undefined behavior unless the `MaybeUninit` is initialized.
1175+
/// into a reference is undefined behavior unless the `MaybeUninit` is initialized.
11661176
#[unstable(feature = "maybe_uninit", issue = "53491")]
11671177
#[inline(always)]
11681178
pub fn as_mut_ptr(&mut self) -> *mut T {

0 commit comments

Comments
 (0)