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)"
|
|
|
|
// normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*a(lloc)?[0-9]+(\+[a-z0-9]+)?─*╼ )+ *│.*" -> "HEX_DUMP"
|
2022-11-03 10:12:49 +00:00
|
|
|
#![feature(rustc_attrs, ptr_metadata)]
|
2022-09-21 11:05:20 +00:00
|
|
|
#![allow(invalid_value)] // make sure we cannot allow away the errors tested here
|
2018-10-02 18:05:12 +00:00
|
|
|
|
|
|
|
use std::mem;
|
|
|
|
use std::ptr::NonNull;
|
|
|
|
use std::num::{NonZeroU8, NonZeroUsize};
|
|
|
|
|
2019-07-01 17:11:32 +00:00
|
|
|
const NON_NULL: NonNull<u8> = unsafe { mem::transmute(1usize) };
|
|
|
|
const NON_NULL_PTR: NonNull<u8> = unsafe { mem::transmute(&1) };
|
|
|
|
|
2018-10-02 18:05:12 +00:00
|
|
|
const NULL_PTR: NonNull<u8> = unsafe { mem::transmute(0usize) };
|
2018-10-01 10:52:47 +00:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-10-02 18:05:12 +00:00
|
|
|
|
2019-07-01 17:11:32 +00:00
|
|
|
const OUT_OF_BOUNDS_PTR: NonNull<u8> = { unsafe {
|
2019-07-28 12:25:04 +00:00
|
|
|
let ptr: &[u8; 256] = mem::transmute(&0u8); // &0 gets promoted so it does not dangle
|
2021-05-02 21:55:22 +00:00
|
|
|
// Use address-of-element for pointer arithmetic. This could wrap around to null!
|
2021-06-18 17:31:56 +00:00
|
|
|
let out_of_bounds_ptr = &ptr[255]; //~ ERROR evaluation of constant value failed
|
2019-07-01 17:11:32 +00:00
|
|
|
mem::transmute(out_of_bounds_ptr)
|
|
|
|
} };
|
|
|
|
|
2018-10-02 18:05:12 +00:00
|
|
|
const NULL_U8: NonZeroU8 = unsafe { mem::transmute(0u8) };
|
2018-10-01 10:52:47 +00:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-10-02 18:05:12 +00:00
|
|
|
const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) };
|
2018-10-01 10:52:47 +00:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-10-02 18:05:12 +00:00
|
|
|
|
2019-08-30 07:35:02 +00:00
|
|
|
#[repr(C)]
|
2020-03-02 19:37:33 +00:00
|
|
|
union MaybeUninit<T: Copy> {
|
2019-01-21 14:48:07 +00:00
|
|
|
uninit: (),
|
2020-03-02 19:37:33 +00:00
|
|
|
init: T,
|
2019-01-21 14:48:07 +00:00
|
|
|
}
|
2020-03-02 19:37:33 +00:00
|
|
|
const UNINIT: NonZeroU8 = unsafe { MaybeUninit { uninit: () }.init };
|
2022-08-01 23:05:20 +00:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
|
|
|
//~| uninitialized
|
2019-01-21 14:48:07 +00:00
|
|
|
|
2018-11-03 13:10:34 +00:00
|
|
|
// Also test other uses of rustc_layout_scalar_valid_range_start
|
|
|
|
|
|
|
|
#[rustc_layout_scalar_valid_range_start(10)]
|
|
|
|
#[rustc_layout_scalar_valid_range_end(30)]
|
|
|
|
struct RestrictedRange1(u32);
|
|
|
|
const BAD_RANGE1: RestrictedRange1 = unsafe { RestrictedRange1(42) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
|
|
|
|
#[rustc_layout_scalar_valid_range_start(30)]
|
|
|
|
#[rustc_layout_scalar_valid_range_end(10)]
|
|
|
|
struct RestrictedRange2(u32);
|
|
|
|
const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
|
2022-11-03 10:12:49 +00:00
|
|
|
const NULL_FAT_PTR: NonNull<dyn Send> = unsafe {
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
let x: &dyn Send = &42;
|
|
|
|
let meta = std::ptr::metadata(x);
|
|
|
|
mem::transmute((0_usize, meta))
|
|
|
|
};
|
|
|
|
|
2018-10-02 18:05:12 +00:00
|
|
|
fn main() {}
|