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