2 #![feature(never_type)]
3 #![allow(const_err)] // make sure we cannot allow away the errors tested here
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 is undefined behavior
30 const BAD_ENUM_WRAPPED: Wrap<Enum> = unsafe { mem::transmute(&1) };
31 //~^ ERROR is undefined behavior
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 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
50 // Undef enum discriminant.
52 union MaybeUninit<T: Copy> {
56 const BAD_ENUM2_UNDEF : Enum2 = unsafe { MaybeUninit { uninit: () }.init };
57 //~^ ERROR is undefined behavior
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
63 // # valid discriminant for uninhabited variant
65 // An enum with 3 variants of which some are uninhabited -- so the uninhabited variants *do*
66 // have a discriminant.
67 enum UninhDiscriminant {
74 const GOOD_INHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(0u8) }; // variant A
75 const GOOD_INHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(2u8) }; // variant C
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
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
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