2 // ignore-tidy-linelength
7 // normalize-stderr-test "offset \d+" -> "offset N"
8 // normalize-stderr-test "alloc\d+" -> "allocN"
9 // normalize-stderr-test "size \d+" -> "size N"
11 /// A newtype wrapper to prevent MIR generation from inserting reborrows that would affect the error
12 /// message. Use this whenever the message is "any use of this value will cause an error" instead of
13 /// "it is undefined behavior to use this value".
18 union MaybeUninit
<T
: Copy
> {
24 impl Trait
for bool {}
26 // custom unsized type
29 // custom unsized type with sized fields
30 struct MySlice
<T
: ?Sized
>(bool
, T
);
31 type MySliceBool
= MySlice
<[bool
]>;
35 const STR_VALID
: &str = unsafe { mem::transmute((&42u8, 1usize)) }
;
37 const STR_TOO_LONG
: &str = unsafe { mem::transmute((&42u8, 999usize)) }
;
38 //~^ ERROR it is undefined behavior to use this value
39 const NESTED_STR_MUCH_TOO_LONG
: (&str,) = (unsafe { mem::transmute((&42, usize::MAX)) }
,);
40 //~^ ERROR it is undefined behavior to use this value
42 const STR_LENGTH_PTR
: &str = unsafe { mem::transmute((&42u8, &3)) }
;
43 //~^ ERROR any use of this value will cause an error
44 //~| WARN this was previously accepted by the compiler but is being phased out
45 // bad str in user-defined unsized type
46 const MY_STR_LENGTH_PTR
: &MyStr
= unsafe { mem::transmute((&42u8, &3)) }
;
47 //~^ ERROR any use of this value will cause an error
48 //~| WARN this was previously accepted by the compiler but is being phased out
49 const MY_STR_MUCH_TOO_LONG
: &MyStr
= unsafe { mem::transmute((&42u8, usize::MAX)) }
;
50 //~^ ERROR it is undefined behavior to use this value
53 const STR_NO_INIT
: &str = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }
]) };
54 //~^ ERROR it is undefined behavior to use this value
55 // uninitialized byte in user-defined str-like
56 const MYSTR_NO_INIT
: &MyStr
= unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }
]) };
57 //~^ ERROR it is undefined behavior to use this value
61 const SLICE_VALID
: &[u8] = unsafe { mem::transmute((&42u8, 1usize)) }
;
62 // bad slice: length uninit
63 const SLICE_LENGTH_UNINIT
: &[u8] = unsafe {
64 //~^ ERROR evaluation of constant value failed
66 let uninit_len
= MaybeUninit
::<usize> { uninit: () }
;
67 mem
::transmute((42, uninit_len
))
69 // bad slice: length too big
70 const SLICE_TOO_LONG
: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) }
;
71 //~^ ERROR it is undefined behavior to use this value
72 // bad slice: length computation overflows
73 const SLICE_TOO_LONG_OVERFLOW
: &[u32] = unsafe { mem::transmute((&42u32, isize::MAX)) }
;
74 //~^ ERROR it is undefined behavior to use this value
75 // bad slice: length not an int
76 const SLICE_LENGTH_PTR
: &[u8] = unsafe { mem::transmute((&42u8, &3)) }
;
77 //~^ ERROR any use of this value will cause an error
78 //~| WARN this was previously accepted by the compiler but is being phased out
79 // bad slice box: length too big
80 const SLICE_TOO_LONG_BOX
: Box
<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) }
;
81 //~^ ERROR it is undefined behavior to use this value
82 // bad slice box: length not an int
83 const SLICE_LENGTH_PTR_BOX
: Box
<[u8]> = unsafe { mem::transmute((&42u8, &3)) }
;
84 //~^ ERROR any use of this value will cause an error
85 //~| WARN this was previously accepted by the compiler but is being phased out
87 // bad data *inside* the slice
88 const SLICE_CONTENT_INVALID
: &[bool
] = &[unsafe { mem::transmute(3u8) }
];
89 //~^ ERROR it is undefined behavior to use this value
90 //~| ERROR any use of this value will cause an error
91 //~| WARNING this was previously accepted
94 const MYSLICE_GOOD
: &MySliceBool
= &MySlice(true, [false]);
95 // bad: sized field is not okay
96 const MYSLICE_PREFIX_BAD
: &MySliceBool
= &MySlice(unsafe { mem::transmute(3u8) }
, [false]);
97 //~^ ERROR it is undefined behavior to use this value
98 //~| ERROR any use of this value will cause an error
99 //~| WARNING this was previously accepted
100 // bad: unsized part is not okay
101 const MYSLICE_SUFFIX_BAD
: &MySliceBool
= &MySlice(true, [unsafe { mem::transmute(3u8) }
]);
102 //~^ ERROR it is undefined behavior to use this value
103 //~| ERROR any use of this value will cause an error
104 //~| WARNING this was previously accepted
107 const RAW_SLICE_VALID
: *const [u8] = unsafe { mem::transmute((&42u8, 1usize)) }
; // ok
108 const RAW_SLICE_TOO_LONG
: *const [u8] = unsafe { mem::transmute((&42u8, 999usize)) }
; // ok because raw
109 const RAW_SLICE_MUCH_TOO_LONG
: *const [u8] = unsafe { mem::transmute((&42u8, usize::MAX)) }
; // ok because raw
110 const RAW_SLICE_LENGTH_UNINIT
: *const [u8] = unsafe {
111 //~^ ERROR evaluation of constant value failed
113 let uninit_len
= MaybeUninit
::<usize> { uninit: () }
;
114 mem
::transmute((42, uninit_len
))
119 const TRAIT_OBJ_SHORT_VTABLE_1
: W
<&dyn Trait
> = unsafe { mem::transmute(W((&92u8, &3u8))) }
;
120 //~^ ERROR it is undefined behavior to use this value
121 //~| expected a vtable
123 const TRAIT_OBJ_SHORT_VTABLE_2
: W
<&dyn Trait
> = unsafe { mem::transmute(W((&92u8, &3u64))) }
;
124 //~^ ERROR it is undefined behavior to use this value
125 //~| expected a vtable
127 const TRAIT_OBJ_INT_VTABLE
: W
<&dyn Trait
> = unsafe { mem::transmute(W((&92u8, 4usize))) }
;
128 //~^ ERROR it is undefined behavior to use this value
129 //~| expected a vtable
130 const TRAIT_OBJ_UNALIGNED_VTABLE
: &dyn Trait
= unsafe { mem::transmute((&92u8, &[0u8; 128])) }
;
131 //~^ ERROR evaluation of constant value failed
132 //~| does not point to a vtable
133 const TRAIT_OBJ_BAD_DROP_FN_NULL
: &dyn Trait
= unsafe { mem::transmute((&92u8, &[0usize; 8])) }
;
134 //~^ ERROR evaluation of constant value failed
135 //~| does not point to a vtable
136 const TRAIT_OBJ_BAD_DROP_FN_INT
: &dyn Trait
= unsafe { mem::transmute((&92u8, &[1usize; 8])) }
;
137 //~^ ERROR evaluation of constant value failed
138 //~| does not point to a vtable
139 const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR
: W
<&dyn Trait
> = unsafe { mem::transmute(W((&92u8, &[&42u8; 8]))) }
;
140 //~^ ERROR it is undefined behavior to use this value
141 //~| expected a vtable
143 // bad data *inside* the trait object
144 const TRAIT_OBJ_CONTENT_INVALID
: &dyn Trait
= unsafe { mem::transmute::<_, &bool>(&3u8) }
;
145 //~^ ERROR it is undefined behavior to use this value
146 //~| expected a boolean
148 // # raw trait object
149 const RAW_TRAIT_OBJ_VTABLE_NULL
: *const dyn Trait
= unsafe { mem::transmute((&92u8, 0usize)) }
;
150 //~^ ERROR it is undefined behavior to use this value
151 const RAW_TRAIT_OBJ_VTABLE_INVALID
: *const dyn Trait
= unsafe { mem::transmute((&92u8, &3u64)) }
;
152 //~^ ERROR it is undefined behavior to use this value
153 const RAW_TRAIT_OBJ_CONTENT_INVALID
: *const dyn Trait
= unsafe { mem::transmute::<_, &bool>(&3u8) }
as *const dyn Trait
; // ok because raw
155 // Const eval fails for these, so they need to be statics to error.
156 static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF
: *const dyn Trait
= unsafe {
157 mem
::transmute
::<_
, &dyn Trait
>((&92u8, 0usize
))
158 //~^ ERROR could not evaluate static initializer
160 static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF
: *const dyn Trait
= unsafe {
161 mem
::transmute
::<_
, &dyn Trait
>((&92u8, &3u64))
162 //~^ ERROR could not evaluate static initializer