1 // ignore-tidy-linelength
3 #![allow(invalid_value)]
4 #![feature(const_ptr_read)]
9 union MaybeUninit<T: Copy> {
14 const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
15 //~^ ERROR it is undefined behavior to use this value
16 //~| constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
18 const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
19 //~^ ERROR it is undefined behavior to use this value
20 //~| constructing invalid value: encountered an unaligned box (required 2 byte alignment but found 1)
22 const NULL: &u16 = unsafe { mem::transmute(0usize) };
23 //~^ ERROR it is undefined behavior to use this value
25 const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
26 //~^ ERROR it is undefined behavior to use this value
29 // It is very important that we reject this: We do promote `&(4 * REF_AS_USIZE)`,
30 // but that would fail to compile; so we ended up breaking user code that would
31 // have worked fine had we not promoted.
32 const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
33 //~^ ERROR evaluation of constant value failed
35 const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
36 //~^ ERROR evaluation of constant value failed
38 const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
39 //~^ ERROR evaluation of constant value failed
41 const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
42 //~^ ERROR it is undefined behavior to use this value
44 const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
45 //~^ ERROR it is undefined behavior to use this value
47 const UNINIT_PTR: *const i32 = unsafe { MaybeUninit { uninit: () }.init };
48 //~^ ERROR evaluation of constant value failed
51 const NULL_FN_PTR: fn() = unsafe { mem::transmute(0usize) };
52 //~^ ERROR it is undefined behavior to use this value
53 const UNINIT_FN_PTR: fn() = unsafe { MaybeUninit { uninit: () }.init };
54 //~^ ERROR evaluation of constant value failed
56 const DANGLING_FN_PTR: fn() = unsafe { mem::transmute(13usize) };
57 //~^ ERROR it is undefined behavior to use this value
58 const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) };
59 //~^ ERROR it is undefined behavior to use this value
62 const UNALIGNED_READ: () = unsafe {
64 let ptr = x.as_ptr().cast::<u32>();
65 ptr.read(); //~ inside `UNALIGNED_READ`