You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> `rustc` achieves this with the unstable `rustc_layout_scalar_valid_range_*` attributes.
223
216
217
+
r[undefined.validity.const-provenance]
218
+
***In [const contexts]**: In addition to what is described above, further provenance-related requirements apply during const evaluation. Any value that holds pure integer data (the `i*`/`u*`/`f*` types as well as `bool` and `char`, enum discriminants, and slice metadata) must not carry any provenance. Any value that holds pointer data (references, raw pointers, function pointers, and `dyn Trait` metadata) must either carry no provenance, or all bytes must be fragments of the same original pointer value in the correct order.
219
+
220
+
This implies that transmuting or otherwise reinterpreting a pointer (reference, raw pointer, or function pointer) into a non-pointer type (such as integers) is undefined behavior if the pointer had provenance.
221
+
222
+
> [!EXAMPLE]
223
+
> All of the following are UB:
224
+
>
225
+
> ```rust,compile_fail
226
+
> # use core::mem::MaybeUninit;
227
+
> # use core::ptr;
228
+
> // We cannot reinterpret a pointer with provenance as an integer,
229
+
> // as then the bytes of the integer will have provenance.
230
+
> const _: usize = {
231
+
> let ptr = &0;
232
+
> unsafe { (&raw const ptr as *const usize).read() }
233
+
> };
234
+
>
235
+
> // We cannot rearrange the bytes of a pointer with provenance and
236
+
> // then interpret them as a reference, as then a value holding
237
+
> // pointer data will have pointer fragments in the wrong order.
238
+
> const _: &i32 = {
239
+
> let mut ptr = &0;
240
+
> let ptr_bytes = &raw mut ptr as *mut MaybeUninit::<u8>;
0 commit comments