2018-11-03 08:10:34 -05:00
|
|
|
#![feature(rustc_attrs, const_transmute)]
|
2019-08-17 06:45:11 -05:00
|
|
|
#![allow(const_err, invalid_value)] // make sure we cannot allow away the errors tested here
|
2018-10-02 13:05:12 -05:00
|
|
|
|
|
|
|
use std::mem;
|
|
|
|
use std::ptr::NonNull;
|
|
|
|
use std::num::{NonZeroU8, NonZeroUsize};
|
|
|
|
|
2019-07-01 12:11:32 -05:00
|
|
|
const NON_NULL: NonNull<u8> = unsafe { mem::transmute(1usize) };
|
|
|
|
const NON_NULL_PTR: NonNull<u8> = unsafe { mem::transmute(&1) };
|
|
|
|
|
2018-10-02 13:05:12 -05:00
|
|
|
const NULL_PTR: NonNull<u8> = unsafe { mem::transmute(0usize) };
|
2018-10-01 05:52:47 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-10-02 13:05:12 -05:00
|
|
|
|
2019-07-28 07:25:04 -05:00
|
|
|
#[deny(const_err)] // this triggers a `const_err` so validation does not even happen
|
2019-07-01 12:11:32 -05:00
|
|
|
const OUT_OF_BOUNDS_PTR: NonNull<u8> = { unsafe {
|
2019-07-28 07:25:04 -05:00
|
|
|
let ptr: &[u8; 256] = mem::transmute(&0u8); // &0 gets promoted so it does not dangle
|
|
|
|
// Use address-of-element for pointer arithmetic. This could wrap around to NULL!
|
|
|
|
let out_of_bounds_ptr = &ptr[255]; //~ ERROR any use of this value will cause an error
|
2019-07-01 12:11:32 -05:00
|
|
|
mem::transmute(out_of_bounds_ptr)
|
|
|
|
} };
|
|
|
|
|
2018-10-02 13:05:12 -05:00
|
|
|
const NULL_U8: NonZeroU8 = unsafe { mem::transmute(0u8) };
|
2018-10-01 05:52:47 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-10-02 13:05:12 -05:00
|
|
|
const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) };
|
2018-10-01 05:52:47 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-10-02 13:05:12 -05:00
|
|
|
|
2019-01-21 08:48:07 -06:00
|
|
|
union Transmute {
|
|
|
|
uninit: (),
|
|
|
|
out: NonZeroU8,
|
|
|
|
}
|
|
|
|
const UNINIT: NonZeroU8 = unsafe { Transmute { uninit: () }.out };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
|
2018-11-03 08:10:34 -05: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
|
|
|
|
|
2018-10-02 13:05:12 -05:00
|
|
|
fn main() {}
|