1 // ignore-tidy-linelength
3 #![allow(const_err)] // make sure we cannot allow away the errors tested here
7 // normalize-stderr-test "offset \d+" -> "offset N"
8 // normalize-stderr-test "alloc\d+" -> "allocN"
9 // normalize-stderr-test "size \d+" -> "size N"
12 union MaybeUninit<T: Copy> {
18 impl Trait for bool {}
20 // custom unsized type
23 // custom unsized type with sized fields
24 struct MySlice<T: ?Sized>(bool, T);
25 type MySliceBool = MySlice<[bool]>;
29 const STR_VALID: &str = unsafe { mem::transmute((&42u8, 1usize)) };
31 const STR_TOO_LONG: &str = unsafe { mem::transmute((&42u8, 999usize)) };
32 //~^ ERROR it is undefined behavior to use this value
33 const NESTED_STR_MUCH_TOO_LONG: (&str,) = (unsafe { mem::transmute((&42, usize::MAX)) },);
34 //~^ ERROR it is undefined behavior to use this value
36 const STR_LENGTH_PTR: &str = unsafe { mem::transmute((&42u8, &3)) };
37 //~^ ERROR it is undefined behavior to use this value
38 // bad str in user-defined unsized type
39 const MY_STR_LENGTH_PTR: &MyStr = unsafe { mem::transmute((&42u8, &3)) };
40 //~^ ERROR it is undefined behavior to use this value
41 const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize::MAX)) };
42 //~^ ERROR it is undefined behavior to use this value
45 const STR_NO_INIT: &str = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
46 //~^ ERROR it is undefined behavior to use this value
47 // uninitialized byte in user-defined str-like
48 const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
49 //~^ ERROR it is undefined behavior to use this value
53 const SLICE_VALID: &[u8] = unsafe { mem::transmute((&42u8, 1usize)) };
54 // bad slice: length uninit
55 const SLICE_LENGTH_UNINIT: &[u8] = unsafe {
56 //~^ ERROR it is undefined behavior to use this value
57 let uninit_len = MaybeUninit::<usize> { uninit: () };
58 mem::transmute((42, uninit_len))
60 // bad slice: length too big
61 const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) };
62 //~^ ERROR it is undefined behavior to use this value
63 // bad slice: length not an int
64 const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) };
65 //~^ ERROR it is undefined behavior to use this value
66 // bad slice box: length too big
67 const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) };
68 //~^ ERROR it is undefined behavior to use this value
69 // bad slice box: length not an int
70 const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) };
71 //~^ ERROR it is undefined behavior to use this value
73 // bad data *inside* the slice
74 const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
75 //~^ ERROR it is undefined behavior to use this value
78 const MYSLICE_GOOD: &MySliceBool = &MySlice(true, [false]);
79 // bad: sized field is not okay
80 const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
81 //~^ ERROR it is undefined behavior to use this value
82 // bad: unsized part is not okay
83 const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
84 //~^ ERROR it is undefined behavior to use this value
87 const RAW_SLICE_VALID: *const [u8] = unsafe { mem::transmute((&42u8, 1usize)) }; // ok
88 const RAW_SLICE_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, 999usize)) }; // ok because raw
89 const RAW_SLICE_MUCH_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, usize::MAX)) }; // ok because raw
90 const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe {
91 //~^ ERROR it is undefined behavior to use this value
92 let uninit_len = MaybeUninit::<usize> { uninit: () };
93 mem::transmute((42, uninit_len))
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]
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_INT_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, 4usize)) };
108 //~^ WARN any use of this value will cause an error [const_err]
109 const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) };
110 //~^ ERROR it is undefined behavior to use this value
111 const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92u8, &[0usize; 8])) };
112 //~^ ERROR it is undefined behavior to use this value
113 const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u8, &[1usize; 8])) };
114 //~^ ERROR it is undefined behavior to use this value
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]
119 // bad data *inside* the trait object
120 const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };
121 //~^ ERROR it is undefined behavior to use this value
123 // # raw trait object
124 const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
125 //~^ ERROR it is undefined behavior to use this value
126 const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
127 //~^ ERROR it is undefined behavior to use this value
128 const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) } as *const dyn Trait; // ok because raw
130 // Const eval fails for these, so they need to be statics to error.
131 static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe {
132 mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
133 //~^ ERROR could not evaluate static initializer
135 static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF: *const dyn Trait = unsafe {
136 mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
137 //~^ ERROR could not evaluate static initializer