2021-03-31 03:09:16 -05:00
|
|
|
// stderr-per-bitwidth
|
2019-08-25 07:26:56 -05:00
|
|
|
// ignore-tidy-linelength
|
2018-08-25 07:36:24 -05:00
|
|
|
#![allow(unused)]
|
|
|
|
|
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"
|
|
|
|
|
2021-01-17 12:49:20 -06:00
|
|
|
/// A newtype wrapper to prevent MIR generation from inserting reborrows that would affect the error
|
|
|
|
/// message. Use this whenever the message is "any use of this value will cause an error" instead of
|
|
|
|
/// "it is undefined behavior to use this value".
|
|
|
|
#[repr(transparent)]
|
|
|
|
struct W<T>(T);
|
|
|
|
|
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)) };
|
2022-06-02 19:30:29 -05: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-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)) };
|
2022-06-02 19:30:29 -05:00
|
|
|
//~^ 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-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
|
|
|
|
2020-05-12 02:46:41 -05:00
|
|
|
// uninitialized byte
|
|
|
|
const STR_NO_INIT: &str = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2020-05-12 02:46:41 -05:00
|
|
|
// uninitialized byte in user-defined str-like
|
|
|
|
const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
|
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 {
|
2022-08-01 18:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
|
|
|
//~| uninitialized
|
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
|
2022-03-27 19:02:11 -05:00
|
|
|
// bad slice: length computation overflows
|
|
|
|
const SLICE_TOO_LONG_OVERFLOW: &[u32] = unsafe { mem::transmute((&42u32, isize::MAX)) };
|
|
|
|
//~^ 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)) };
|
2022-06-02 19:30:29 -05:00
|
|
|
//~^ 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 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)) };
|
2022-06-02 19:30:29 -05: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-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
|
2022-06-07 12:29:13 -05:00
|
|
|
//~| ERROR any use of this value will cause an error
|
|
|
|
//~| WARNING this was previously accepted
|
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
|
2022-06-07 12:29:13 -05:00
|
|
|
//~| ERROR any use of this value will cause an error
|
|
|
|
//~| WARNING this was previously accepted
|
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
|
2022-06-07 12:29:13 -05:00
|
|
|
//~| ERROR any use of this value will cause an error
|
|
|
|
//~| WARNING this was previously accepted
|
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 {
|
2022-08-01 18:05:20 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
|
|
|
//~| uninitialized
|
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
|
2021-01-17 12:49:20 -06:00
|
|
|
const TRAIT_OBJ_SHORT_VTABLE_1: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u8))) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2022-07-19 19:49:16 -05:00
|
|
|
//~| expected a vtable
|
2019-08-25 07:26:56 -05:00
|
|
|
// bad trait object
|
2021-01-17 12:49:20 -06:00
|
|
|
const TRAIT_OBJ_SHORT_VTABLE_2: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u64))) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2022-07-19 19:49:16 -05:00
|
|
|
//~| expected a vtable
|
2019-08-25 07:26:56 -05:00
|
|
|
// bad trait object
|
2021-01-17 12:49:20 -06:00
|
|
|
const TRAIT_OBJ_INT_VTABLE: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, 4usize))) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2022-07-19 19:49:16 -05:00
|
|
|
//~| expected a vtable
|
2020-05-05 17:13:41 -05:00
|
|
|
const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) };
|
2022-07-17 15:02:49 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
|
|
|
//~| does not point to a vtable
|
2020-05-06 02:22:52 -05:00
|
|
|
const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92u8, &[0usize; 8])) };
|
2022-07-17 15:02:49 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
|
|
|
//~| does not point to a vtable
|
2020-05-06 02:22:52 -05:00
|
|
|
const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u8, &[1usize; 8])) };
|
2022-07-17 15:02:49 -05:00
|
|
|
//~^ ERROR evaluation of constant value failed
|
|
|
|
//~| does not point to a vtable
|
2021-01-17 12:49:20 -06:00
|
|
|
const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &[&42u8; 8]))) };
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2022-07-19 19:49:16 -05:00
|
|
|
//~| expected a vtable
|
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
|
2022-07-19 19:49:16 -05:00
|
|
|
//~| expected a boolean
|
2019-08-25 07:26:56 -05:00
|
|
|
|
|
|
|
// # 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)) };
|
2022-08-06 16:18:59 -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)) };
|
2022-08-06 16:18:59 -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() {}
|