1 // ignore-tidy-linelength
3 #![allow(invalid_value)]
8 union MaybeUninit<T: Copy> {
13 const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
14 //~^ ERROR it is undefined behavior to use this value
15 //~| constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
17 const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
18 //~^ ERROR it is undefined behavior to use this value
19 //~| constructing invalid value: encountered an unaligned box (required 2 byte alignment but found 1)
21 const NULL: &u16 = unsafe { mem::transmute(0usize) };
22 //~^ ERROR it is undefined behavior to use this value
24 const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
25 //~^ ERROR it is undefined behavior to use this value
28 // It is very important that we reject this: We do promote `&(4 * REF_AS_USIZE)`,
29 // but that would fail to compile; so we ended up breaking user code that would
30 // have worked fine had we not promoted.
31 const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
32 //~^ ERROR evaluation of constant value failed
34 const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
35 //~^ ERROR evaluation of constant value failed
37 const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
38 //~^ ERROR evaluation of constant value failed
40 const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
41 //~^ ERROR it is undefined behavior to use this value
43 const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
44 //~^ ERROR it is undefined behavior to use this value
46 const UNINIT_PTR: *const i32 = unsafe { MaybeUninit { uninit: () }.init };
47 //~^ ERROR evaluation of constant value failed
50 const NULL_FN_PTR: fn() = unsafe { mem::transmute(0usize) };
51 //~^ ERROR it is undefined behavior to use this value
52 const UNINIT_FN_PTR: fn() = unsafe { MaybeUninit { uninit: () }.init };
53 //~^ ERROR evaluation of constant value failed
55 const DANGLING_FN_PTR: fn() = unsafe { mem::transmute(13usize) };
56 //~^ ERROR it is undefined behavior to use this value
57 const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) };
58 //~^ ERROR it is undefined behavior to use this value