]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/ub-enum.rs
review comment
[rust.git] / src / test / ui / consts / const-eval / ub-enum.rs
1 // stderr-per-bitwidth
2 #![feature(never_type)]
3 #![allow(invalid_value)]
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 evaluation of constant value failed
29
30 const BAD_ENUM_WRAPPED: Wrap<Enum> = unsafe { mem::transmute(&1) };
31 //~^ ERROR evaluation of constant value failed
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 evaluation of constant value failed
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 evaluation of constant value failed
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 evaluation of constant value failed
58 //~| uninitialized
59
60 // Pointer value in an enum with a niche that is not just 0.
61 const BAD_ENUM2_OPTION_PTR: Option<Enum2> = unsafe { mem::transmute(&0) };
62 //~^ ERROR evaluation of constant value failed
63
64 // # valid discriminant for uninhabited variant
65
66 // An enum with 3 variants of which some are uninhabited -- so the uninhabited variants *do*
67 // have a discriminant.
68 enum UninhDiscriminant {
69     A,
70     B(!),
71     C,
72     D(Never),
73 }
74
75 const GOOD_INHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(0u8) }; // variant A
76 const GOOD_INHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(2u8) }; // variant C
77
78 const BAD_UNINHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(1u8) };
79 //~^ ERROR is undefined behavior
80 const BAD_UNINHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(3u8) };
81 //~^ ERROR is undefined behavior
82
83 // # other
84
85 // Invalid enum field content (mostly to test printing of paths for enum tuple
86 // variants and tuples).
87 // Need to create something which does not clash with enum layout optimizations.
88 const BAD_OPTION_CHAR: Option<(char, char)> = Some(('x', unsafe { mem::transmute(!0u32) }));
89 //~^ ERROR is undefined behavior
90
91 // All variants are uninhabited but also have data.
92 // Use `0` as constant to make behavior endianness-independent.
93 const BAD_UNINHABITED_WITH_DATA1: Result<(i32, Never), (i32, !)> = unsafe { mem::transmute(0u64) };
94 //~^ ERROR evaluation of constant value failed
95 const BAD_UNINHABITED_WITH_DATA2: Result<(i32, !), (i32, Never)> = unsafe { mem::transmute(0u64) };
96 //~^ ERROR evaluation of constant value failed
97
98 fn main() {
99 }