]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/ub-ref.rs
Rollup merge of #69201 - Aaron1011:feature/permit-if-attr, r=Centril
[rust.git] / src / test / ui / consts / const-eval / ub-ref.rs
1 // ignore-tidy-linelength
2 #![feature(const_transmute)]
3 #![allow(const_err, invalid_value)] // make sure we cannot allow away the errors tested here
4
5 use std::mem;
6
7 const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
8 //~^ ERROR it is undefined behavior to use this value
9 //~^^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
10
11 const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
12 //~^ ERROR it is undefined behavior to use this value
13 //~^^ type validation failed: encountered an unaligned box (required 2 byte alignment but found 1)
14
15 const NULL: &u16 = unsafe { mem::transmute(0usize) };
16 //~^ ERROR it is undefined behavior to use this value
17
18 const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
19 //~^ ERROR it is undefined behavior to use this value
20
21 // It is very important that we reject this: We do promote `&(4 * REF_AS_USIZE)`,
22 // but that would fail to compile; so we ended up breaking user code that would
23 // have worked fine had we not promoted.
24 const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
25 //~^ ERROR it is undefined behavior to use this value
26
27 const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
28 //~^ ERROR it is undefined behavior to use this value
29
30 const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
31 //~^ ERROR it is undefined behavior to use this value
32
33 const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
34 //~^ ERROR it is undefined behavior to use this value
35
36 const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
37 //~^ ERROR it is undefined behavior to use this value
38
39 fn main() {}