|
24 | 24 |
|
25 | 25 | - All bytes of the field read have been initialized by a prior write. |
26 | 26 | - The bytes form a valid representation for the field's type |
27 | | - (e.g., no invalid ``enum`` discriminants, no invalid pointer values, etc.). |
| 27 | + (e.g., ``enum`` discriminants are valid and ``bool`` has an underlying value of 0 or 1). |
28 | 28 |
|
29 | 29 | .. rationale:: |
30 | 30 | :id: rat_kjFRrhpS8Wu6 |
|
43 | 43 | :status: draft |
44 | 44 |
|
45 | 45 | This noncompliant example creates a value of type ``u32`` from uninitialized memory via |
46 | | - `assume_init <https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.html#method.assume_init>`_: |
| 46 | + `assume_init <https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.html#method.assume_init>`_. |
| 47 | + Here, calling ``assume_init`` is equivalent to an uninitialied read. |
47 | 48 |
|
48 | 49 | .. code-block:: rust |
49 | 50 |
|
@@ -153,20 +154,6 @@ Values |
153 | 154 | x.write(42); |
154 | 155 | let val = unsafe { x.assume_init() }; // OK — value was fully initialized |
155 | 156 |
|
156 | | - .. non_compliant_example:: |
157 | | - :id: non_compl_ex_Qb5GqYTP6db2 |
158 | | - :status: draft |
159 | | - |
160 | | - Creating a reference from arbitrary or uninitialized bytes is always undefined behavior. |
161 | | - References must be valid, aligned, properly dereferenceable, and non-null. |
162 | | - Uninitialized memory cannot satisfy these invariants. |
163 | | - |
164 | | - .. code-block:: rust |
165 | | -
|
166 | | - use std::mem::MaybeUninit; |
167 | | -
|
168 | | - let r: &u32 = unsafe { MaybeUninit::uninit().assume_init() }; // UB — invalid reference |
169 | | -
|
170 | 157 | .. non_compliant_example:: |
171 | 158 | :id: non_compl_ex_Qb5GqYTP6db5 |
172 | 159 | :status: draft |
@@ -201,9 +188,8 @@ Values |
201 | 188 | :status: draft |
202 | 189 |
|
203 | 190 | Even though unions allow reads of any field, not all bit patterns are valid for a ``bool``. |
204 | | - Unions do not relax type validity requirements. |
205 | | - Only the read itself is allowed; |
206 | | - the resulting bytes must still be a valid bool. |
| 191 | + Unions do not relax type validity requirements for individual fields. |
| 192 | + The resulting bytes must still be a valid allocation of type ``bool``. |
207 | 193 |
|
208 | 194 | .. code-block:: rust |
209 | 195 |
|
|
0 commit comments