1 // ignore-tidy-linelength
3 #![allow(const_err)] // make sure we cannot allow away the errors tested here
5 // normalize-stderr-test "offset \d+" -> "offset N"
6 // normalize-stderr-test "allocation \d+" -> "allocation N"
7 // normalize-stderr-test "size \d+" -> "size N"
16 #[derive(Copy, Clone)]
23 #[derive(Copy, Clone)]
30 union SliceTransmute {
35 raw_slice: *const [u8],
37 my_str: &'static MyStr,
38 my_slice: &'static MySliceBool,
42 #[derive(Copy, Clone)]
49 #[derive(Copy, Clone)]
56 #[derive(Copy, Clone)]
68 rust: &'static dyn Trait,
69 raw_rust: *const dyn Trait,
73 impl Trait for bool {}
75 // custom unsized type
78 // custom unsized type with sized fields
79 struct MySlice<T: ?Sized>(bool, T);
80 type MySliceBool = MySlice<[bool]>;
84 const STR_VALID: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.str};
86 const STR_TOO_LONG: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.str};
87 //~^ ERROR it is undefined behavior to use this value
89 const STR_LENGTH_PTR: &str = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.str};
90 //~^ ERROR it is undefined behavior to use this value
91 // bad str in user-defined unsized type
92 const MY_STR_LENGTH_PTR: &MyStr = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.my_str};
93 //~^ ERROR it is undefined behavior to use this value
96 const STR_NO_UTF8: &str = unsafe { SliceTransmute { slice: &[0xFF] }.str };
97 //~^ ERROR it is undefined behavior to use this value
98 // invalid UTF-8 in user-defined str-like
99 const MYSTR_NO_UTF8: &MyStr = unsafe { SliceTransmute { slice: &[0xFF] }.my_str };
100 //~^ ERROR it is undefined behavior to use this value
104 const SLICE_VALID: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.slice};
105 // bad slice: length uninit
106 const SLICE_LENGTH_UNINIT: &[u8] = unsafe { SliceTransmute { addr: 42 }.slice};
107 //~^ ERROR it is undefined behavior to use this value
108 // bad slice: length too big
109 const SLICE_TOO_LONG: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.slice};
110 //~^ ERROR it is undefined behavior to use this value
111 // bad slice: length not an int
112 const SLICE_LENGTH_PTR: &[u8] = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.slice};
113 //~^ ERROR it is undefined behavior to use this value
115 // bad data *inside* the slice
116 const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { BoolTransmute { val: 3 }.bl }];
117 //~^ ERROR it is undefined behavior to use this value
120 const MYSLICE_GOOD: &MySliceBool = &MySlice(true, [false]);
121 // bad: sized field is not okay
122 const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { BoolTransmute { val: 3 }.bl }, [false]);
123 //~^ ERROR it is undefined behavior to use this value
124 // bad: unsized part is not okay
125 const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { BoolTransmute { val: 3 }.bl }]);
126 //~^ ERROR it is undefined behavior to use this value
129 const RAW_SLICE_VALID: *const [u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.raw_slice}; // ok
130 const RAW_SLICE_TOO_LONG: *const [u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.raw_slice}; // ok because raw
131 const RAW_SLICE_MUCH_TOO_LONG: *const [u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: usize::max_value() } }.raw_slice}; // ok because raw
132 const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe { SliceTransmute { addr: 42 }.raw_slice};
133 //~^ ERROR it is undefined behavior to use this value
137 const TRAIT_OBJ_SHORT_VTABLE_1: &dyn Trait = unsafe { DynTransmute { repr: DynRepr { ptr: &92, vtable: &3 } }.rust};
138 //~^ ERROR it is undefined behavior to use this value
140 const TRAIT_OBJ_SHORT_VTABLE_2: &dyn Trait = unsafe { DynTransmute { repr2: DynRepr2 { ptr: &92, vtable: &3 } }.rust};
141 //~^ ERROR it is undefined behavior to use this value
143 const TRAIT_OBJ_INT_VTABLE: &dyn Trait = unsafe { DynTransmute { bad: BadDynRepr { ptr: &92, vtable: 3 } }.rust};
144 //~^ ERROR it is undefined behavior to use this value
146 // bad data *inside* the trait object
147 const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = &unsafe { BoolTransmute { val: 3 }.bl };
148 //~^ ERROR it is undefined behavior to use this value
150 // # raw trait object
151 const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { DynTransmute { bad: BadDynRepr { ptr: &92, vtable: 0 } }.raw_rust};
152 //~^ ERROR it is undefined behavior to use this value
153 const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { DynTransmute { repr2: DynRepr2 { ptr: &92, vtable: &3 } }.raw_rust};
154 //~^ ERROR it is undefined behavior to use this value
155 const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = &unsafe { BoolTransmute { val: 3 }.bl } as *const _; // ok because raw
157 // Const eval fails for these, so they need to be statics to error.
158 static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe {
159 DynTransmute { bad: BadDynRepr { ptr: &92, vtable: 0 } }.rust
160 //~^ ERROR could not evaluate static initializer
162 static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF: *const dyn Trait = unsafe {
163 DynTransmute { repr2: DynRepr2 { ptr: &92, vtable: &3 } }.rust
164 //~^ ERROR could not evaluate static initializer
168 let _ = RAW_TRAIT_OBJ_VTABLE_NULL;
169 let _ = RAW_TRAIT_OBJ_VTABLE_INVALID;