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"
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 it is undefined behavior to use this value
44 // bad str in user-defined unsized type
45 const MY_STR_LENGTH_PTR: &MyStr = unsafe { mem::transmute((&42u8, &3)) };
46 //~^ ERROR it is undefined behavior to use this value
47 const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize::MAX)) };
48 //~^ ERROR it is undefined behavior to use this value
51 const STR_NO_INIT: &str = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
52 //~^ ERROR it is undefined behavior to use this value
53 // uninitialized byte in user-defined str-like
54 const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
55 //~^ ERROR it is undefined behavior to use this value
59 const SLICE_VALID: &[u8] = unsafe { mem::transmute((&42u8, 1usize)) };
60 // bad slice: length uninit
61 const SLICE_LENGTH_UNINIT: &[u8] = unsafe {
62 //~^ ERROR it is undefined behavior to use this value
63 let uninit_len = MaybeUninit::<usize> { uninit: () };
64 mem::transmute((42, uninit_len))
66 // bad slice: length too big
67 const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) };
68 //~^ ERROR it is undefined behavior to use this value
69 // bad slice: length not an int
70 const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) };
71 //~^ ERROR it is undefined behavior to use this value
72 // bad slice box: length too big
73 const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) };
74 //~^ ERROR it is undefined behavior to use this value
75 // bad slice box: length not an int
76 const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) };
77 //~^ ERROR it is undefined behavior to use this value
79 // bad data *inside* the slice
80 const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
81 //~^ ERROR it is undefined behavior to use this value
84 const MYSLICE_GOOD: &MySliceBool = &MySlice(true, [false]);
85 // bad: sized field is not okay
86 const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
87 //~^ ERROR it is undefined behavior to use this value
88 // bad: unsized part is not okay
89 const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
90 //~^ ERROR it is undefined behavior to use this value
93 const RAW_SLICE_VALID: *const [u8] = unsafe { mem::transmute((&42u8, 1usize)) }; // ok
94 const RAW_SLICE_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, 999usize)) }; // ok because raw
95 const RAW_SLICE_MUCH_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, usize::MAX)) }; // ok because raw
96 const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe {
97 //~^ ERROR it is undefined behavior to use this value
98 let uninit_len = MaybeUninit::<usize> { uninit: () };
99 mem::transmute((42, uninit_len))
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
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
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
112 const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) };
113 //~^ ERROR it is undefined behavior to use this value
114 const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92u8, &[0usize; 8])) };
115 //~^ ERROR it is undefined behavior to use this value
116 const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u8, &[1usize; 8])) };
117 //~^ ERROR it is undefined behavior to use this value
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
121 // bad data *inside* the trait object
122 const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };
123 //~^ ERROR it is undefined behavior to use this value
125 // # raw trait object
126 const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
127 //~^ ERROR it is undefined behavior to use this value
128 const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
129 //~^ ERROR it is undefined behavior to use this value
130 const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) } as *const dyn Trait; // ok because raw
132 // Const eval fails for these, so they need to be statics to error.
133 static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe {
134 mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
135 //~^ ERROR could not evaluate static initializer
137 static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF: *const dyn Trait = unsafe {
138 mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
139 //~^ ERROR could not evaluate static initializer