2019-08-25 07:26:56 -05:00
|
|
|
// ignore-tidy-linelength
|
2020-03-02 13:37:33 -06:00
|
|
|
#![feature(const_transmute)]
|
2018-08-25 07:36:24 -05:00
|
|
|
#![allow(unused)]
|
2018-11-01 08:14:51 -05:00
|
|
|
#![allow(const_err)] // make sure we cannot allow away the errors tested here
|
2018-08-25 07:36:24 -05:00
|
|
|
|
2020-03-02 13:37:33 -06:00
|
|
|
use std::mem;
|
|
|
|
|
2018-08-10 16:46:59 -05:00
|
|
|
// normalize-stderr-test "offset \d+" -> "offset N"
|
2020-03-08 13:44:09 -05:00
|
|
|
// normalize-stderr-test "alloc\d+" -> "allocN"
|
2018-08-10 16:46:59 -05:00
|
|
|
// normalize-stderr-test "size \d+" -> "size N"
|
|
|
|
|
2019-08-30 02:29:58 -05:00
|
|
|
#[repr(C)]
|
2020-03-02 13:37:33 -06:00
|
|
|
union MaybeUninit<T: Copy> {
|
|
|
|
uninit: (),
|
|
|
|
init: T,
|
2018-06-04 11:32:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
trait Trait {}
|
2018-08-17 05:39:36 -05:00
|
|
|
impl Trait for bool {}
|
2018-06-04 11:32:06 -05:00
|
|
|
|
2018-08-25 07:36:24 -05:00
|
|
|
// custom unsized type
|
|
|
|
struct MyStr(str);
|
|
|
|
|
|
|
|
// custom unsized type with sized fields
|
|
|
|
struct MySlice<T: ?Sized>(bool, T);
|
|
|
|
type MySliceBool = MySlice<[bool]>;
|
2018-08-22 04:27:38 -05:00
|
|
|
|
2019-08-25 07:26:56 -05:00
|
|
|
// # str
|
2018-06-04 11:32:06 -05:00
|
|
|
// OK
|
2020-03-02 13:37:33 -06:00
|
|
|
const STR_VALID: &str = unsafe { mem::transmute((&42u8, 1usize)) };
|
2018-08-17 05:39:36 -05:00
|
|
|
// bad str
|
2020-03-02 13:37:33 -06:00
|
|
|
const STR_TOO_LONG: &str = unsafe { mem::transmute((&42u8, 999usize)) };
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2020-03-05 16:31:39 -06:00
|
|
|
const NESTED_STR_MUCH_TOO_LONG: (&str,) = (unsafe { mem::transmute((&42, usize::MAX)) },);
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-08-17 05:39:36 -05:00
|
|
|
// bad str
|
2020-03-02 13:37:33 -06:00
|
|
|
const STR_LENGTH_PTR: &str = unsafe { mem::transmute((&42u8, &3)) };
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-08-25 07:36:24 -05:00
|
|
|
// bad str in user-defined unsized type
|
2020-03-02 13:37:33 -06:00
|
|
|
const MY_STR_LENGTH_PTR: &MyStr = unsafe { mem::transmute((&42u8, &3)) };
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2020-03-05 16:31:39 -06:00
|
|
|
const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize::MAX)) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-06-04 11:32:06 -05:00
|
|
|
|
2019-08-25 07:26:56 -05:00
|
|
|
// invalid UTF-8
|
2020-03-02 13:37:33 -06:00
|
|
|
const STR_NO_UTF8: &str = unsafe { mem::transmute::<&[u8], _>(&[0xFF]) };
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2019-08-25 07:26:56 -05:00
|
|
|
// invalid UTF-8 in user-defined str-like
|
2020-03-02 13:37:33 -06:00
|
|
|
const MYSTR_NO_UTF8: &MyStr = unsafe { mem::transmute::<&[u8], _>(&[0xFF]) };
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-06-04 11:32:06 -05:00
|
|
|
|
2019-08-25 07:26:56 -05:00
|
|
|
// # slice
|
|
|
|
// OK
|
2020-03-02 13:37:33 -06:00
|
|
|
const SLICE_VALID: &[u8] = unsafe { mem::transmute((&42u8, 1usize)) };
|
2019-08-25 07:26:56 -05:00
|
|
|
// bad slice: length uninit
|
2020-03-02 13:37:33 -06:00
|
|
|
const SLICE_LENGTH_UNINIT: &[u8] = unsafe {
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2020-03-02 13:37:33 -06:00
|
|
|
let uninit_len = MaybeUninit::<usize> { uninit: () };
|
|
|
|
mem::transmute((42, uninit_len))
|
|
|
|
};
|
2019-08-25 07:26:56 -05:00
|
|
|
// bad slice: length too big
|
2020-03-02 13:37:33 -06:00
|
|
|
const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) };
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2019-08-25 07:26:56 -05:00
|
|
|
// bad slice: length not an int
|
2020-03-02 13:37:33 -06:00
|
|
|
const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) };
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2020-03-02 14:17:34 -06:00
|
|
|
// bad slice box: length too big
|
|
|
|
const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
// bad slice box: length not an int
|
|
|
|
const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-08-26 08:19:34 -05:00
|
|
|
|
2018-08-17 05:39:36 -05:00
|
|
|
// bad data *inside* the slice
|
2020-03-02 13:37:33 -06:00
|
|
|
const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-08-17 05:39:36 -05:00
|
|
|
|
2018-08-25 07:36:24 -05:00
|
|
|
// good MySliceBool
|
2019-08-27 11:36:11 -05:00
|
|
|
const MYSLICE_GOOD: &MySliceBool = &MySlice(true, [false]);
|
2018-08-25 07:36:24 -05:00
|
|
|
// bad: sized field is not okay
|
2020-03-02 13:37:33 -06:00
|
|
|
const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-08-25 07:36:24 -05:00
|
|
|
// bad: unsized part is not okay
|
2020-03-02 13:37:33 -06:00
|
|
|
const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-08-25 07:36:24 -05:00
|
|
|
|
2019-08-25 07:26:56 -05:00
|
|
|
// # raw slice
|
2020-03-02 13:37:33 -06:00
|
|
|
const RAW_SLICE_VALID: *const [u8] = unsafe { mem::transmute((&42u8, 1usize)) }; // ok
|
|
|
|
const RAW_SLICE_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, 999usize)) }; // ok because raw
|
2020-03-05 16:31:39 -06:00
|
|
|
const RAW_SLICE_MUCH_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, usize::MAX)) }; // ok because raw
|
2020-03-02 13:37:33 -06:00
|
|
|
const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe {
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2020-03-02 13:37:33 -06:00
|
|
|
let uninit_len = MaybeUninit::<usize> { uninit: () };
|
|
|
|
mem::transmute((42, uninit_len))
|
|
|
|
};
|
2019-08-25 07:26:56 -05:00
|
|
|
|
|
|
|
// # trait object
|
|
|
|
// bad trait object
|
2020-03-02 13:37:33 -06:00
|
|
|
const TRAIT_OBJ_SHORT_VTABLE_1: &dyn Trait = unsafe { mem::transmute((&92u8, &3u8)) };
|
2019-08-25 07:26:56 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
// bad trait object
|
2020-03-02 13:37:33 -06:00
|
|
|
const TRAIT_OBJ_SHORT_VTABLE_2: &dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
|
2019-08-25 07:26:56 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
// bad trait object
|
2020-03-02 13:37:33 -06:00
|
|
|
const TRAIT_OBJ_INT_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, 4usize)) };
|
2019-08-25 07:26:56 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2020-05-05 17:13:41 -05:00
|
|
|
const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2019-08-25 07:26:56 -05:00
|
|
|
|
|
|
|
// bad data *inside* the trait object
|
2020-03-02 13:37:33 -06:00
|
|
|
const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };
|
2019-08-25 07:26:56 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
|
|
|
|
// # raw trait object
|
2020-03-02 13:37:33 -06:00
|
|
|
const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
|
2019-08-25 07:26:56 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2020-03-02 13:37:33 -06:00
|
|
|
const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2020-03-02 13:37:33 -06:00
|
|
|
const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) } as *const dyn Trait; // ok because raw
|
2018-08-25 07:36:24 -05:00
|
|
|
|
2019-09-17 15:18:15 -05:00
|
|
|
// Const eval fails for these, so they need to be statics to error.
|
|
|
|
static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe {
|
2020-03-02 13:37:33 -06:00
|
|
|
mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
|
2019-09-17 15:18:15 -05:00
|
|
|
//~^ ERROR could not evaluate static initializer
|
|
|
|
};
|
|
|
|
static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF: *const dyn Trait = unsafe {
|
2020-03-02 13:37:33 -06:00
|
|
|
mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
|
2019-09-17 15:18:15 -05:00
|
|
|
//~^ ERROR could not evaluate static initializer
|
|
|
|
};
|
|
|
|
|
2020-03-02 13:37:33 -06:00
|
|
|
fn main() {}
|