2019-02-22 16:07:13 -06:00
|
|
|
// ignore-tidy-linelength
|
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)"
|
2023-11-26 11:21:56 -06:00
|
|
|
//@ normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> "HEX_DUMP"
|
2022-06-02 19:30:29 -05:00
|
|
|
#![allow(invalid_value)]
|
2018-06-04 11:32:06 -05:00
|
|
|
|
2018-10-02 11:07:40 -05:00
|
|
|
use std::mem;
|
|
|
|
|
2021-02-16 04:14:34 -06:00
|
|
|
#[repr(C)]
|
|
|
|
union MaybeUninit<T: Copy> {
|
|
|
|
uninit: (),
|
|
|
|
init: T,
|
|
|
|
}
|
|
|
|
|
2018-10-02 11:07:40 -05:00
|
|
|
const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
|
2018-10-01 05:52:47 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2022-06-25 22:46:51 -05:00
|
|
|
//~| constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
|
2018-10-02 11:07:40 -05:00
|
|
|
|
2020-03-02 13:52:27 -06:00
|
|
|
const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2022-06-25 22:46:51 -05:00
|
|
|
//~| constructing invalid value: encountered an unaligned box (required 2 byte alignment but found 1)
|
2020-03-02 13:52:27 -06:00
|
|
|
|
2018-10-03 04:38:16 -05:00
|
|
|
const NULL: &u16 = unsafe { mem::transmute(0usize) };
|
2018-10-01 05:52:47 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-10-03 04:38:16 -05:00
|
|
|
|
2020-03-02 13:52:27 -06:00
|
|
|
const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
|
2022-02-22 17:49:12 -06:00
|
|
|
|
2019-08-30 02:31:21 -05:00
|
|
|
// It is very important that we reject this: We do promote `&(4 * REF_AS_USIZE)`,
|
|
|
|
// but that would fail to compile; so we ended up breaking user code that would
|
|
|
|
// have worked fine had we not promoted.
|
2018-10-02 11:07:40 -05:00
|
|
|
const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
|
2022-09-21 06:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
2018-10-02 11:07:40 -05:00
|
|
|
|
2018-10-11 04:15:30 -05:00
|
|
|
const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
|
2022-09-21 06:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
2018-10-11 04:15:30 -05:00
|
|
|
|
2020-03-02 13:52:27 -06:00
|
|
|
const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
|
2022-09-21 06:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
2020-03-02 13:52:27 -06:00
|
|
|
|
2018-10-02 11:07:40 -05:00
|
|
|
const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
|
2018-10-01 05:52:47 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-06-04 11:32:06 -05:00
|
|
|
|
2020-03-02 13:52:27 -06:00
|
|
|
const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
|
2021-02-16 04:14:34 -06:00
|
|
|
const UNINIT_PTR: *const i32 = unsafe { MaybeUninit { uninit: () }.init };
|
2022-08-01 18:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
|
|
|
//~| uninitialized
|
2022-02-22 17:49:12 -06:00
|
|
|
|
|
|
|
const NULL_FN_PTR: fn() = unsafe { mem::transmute(0usize) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2021-02-16 04:14:34 -06:00
|
|
|
const UNINIT_FN_PTR: fn() = unsafe { MaybeUninit { uninit: () }.init };
|
2022-08-01 18:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
|
|
|
//~| uninitialized
|
2022-02-22 17:49:12 -06:00
|
|
|
const DANGLING_FN_PTR: fn() = unsafe { mem::transmute(13usize) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2021-02-16 04:14:34 -06:00
|
|
|
|
2022-11-20 02:54:45 -06:00
|
|
|
|
|
|
|
const UNALIGNED_READ: () = unsafe {
|
|
|
|
let x = &[0u8; 4];
|
|
|
|
let ptr = x.as_ptr().cast::<u32>();
|
|
|
|
ptr.read(); //~ inside `UNALIGNED_READ`
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-10-02 11:07:40 -05:00
|
|
|
fn main() {}
|