2020-03-02 19:37:33 +00:00
|
|
|
#![feature(const_transmute)]
|
2018-11-01 13:14:51 +00:00
|
|
|
#![allow(const_err)] // make sure we cannot allow away the errors tested here
|
|
|
|
|
2020-03-02 19:37:33 +00:00
|
|
|
use std::mem;
|
2019-07-01 17:11:32 +00:00
|
|
|
|
|
|
|
#[repr(transparent)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct Wrap<T>(T);
|
|
|
|
|
2018-08-18 11:46:52 +00:00
|
|
|
#[repr(usize)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum Enum {
|
|
|
|
A = 0,
|
|
|
|
}
|
|
|
|
|
2020-03-02 19:37:33 +00:00
|
|
|
const GOOD_ENUM: Enum = unsafe { mem::transmute(0usize) };
|
2019-07-01 17:11:32 +00:00
|
|
|
|
2020-03-02 19:37:33 +00:00
|
|
|
const BAD_ENUM: Enum = unsafe { mem::transmute(1usize) };
|
2019-07-01 17:11:32 +00:00
|
|
|
//~^ ERROR is undefined behavior
|
|
|
|
|
2020-03-02 19:37:33 +00:00
|
|
|
const BAD_ENUM_PTR: Enum = unsafe { mem::transmute(&1) };
|
2019-07-01 17:11:32 +00:00
|
|
|
//~^ ERROR is undefined behavior
|
|
|
|
|
2020-03-02 19:37:33 +00:00
|
|
|
const BAD_ENUM_WRAPPED: Wrap<Enum> = unsafe { mem::transmute(&1) };
|
2018-08-26 13:19:34 +00:00
|
|
|
//~^ ERROR is undefined behavior
|
2018-08-18 11:46:52 +00:00
|
|
|
|
2018-11-03 11:44:10 +00:00
|
|
|
// (Potentially) invalid enum discriminant
|
2018-08-18 11:46:52 +00:00
|
|
|
#[repr(usize)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum Enum2 {
|
|
|
|
A = 2,
|
|
|
|
}
|
2019-07-01 17:11:32 +00:00
|
|
|
|
2020-03-02 19:37:33 +00:00
|
|
|
const BAD_ENUM2: Enum2 = unsafe { mem::transmute(0usize) };
|
2018-11-03 11:44:10 +00:00
|
|
|
//~^ ERROR is undefined behavior
|
2020-03-02 19:37:33 +00:00
|
|
|
const BAD_ENUM2_PTR: Enum2 = unsafe { mem::transmute(&0) };
|
2018-11-03 11:44:10 +00:00
|
|
|
//~^ ERROR is undefined behavior
|
2020-03-02 19:37:33 +00:00
|
|
|
// something wrapping the enum so that we test layout first, not enum
|
|
|
|
const BAD_ENUM2_WRAPPED: Wrap<Enum2> = unsafe { mem::transmute(&0) };
|
2018-08-26 13:19:34 +00:00
|
|
|
//~^ ERROR is undefined behavior
|
2018-08-18 11:46:52 +00:00
|
|
|
|
2018-11-07 12:51:29 +00:00
|
|
|
// Undef enum discriminant.
|
2020-03-02 19:37:33 +00:00
|
|
|
#[repr(C)]
|
|
|
|
union MaybeUninit<T: Copy> {
|
|
|
|
uninit: (),
|
|
|
|
init: T,
|
|
|
|
}
|
|
|
|
const BAD_ENUM2_UNDEF : Enum2 = unsafe { MaybeUninit { uninit: () }.init };
|
2018-11-01 13:41:36 +00:00
|
|
|
//~^ ERROR is undefined behavior
|
|
|
|
|
2018-11-12 10:22:18 +00:00
|
|
|
// Pointer value in an enum with a niche that is not just 0.
|
2020-03-02 19:37:33 +00:00
|
|
|
const BAD_ENUM2_OPTION_PTR: Option<Enum2> = unsafe { mem::transmute(&0) };
|
2018-11-12 10:22:18 +00:00
|
|
|
//~^ ERROR is undefined behavior
|
|
|
|
|
2018-11-03 11:44:10 +00:00
|
|
|
// Invalid enum field content (mostly to test printing of paths for enum tuple
|
2018-08-18 11:46:52 +00:00
|
|
|
// variants and tuples).
|
|
|
|
// Need to create something which does not clash with enum layout optimizations.
|
2020-03-02 19:37:33 +00:00
|
|
|
const BAD_OPTION_CHAR: Option<(char, char)> = Some(('x', unsafe { mem::transmute(!0u32) }));
|
2018-08-26 13:19:34 +00:00
|
|
|
//~^ ERROR is undefined behavior
|
2018-08-18 11:46:52 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|