1 // ignore-tidy-linelength
6 // Strip out raw byte dumps to make comparison platform-independent:
7 // normalize-stderr-test "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
8 // normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*a(lloc)?[0-9]+(\+[a-z0-9]+)?─*╼ )+ *│.*" -> "HEX_DUMP"
9 // normalize-stderr-test "offset \d+" -> "offset N"
10 // normalize-stderr-test "alloc\d+" -> "allocN"
11 // normalize-stderr-test "size \d+" -> "size N"
13 /// A newtype wrapper to prevent MIR generation from inserting reborrows that would affect the error
19 union MaybeUninit
<T
: Copy
> {
25 impl Trait
for bool {}
27 // custom unsized type
30 // custom unsized type with sized fields
31 struct MySlice
<T
: ?Sized
>(bool
, T
);
32 type MySliceBool
= MySlice
<[bool
]>;
36 const STR_VALID
: &str = unsafe { mem::transmute((&42u8, 1usize)) }
;
38 const STR_TOO_LONG
: &str = unsafe { mem::transmute((&42u8, 999usize)) }
;
39 //~^ ERROR it is undefined behavior to use this value
40 const NESTED_STR_MUCH_TOO_LONG
: (&str,) = (unsafe { mem::transmute((&42, usize::MAX)) }
,);
41 //~^ ERROR it is undefined behavior to use this value
43 const STR_LENGTH_PTR
: &str = unsafe { mem::transmute((&42u8, &3)) }
;
44 //~^ ERROR evaluation of constant value failed
45 // bad str in user-defined unsized type
46 const MY_STR_LENGTH_PTR
: &MyStr
= unsafe { mem::transmute((&42u8, &3)) }
;
47 //~^ ERROR evaluation of constant value failed
48 const MY_STR_MUCH_TOO_LONG
: &MyStr
= unsafe { mem::transmute((&42u8, usize::MAX)) }
;
49 //~^ ERROR it is undefined behavior to use this value
52 const STR_NO_INIT
: &str = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }
]) };
53 //~^ ERROR it is undefined behavior to use this value
54 // uninitialized byte in user-defined str-like
55 const MYSTR_NO_INIT
: &MyStr
= unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }
]) };
56 //~^ ERROR it is undefined behavior to use this value
60 const SLICE_VALID
: &[u8] = unsafe { mem::transmute((&42u8, 1usize)) }
;
61 // bad slice: length uninit
62 const SLICE_LENGTH_UNINIT
: &[u8] = unsafe {
63 //~^ ERROR evaluation of constant value failed
65 let uninit_len
= MaybeUninit
::<usize> { uninit: () }
;
66 mem
::transmute((42, uninit_len
))
68 // bad slice: length too big
69 const SLICE_TOO_LONG
: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) }
;
70 //~^ ERROR it is undefined behavior to use this value
71 // bad slice: length computation overflows
72 const SLICE_TOO_LONG_OVERFLOW
: &[u32] = unsafe { mem::transmute((&42u32, isize::MAX)) }
;
73 //~^ ERROR it is undefined behavior to use this value
74 // bad slice: length not an int
75 const SLICE_LENGTH_PTR
: &[u8] = unsafe { mem::transmute((&42u8, &3)) }
;
76 //~^ ERROR evaluation of constant value failed
77 // bad slice box: length too big
78 const SLICE_TOO_LONG_BOX
: Box
<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) }
;
79 //~^ ERROR it is undefined behavior to use this value
80 // bad slice box: length not an int
81 const SLICE_LENGTH_PTR_BOX
: Box
<[u8]> = unsafe { mem::transmute((&42u8, &3)) }
;
82 //~^ ERROR evaluation of constant value failed
84 // bad data *inside* the slice
85 const SLICE_CONTENT_INVALID
: &[bool
] = &[unsafe { mem::transmute(3u8) }
];
86 //~^ ERROR it is undefined behavior to use this value
90 const MYSLICE_GOOD
: &MySliceBool
= &MySlice(true, [false]);
91 // bad: sized field is not okay
92 const MYSLICE_PREFIX_BAD
: &MySliceBool
= &MySlice(unsafe { mem::transmute(3u8) }
, [false]);
93 //~^ ERROR it is undefined behavior to use this value
95 // bad: unsized part is not okay
96 const MYSLICE_SUFFIX_BAD
: &MySliceBool
= &MySlice(true, [unsafe { mem::transmute(3u8) }
]);
97 //~^ ERROR it is undefined behavior to use this value
101 const RAW_SLICE_VALID
: *const [u8] = unsafe { mem::transmute((&42u8, 1usize)) }
; // ok
102 const RAW_SLICE_TOO_LONG
: *const [u8] = unsafe { mem::transmute((&42u8, 999usize)) }
; // ok because raw
103 const RAW_SLICE_MUCH_TOO_LONG
: *const [u8] = unsafe { mem::transmute((&42u8, usize::MAX)) }
; // ok because raw
104 const RAW_SLICE_LENGTH_UNINIT
: *const [u8] = unsafe {
105 //~^ ERROR evaluation of constant value failed
107 let uninit_len
= MaybeUninit
::<usize> { uninit: () }
;
108 mem
::transmute((42, uninit_len
))
113 const TRAIT_OBJ_SHORT_VTABLE_1
: W
<&dyn Trait
> = unsafe { mem::transmute(W((&92u8, &3u8))) }
;
114 //~^ ERROR it is undefined behavior to use this value
115 //~| expected a vtable
117 const TRAIT_OBJ_SHORT_VTABLE_2
: W
<&dyn Trait
> = unsafe { mem::transmute(W((&92u8, &3u64))) }
;
118 //~^ ERROR it is undefined behavior to use this value
119 //~| expected a vtable
121 const TRAIT_OBJ_INT_VTABLE
: W
<&dyn Trait
> = unsafe { mem::transmute(W((&92u8, 4usize))) }
;
122 //~^ ERROR it is undefined behavior to use this value
123 //~| expected a vtable
124 const TRAIT_OBJ_UNALIGNED_VTABLE
: &dyn Trait
= unsafe { mem::transmute((&92u8, &[0u8; 128])) }
;
125 //~^ ERROR evaluation of constant value failed
126 //~| does not point to a vtable
127 const TRAIT_OBJ_BAD_DROP_FN_NULL
: &dyn Trait
= unsafe { mem::transmute((&92u8, &[0usize; 8])) }
;
128 //~^ ERROR evaluation of constant value failed
129 //~| does not point to a vtable
130 const TRAIT_OBJ_BAD_DROP_FN_INT
: &dyn Trait
= unsafe { mem::transmute((&92u8, &[1usize; 8])) }
;
131 //~^ ERROR evaluation of constant value failed
132 //~| does not point to a vtable
133 const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR
: W
<&dyn Trait
> = unsafe { mem::transmute(W((&92u8, &[&42u8; 8]))) }
;
134 //~^ ERROR it is undefined behavior to use this value
135 //~| expected a vtable
137 // bad data *inside* the trait object
138 const TRAIT_OBJ_CONTENT_INVALID
: &dyn Trait
= unsafe { mem::transmute::<_, &bool>(&3u8) }
;
139 //~^ ERROR it is undefined behavior to use this value
140 //~| expected a boolean
142 // # raw trait object
143 const RAW_TRAIT_OBJ_VTABLE_NULL
: *const dyn Trait
= unsafe { mem::transmute((&92u8, 0usize)) }
;
144 //~^ ERROR it is undefined behavior to use this value
145 const RAW_TRAIT_OBJ_VTABLE_INVALID
: *const dyn Trait
= unsafe { mem::transmute((&92u8, &3u64)) }
;
146 //~^ ERROR it is undefined behavior to use this value
147 const RAW_TRAIT_OBJ_CONTENT_INVALID
: *const dyn Trait
= unsafe { mem::transmute::<_, &bool>(&3u8) }
as *const dyn Trait
; // ok because raw
149 // Const eval fails for these, so they need to be statics to error.
150 static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF
: *const dyn Trait
= unsafe {
151 mem
::transmute
::<_
, &dyn Trait
>((&92u8, 0usize
))
152 //~^ ERROR could not evaluate static initializer
154 static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF
: *const dyn Trait
= unsafe {
155 mem
::transmute
::<_
, &dyn Trait
>((&92u8, &3u64))
156 //~^ ERROR could not evaluate static initializer