@@ -8,6 +8,12 @@ use std::mem;
8
8
// normalize-stderr-test "alloc\d+" -> "allocN"
9
9
// normalize-stderr-test "size \d+" -> "size N"
10
10
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".
14
+ #[ repr( transparent) ]
15
+ struct W < T > ( T ) ;
16
+
11
17
#[ repr( C ) ]
12
18
union MaybeUninit < T : Copy > {
13
19
uninit : ( ) ,
@@ -95,26 +101,22 @@ const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe {
95
101
96
102
// # trait object
97
103
// bad trait object
98
- #[ warn( const_err) ]
99
- const TRAIT_OBJ_SHORT_VTABLE_1 : & dyn Trait = unsafe { mem:: transmute ( ( & 92u8 , & 3u8 ) ) } ;
100
- //~^ WARN any use of this value will cause an error [const_err]
104
+ const TRAIT_OBJ_SHORT_VTABLE_1 : W < & dyn Trait > = unsafe { mem:: transmute ( W ( ( & 92u8 , & 3u8 ) ) ) } ;
105
+ //~^ ERROR it is undefined behavior to use this value
101
106
// bad trait object
102
- #[ warn( const_err) ]
103
- const TRAIT_OBJ_SHORT_VTABLE_2 : & dyn Trait = unsafe { mem:: transmute ( ( & 92u8 , & 3u64 ) ) } ;
104
- //~^ WARN any use of this value will cause an error [const_err]
107
+ const TRAIT_OBJ_SHORT_VTABLE_2 : W < & dyn Trait > = unsafe { mem:: transmute ( W ( ( & 92u8 , & 3u64 ) ) ) } ;
108
+ //~^ ERROR it is undefined behavior to use this value
105
109
// bad trait object
106
- #[ warn( const_err) ]
107
- const TRAIT_OBJ_INT_VTABLE : & dyn Trait = unsafe { mem:: transmute ( ( & 92u8 , 4usize ) ) } ;
108
- //~^ WARN any use of this value will cause an error [const_err]
110
+ const TRAIT_OBJ_INT_VTABLE : W < & dyn Trait > = unsafe { mem:: transmute ( W ( ( & 92u8 , 4usize ) ) ) } ;
111
+ //~^ ERROR it is undefined behavior to use this value
109
112
const TRAIT_OBJ_UNALIGNED_VTABLE : & dyn Trait = unsafe { mem:: transmute ( ( & 92u8 , & [ 0u8 ; 128 ] ) ) } ;
110
113
//~^ ERROR it is undefined behavior to use this value
111
114
const TRAIT_OBJ_BAD_DROP_FN_NULL : & dyn Trait = unsafe { mem:: transmute ( ( & 92u8 , & [ 0usize ; 8 ] ) ) } ;
112
115
//~^ ERROR it is undefined behavior to use this value
113
116
const TRAIT_OBJ_BAD_DROP_FN_INT : & dyn Trait = unsafe { mem:: transmute ( ( & 92u8 , & [ 1usize ; 8 ] ) ) } ;
114
117
//~^ ERROR it is undefined behavior to use this value
115
- #[ warn( const_err) ]
116
- const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR : & dyn Trait = unsafe { mem:: transmute ( ( & 92u8 , & [ & 42u8 ; 8 ] ) ) } ;
117
- //~^ WARN any use of this value will cause an error [const_err]
118
+ const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR : W < & dyn Trait > = unsafe { mem:: transmute ( W ( ( & 92u8 , & [ & 42u8 ; 8 ] ) ) ) } ;
119
+ //~^ ERROR it is undefined behavior to use this value
118
120
119
121
// bad data *inside* the trait object
120
122
const TRAIT_OBJ_CONTENT_INVALID : & dyn Trait = unsafe { mem:: transmute :: < _ , & bool > ( & 3u8 ) } ;
0 commit comments