2019-02-22 22:07:13 +00:00
|
|
|
// ignore-tidy-linelength
|
2021-03-31 08:09:16 +00:00
|
|
|
// stderr-per-bitwidth
|
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
|
2020-04-16 11:21:23 +00:00
|
|
|
//~| type validation failed: 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
|
2020-04-16 11:21:23 +00:00
|
|
|
//~| type validation failed: 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-06-03 00:30:29 +00:00
|
|
|
//~^ ERROR any use of this value will cause an error
|
|
|
|
//~| WARN this was previously accepted by the compiler but is being phased out
|
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-06-03 00:30:29 +00:00
|
|
|
//~^ ERROR any use of this value will cause an error
|
|
|
|
//~| WARN this was previously accepted by the compiler but is being phased out
|
|
|
|
//~| ERROR any use of this value will cause an error
|
|
|
|
//~| WARN this was previously accepted by the compiler but is being phased out
|
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-06-03 00:30:29 +00:00
|
|
|
//~^ ERROR any use of this value will cause an error
|
|
|
|
//~| WARN this was previously accepted by the compiler but is being phased out
|
|
|
|
//~| ERROR any use of this value will cause an error
|
|
|
|
//~| WARN this was previously accepted by the compiler but is being phased out
|
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 };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
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 };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
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
|
|
|
|
2018-10-02 16:07:40 +00:00
|
|
|
fn main() {}
|