2021-03-31 03:09:16 -05:00
|
|
|
// stderr-per-bitwidth
|
2022-12-22 09:40:50 -06:00
|
|
|
// Strip out raw byte dumps to make comparison platform-independent:
|
|
|
|
// normalize-stderr-test "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
|
|
|
|
// normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*a(lloc)?[0-9]+(\+[a-z0-9]+)?─*╼ )+ *│.*" -> "HEX_DUMP"
|
2019-08-29 19:01:04 -05:00
|
|
|
#![feature(never_type)]
|
2022-09-27 08:59:10 -05:00
|
|
|
#![allow(invalid_value)]
|
2018-11-01 08:14:51 -05:00
|
|
|
|
2020-03-02 13:37:33 -06:00
|
|
|
use std::mem;
|
2019-07-01 12:11:32 -05:00
|
|
|
|
|
|
|
#[repr(transparent)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct Wrap<T>(T);
|
|
|
|
|
2020-03-02 14:03:40 -06:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum Never {}
|
|
|
|
|
|
|
|
// # simple enum with discriminant 0
|
|
|
|
|
2018-08-18 06:46:52 -05:00
|
|
|
#[repr(usize)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum Enum {
|
|
|
|
A = 0,
|
|
|
|
}
|
|
|
|
|
2020-03-02 13:37:33 -06:00
|
|
|
const GOOD_ENUM: Enum = unsafe { mem::transmute(0usize) };
|
2019-07-01 12:11:32 -05:00
|
|
|
|
2020-03-02 13:37:33 -06:00
|
|
|
const BAD_ENUM: Enum = unsafe { mem::transmute(1usize) };
|
2019-07-01 12:11:32 -05:00
|
|
|
//~^ ERROR is undefined behavior
|
|
|
|
|
2020-03-02 13:37:33 -06:00
|
|
|
const BAD_ENUM_PTR: Enum = unsafe { mem::transmute(&1) };
|
2022-09-21 06:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
2019-07-01 12:11:32 -05:00
|
|
|
|
2020-03-02 13:37:33 -06:00
|
|
|
const BAD_ENUM_WRAPPED: Wrap<Enum> = unsafe { mem::transmute(&1) };
|
2022-09-21 06:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
2018-08-18 06:46:52 -05:00
|
|
|
|
2020-03-02 14:03:40 -06:00
|
|
|
// # simple enum with discriminant 2
|
|
|
|
|
2018-11-03 06:44:10 -05:00
|
|
|
// (Potentially) invalid enum discriminant
|
2018-08-18 06:46:52 -05:00
|
|
|
#[repr(usize)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum Enum2 {
|
|
|
|
A = 2,
|
|
|
|
}
|
2019-07-01 12:11:32 -05:00
|
|
|
|
2020-03-02 13:37:33 -06:00
|
|
|
const BAD_ENUM2: Enum2 = unsafe { mem::transmute(0usize) };
|
2018-11-03 06:44:10 -05:00
|
|
|
//~^ ERROR is undefined behavior
|
2020-03-02 13:37:33 -06:00
|
|
|
const BAD_ENUM2_PTR: Enum2 = unsafe { mem::transmute(&0) };
|
2022-09-21 06:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
2020-03-02 13:37:33 -06:00
|
|
|
// something wrapping the enum so that we test layout first, not enum
|
|
|
|
const BAD_ENUM2_WRAPPED: Wrap<Enum2> = unsafe { mem::transmute(&0) };
|
2022-09-21 06:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
2018-08-18 06:46:52 -05:00
|
|
|
|
2018-11-07 06:51:29 -06:00
|
|
|
// Undef enum discriminant.
|
2020-03-02 13:37:33 -06:00
|
|
|
#[repr(C)]
|
|
|
|
union MaybeUninit<T: Copy> {
|
|
|
|
uninit: (),
|
|
|
|
init: T,
|
|
|
|
}
|
|
|
|
const BAD_ENUM2_UNDEF : Enum2 = unsafe { MaybeUninit { uninit: () }.init };
|
2022-08-01 18:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
|
|
|
//~| uninitialized
|
2018-11-01 08:41:36 -05:00
|
|
|
|
2018-11-12 04:22:18 -06:00
|
|
|
// Pointer value in an enum with a niche that is not just 0.
|
2020-03-02 13:37:33 -06:00
|
|
|
const BAD_ENUM2_OPTION_PTR: Option<Enum2> = unsafe { mem::transmute(&0) };
|
2022-09-21 06:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
2018-11-12 04:22:18 -06:00
|
|
|
|
2020-03-02 14:03:40 -06:00
|
|
|
// # valid discriminant for uninhabited variant
|
|
|
|
|
|
|
|
// An enum with 3 variants of which some are uninhabited -- so the uninhabited variants *do*
|
|
|
|
// have a discriminant.
|
|
|
|
enum UninhDiscriminant {
|
|
|
|
A,
|
|
|
|
B(!),
|
|
|
|
C,
|
|
|
|
D(Never),
|
|
|
|
}
|
|
|
|
|
|
|
|
const GOOD_INHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(0u8) }; // variant A
|
|
|
|
const GOOD_INHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(2u8) }; // variant C
|
|
|
|
|
|
|
|
const BAD_UNINHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(1u8) };
|
|
|
|
//~^ ERROR is undefined behavior
|
|
|
|
const BAD_UNINHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(3u8) };
|
|
|
|
//~^ ERROR is undefined behavior
|
|
|
|
|
|
|
|
// # other
|
|
|
|
|
2018-11-03 06:44:10 -05:00
|
|
|
// Invalid enum field content (mostly to test printing of paths for enum tuple
|
2018-08-18 06:46:52 -05:00
|
|
|
// variants and tuples).
|
|
|
|
// Need to create something which does not clash with enum layout optimizations.
|
2020-03-02 13:37:33 -06:00
|
|
|
const BAD_OPTION_CHAR: Option<(char, char)> = Some(('x', unsafe { mem::transmute(!0u32) }));
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR is undefined behavior
|
2018-08-18 06:46:52 -05:00
|
|
|
|
2020-03-02 14:03:40 -06:00
|
|
|
// All variants are uninhabited but also have data.
|
2022-08-17 21:13:37 -05:00
|
|
|
// Use `0` as constant to make behavior endianness-independent.
|
2020-07-03 05:12:50 -05:00
|
|
|
const BAD_UNINHABITED_WITH_DATA1: Result<(i32, Never), (i32, !)> = unsafe { mem::transmute(0u64) };
|
2022-01-24 18:00:00 -06:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
2020-07-03 05:12:50 -05:00
|
|
|
const BAD_UNINHABITED_WITH_DATA2: Result<(i32, !), (i32, Never)> = unsafe { mem::transmute(0u64) };
|
2022-01-24 18:00:00 -06:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
2020-03-02 14:03:40 -06:00
|
|
|
|
2018-08-18 06:46:52 -05:00
|
|
|
fn main() {
|
|
|
|
}
|