Skip to content

Commit c13d0b6

Browse files
committed
Add comments to examples
1 parent b624855 commit c13d0b6

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/behavior-considered-undefined.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,22 @@ r[undefined.validity.const-provenance]
222222
> [!EXAMPLE]
223223
> All of the following are UB:
224224
> ```rust,compile_fail
225-
> const REINTERPRET_PTR_AS_INT: usize = {
225+
> # use core::mem::MaybeUninit;
226+
> # use core::ptr;
227+
> // We cannot reinterpret a pointer with provenance as an integer,
228+
> // as then the bytes of the integer will have provenance.
229+
> const _: usize = {
226230
> let ptr = &0;
227231
> unsafe { (&raw const ptr as *const usize).read() }
228232
> };
229233
>
230-
> const PTR_BYTES_IN_WRONG_ORDER: &i32 = {
234+
> // We cannot rearrange the bytes of a pointer with provenance and
235+
> // then interpret them as a reference, as then a value holding
236+
> // pointer data will have pointer fragments in the wrong order.
237+
> const _: &i32 = {
231238
> let mut ptr = &0;
232-
> let ptr_bytes = &raw mut ptr as *mut std::mem::MaybeUninit::<u8>;
233-
> unsafe { std::ptr::swap(ptr_bytes.add(1), ptr_bytes.add(2)) };
239+
> let ptr_bytes = &raw mut ptr as *mut MaybeUninit::<u8>;
240+
> unsafe { ptr::swap(ptr_bytes.add(1), ptr_bytes.add(2)) };
234241
> ptr
235242
> };
236243
> ```

0 commit comments

Comments
 (0)