@@ -1056,12 +1056,22 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
1056
1056
/// This is exploited by the compiler for various optimizations, such as eliding
1057
1057
/// run-time checks and optimizing `enum` layout.
1058
1058
///
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:
1065
1075
///
1066
1076
/// ```rust,no_run
1067
1077
/// #![feature(maybe_uninit)]
@@ -1074,8 +1084,8 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
1074
1084
/// (Notice that the rules around uninitialized integers are not finalized yet, but
1075
1085
/// until they are, it is advisable to avoid them.)
1076
1086
///
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*
1079
1089
/// be initialized:
1080
1090
///
1081
1091
/// ```rust
@@ -1092,11 +1102,11 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
1092
1102
/// let x = unsafe { x.into_initialized() };
1093
1103
/// ```
1094
1104
///
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.
1096
1106
// FIXME before stabilizing, explain how to initialize a struct field-by-field.
1097
1107
#[ allow( missing_debug_implementations) ]
1098
1108
#[ 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`
1100
1110
pub union MaybeUninit < T > {
1101
1111
uninit : ( ) ,
1102
1112
value : ManuallyDrop < T > ,
@@ -1154,15 +1164,15 @@ impl<T> MaybeUninit<T> {
1154
1164
}
1155
1165
1156
1166
/// 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.
1158
1168
#[ unstable( feature = "maybe_uninit" , issue = "53491" ) ]
1159
1169
#[ inline( always) ]
1160
1170
pub fn as_ptr ( & self ) -> * const T {
1161
1171
unsafe { & * self . value as * const T }
1162
1172
}
1163
1173
1164
1174
/// 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.
1166
1176
#[ unstable( feature = "maybe_uninit" , issue = "53491" ) ]
1167
1177
#[ inline( always) ]
1168
1178
pub fn as_mut_ptr ( & mut self ) -> * mut T {
0 commit comments