]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/ub-ref-ptr.rs
Rollup merge of #104461 - mati865:gnullvm-aarch64-fixup, r=Mark-Simulacrum
[rust.git] / src / test / ui / consts / const-eval / ub-ref-ptr.rs
1 // ignore-tidy-linelength
2 // stderr-per-bitwidth
3 #![allow(invalid_value)]
4
5 use std::mem;
6
7 #[repr(C)]
8 union MaybeUninit<T: Copy> {
9     uninit: (),
10     init: T,
11 }
12
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)
16
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)
20
21 const NULL: &u16 = unsafe { mem::transmute(0usize) };
22 //~^ ERROR it is undefined behavior to use this value
23
24 const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
25 //~^ ERROR it is undefined behavior to use this value
26
27
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
33
34 const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
35 //~^ ERROR evaluation of constant value failed
36
37 const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
38 //~^ ERROR evaluation of constant value failed
39
40 const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
41 //~^ ERROR it is undefined behavior to use this value
42
43 const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
44 //~^ ERROR it is undefined behavior to use this value
45
46 const UNINIT_PTR: *const i32 = unsafe { MaybeUninit { uninit: () }.init };
47 //~^ ERROR evaluation of constant value failed
48 //~| uninitialized
49
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
54 //~| uninitialized
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
59
60 fn main() {}