2 #![feature(never_type)]
3 #![allow(invalid_value)]
11 #[derive(Copy, Clone)]
14 // # simple enum with discriminant 0
17 #[derive(Copy, Clone)]
22 const GOOD_ENUM: Enum = unsafe { mem::transmute(0usize) };
24 const BAD_ENUM: Enum = unsafe { mem::transmute(1usize) };
25 //~^ ERROR is undefined behavior
27 const BAD_ENUM_PTR: Enum = unsafe { mem::transmute(&1) };
28 //~^ ERROR evaluation of constant value failed
30 const BAD_ENUM_WRAPPED: Wrap<Enum> = unsafe { mem::transmute(&1) };
31 //~^ ERROR evaluation of constant value failed
33 // # simple enum with discriminant 2
35 // (Potentially) invalid enum discriminant
37 #[derive(Copy, Clone)]
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
50 // Undef enum discriminant.
52 union MaybeUninit<T: Copy> {
56 const BAD_ENUM2_UNDEF : Enum2 = unsafe { MaybeUninit { uninit: () }.init };
57 //~^ ERROR evaluation of constant value failed
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
64 // # valid discriminant for uninhabited variant
66 // An enum with 3 variants of which some are uninhabited -- so the uninhabited variants *do*
67 // have a discriminant.
68 enum UninhDiscriminant {
75 const GOOD_INHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(0u8) }; // variant A
76 const GOOD_INHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(2u8) }; // variant C
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
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
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