2 #![feature(rustc_attrs)]
3 #![allow(const_err, invalid_value)] // make sure we cannot allow away the errors tested here
7 use std::num::{NonZeroU8, NonZeroUsize};
9 const NON_NULL: NonNull<u8> = unsafe { mem::transmute(1usize) };
10 const NON_NULL_PTR: NonNull<u8> = unsafe { mem::transmute(&1) };
12 const NULL_PTR: NonNull<u8> = unsafe { mem::transmute(0usize) };
13 //~^ ERROR it is undefined behavior to use this value
15 #[deny(const_err)] // this triggers a `const_err` so validation does not even happen
16 const OUT_OF_BOUNDS_PTR: NonNull<u8> = { unsafe {
17 let ptr: &[u8; 256] = mem::transmute(&0u8); // &0 gets promoted so it does not dangle
18 // Use address-of-element for pointer arithmetic. This could wrap around to NULL!
19 let out_of_bounds_ptr = &ptr[255]; //~ ERROR any use of this value will cause an error
20 //~| WARN this was previously accepted by the compiler but is being phased out
21 mem::transmute(out_of_bounds_ptr)
24 const NULL_U8: NonZeroU8 = unsafe { mem::transmute(0u8) };
25 //~^ ERROR it is undefined behavior to use this value
26 const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) };
27 //~^ ERROR it is undefined behavior to use this value
30 union MaybeUninit<T: Copy> {
34 const UNINIT: NonZeroU8 = unsafe { MaybeUninit { uninit: () }.init };
35 //~^ ERROR it is undefined behavior to use this value
37 // Also test other uses of rustc_layout_scalar_valid_range_start
39 #[rustc_layout_scalar_valid_range_start(10)]
40 #[rustc_layout_scalar_valid_range_end(30)]
41 struct RestrictedRange1(u32);
42 const BAD_RANGE1: RestrictedRange1 = unsafe { RestrictedRange1(42) };
43 //~^ ERROR it is undefined behavior to use this value
45 #[rustc_layout_scalar_valid_range_start(30)]
46 #[rustc_layout_scalar_valid_range_end(10)]
47 struct RestrictedRange2(u32);
48 const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) };
49 //~^ ERROR it is undefined behavior to use this value