1 #![feature(const_transmute, never_type)]
2 #![allow(const_err)] // make sure we cannot allow away the errors tested here
10 #[derive(Copy, Clone)]
13 // # simple enum with discriminant 0
16 #[derive(Copy, Clone)]
21 const GOOD_ENUM: Enum = unsafe { mem::transmute(0usize) };
23 const BAD_ENUM: Enum = unsafe { mem::transmute(1usize) };
24 //~^ ERROR is undefined behavior
26 const BAD_ENUM_PTR: Enum = unsafe { mem::transmute(&1) };
27 //~^ ERROR is undefined behavior
29 const BAD_ENUM_WRAPPED: Wrap<Enum> = unsafe { mem::transmute(&1) };
30 //~^ ERROR is undefined behavior
32 // # simple enum with discriminant 2
34 // (Potentially) invalid enum discriminant
36 #[derive(Copy, Clone)]
41 const BAD_ENUM2: Enum2 = unsafe { mem::transmute(0usize) };
42 //~^ ERROR is undefined behavior
43 const BAD_ENUM2_PTR: Enum2 = unsafe { mem::transmute(&0) };
44 //~^ ERROR is undefined behavior
45 // something wrapping the enum so that we test layout first, not enum
46 const BAD_ENUM2_WRAPPED: Wrap<Enum2> = unsafe { mem::transmute(&0) };
47 //~^ ERROR is undefined behavior
49 // Undef enum discriminant.
51 union MaybeUninit<T: Copy> {
55 const BAD_ENUM2_UNDEF : Enum2 = unsafe { MaybeUninit { uninit: () }.init };
56 //~^ ERROR is undefined behavior
58 // Pointer value in an enum with a niche that is not just 0.
59 const BAD_ENUM2_OPTION_PTR: Option<Enum2> = unsafe { mem::transmute(&0) };
60 //~^ ERROR is undefined behavior
62 // # valid discriminant for uninhabited variant
64 // An enum with 3 variants of which some are uninhabited -- so the uninhabited variants *do*
65 // have a discriminant.
66 enum UninhDiscriminant {
73 const GOOD_INHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(0u8) }; // variant A
74 const GOOD_INHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(2u8) }; // variant C
76 const BAD_UNINHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(1u8) };
77 //~^ ERROR is undefined behavior
78 const BAD_UNINHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(3u8) };
79 //~^ ERROR is undefined behavior
83 // Invalid enum field content (mostly to test printing of paths for enum tuple
84 // variants and tuples).
85 // Need to create something which does not clash with enum layout optimizations.
86 const BAD_OPTION_CHAR: Option<(char, char)> = Some(('x', unsafe { mem::transmute(!0u32) }));
87 //~^ ERROR is undefined behavior
89 // All variants are uninhabited but also have data.
90 const BAD_UNINHABITED_WITH_DATA1: Result<(i32, Never), (i32, !)> = unsafe { mem::transmute(1u64) };
91 //~^ ERROR is undefined behavior
92 const BAD_UNINHABITED_WITH_DATA2: Result<(i32, !), (i32, Never)> = unsafe { mem::transmute(1u64) };
93 //~^ ERROR is undefined behavior