]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/ub-enum.rs
Merge commit 'd0cf3481a84e3aa68c2f185c460e282af36ebc42' into clippyup
[rust.git] / src / test / ui / consts / const-eval / ub-enum.rs
1 // stderr-per-bitwidth
2 #![feature(never_type)]
3 #![allow(const_err)] // make sure we cannot allow away the errors tested here
4
5 use std::mem;
6
7 #[repr(transparent)]
8 #[derive(Copy, Clone)]
9 struct Wrap<T>(T);
10
11 #[derive(Copy, Clone)]
12 enum Never {}
13
14 // # simple enum with discriminant 0
15
16 #[repr(usize)]
17 #[derive(Copy, Clone)]
18 enum Enum {
19     A = 0,
20 }
21
22 const GOOD_ENUM: Enum = unsafe { mem::transmute(0usize) };
23
24 const BAD_ENUM: Enum = unsafe { mem::transmute(1usize) };
25 //~^ ERROR is undefined behavior
26
27 const BAD_ENUM_PTR: Enum = unsafe { mem::transmute(&1) };
28 //~^ ERROR is undefined behavior
29
30 const BAD_ENUM_WRAPPED: Wrap<Enum> = unsafe { mem::transmute(&1) };
31 //~^ ERROR is undefined behavior
32
33 // # simple enum with discriminant 2
34
35 // (Potentially) invalid enum discriminant
36 #[repr(usize)]
37 #[derive(Copy, Clone)]
38 enum Enum2 {
39     A = 2,
40 }
41
42 const BAD_ENUM2: Enum2 = unsafe { mem::transmute(0usize) };
43 //~^ ERROR is undefined behavior
44 const BAD_ENUM2_PTR: Enum2 = unsafe { mem::transmute(&0) };
45 //~^ ERROR is undefined behavior
46 // something wrapping the enum so that we test layout first, not enum
47 const BAD_ENUM2_WRAPPED: Wrap<Enum2> = unsafe { mem::transmute(&0) };
48 //~^ ERROR is undefined behavior
49
50 // Undef enum discriminant.
51 #[repr(C)]
52 union MaybeUninit<T: Copy> {
53     uninit: (),
54     init: T,
55 }
56 const BAD_ENUM2_UNDEF : Enum2 = unsafe { MaybeUninit { uninit: () }.init };
57 //~^ ERROR is undefined behavior
58
59 // Pointer value in an enum with a niche that is not just 0.
60 const BAD_ENUM2_OPTION_PTR: Option<Enum2> = unsafe { mem::transmute(&0) };
61 //~^ ERROR is undefined behavior
62
63 // # valid discriminant for uninhabited variant
64
65 // An enum with 3 variants of which some are uninhabited -- so the uninhabited variants *do*
66 // have a discriminant.
67 enum UninhDiscriminant {
68     A,
69     B(!),
70     C,
71     D(Never),
72 }
73
74 const GOOD_INHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(0u8) }; // variant A
75 const GOOD_INHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(2u8) }; // variant C
76
77 const BAD_UNINHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(1u8) };
78 //~^ ERROR is undefined behavior
79 const BAD_UNINHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(3u8) };
80 //~^ ERROR is undefined behavior
81
82 // # other
83
84 // Invalid enum field content (mostly to test printing of paths for enum tuple
85 // variants and tuples).
86 // Need to create something which does not clash with enum layout optimizations.
87 const BAD_OPTION_CHAR: Option<(char, char)> = Some(('x', unsafe { mem::transmute(!0u32) }));
88 //~^ ERROR is undefined behavior
89
90 // All variants are uninhabited but also have data.
91 // Use `0` as constant to make behavior endianess-independent.
92 const BAD_UNINHABITED_WITH_DATA1: Result<(i32, Never), (i32, !)> = unsafe { mem::transmute(0u64) };
93 //~^ ERROR is undefined behavior
94 const BAD_UNINHABITED_WITH_DATA2: Result<(i32, !), (i32, Never)> = unsafe { mem::transmute(0u64) };
95 //~^ ERROR is undefined behavior
96
97 fn main() {
98 }