2021-03-31 03:09:16 -05:00
|
|
|
//@ stderr-per-bitwidth
|
2018-11-01 08:14:51 -05:00
|
|
|
|
2019-08-30 02:35:02 -05:00
|
|
|
#[repr(C)]
|
2018-06-04 11:32:06 -05:00
|
|
|
union DummyUnion {
|
2020-07-05 06:48:06 -05:00
|
|
|
unit: (),
|
2018-06-04 11:32:06 -05:00
|
|
|
u8: u8,
|
|
|
|
bool: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum Enum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
2019-08-30 02:35:02 -05:00
|
|
|
#[repr(C)]
|
2018-06-04 11:32:06 -05:00
|
|
|
union Foo {
|
|
|
|
a: bool,
|
|
|
|
b: Enum,
|
|
|
|
}
|
|
|
|
|
2019-08-30 02:35:02 -05:00
|
|
|
#[repr(C)]
|
2018-06-04 11:32:06 -05:00
|
|
|
union Bar {
|
|
|
|
foo: Foo,
|
|
|
|
u8: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
// the value is not valid for bools
|
|
|
|
const BAD_BOOL: bool = unsafe { DummyUnion { u8: 42 }.bool};
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2020-07-05 06:48:06 -05:00
|
|
|
const UNINIT_BOOL: bool = unsafe { DummyUnion { unit: () }.bool};
|
2022-08-01 18:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
|
|
|
//~| uninitialized
|
2018-06-04 11:32:06 -05:00
|
|
|
|
|
|
|
// The value is not valid for any union variant, but that's fine
|
|
|
|
// unions are just a convenient way to transmute bits around
|
|
|
|
const BAD_UNION: Foo = unsafe { Bar { u8: 42 }.foo };
|
|
|
|
|
|
|
|
|
2018-10-02 11:07:40 -05:00
|
|
|
fn main() {}
|