]>
Commit | Line | Data |
---|---|---|
cdc7bbd5 | 1 | // stderr-per-bitwidth |
e1599b0c XL |
2 | // ignore-tidy-linelength |
3 | #![allow(unused)] | |
e1599b0c | 4 | |
74b04a01 XL |
5 | use std::mem; |
6 | ||
e1599b0c | 7 | // normalize-stderr-test "offset \d+" -> "offset N" |
ba9703b0 | 8 | // normalize-stderr-test "alloc\d+" -> "allocN" |
e1599b0c XL |
9 | // normalize-stderr-test "size \d+" -> "size N" |
10 | ||
5869c6ff | 11 | /// A newtype wrapper to prevent MIR generation from inserting reborrows that would affect the error |
2b03887a | 12 | /// message. |
5869c6ff XL |
13 | #[repr(transparent)] |
14 | struct W<T>(T); | |
15 | ||
e1599b0c | 16 | #[repr(C)] |
74b04a01 XL |
17 | union MaybeUninit<T: Copy> { |
18 | uninit: (), | |
19 | init: T, | |
e1599b0c XL |
20 | } |
21 | ||
22 | trait Trait {} | |
23 | impl Trait for bool {} | |
24 | ||
25 | // custom unsized type | |
26 | struct MyStr(str); | |
27 | ||
28 | // custom unsized type with sized fields | |
29 | struct MySlice<T: ?Sized>(bool, T); | |
30 | type MySliceBool = MySlice<[bool]>; | |
31 | ||
32 | // # str | |
33 | // OK | |
74b04a01 | 34 | const STR_VALID: &str = unsafe { mem::transmute((&42u8, 1usize)) }; |
e1599b0c | 35 | // bad str |
74b04a01 XL |
36 | const STR_TOO_LONG: &str = unsafe { mem::transmute((&42u8, 999usize)) }; |
37 | //~^ ERROR it is undefined behavior to use this value | |
38 | const NESTED_STR_MUCH_TOO_LONG: (&str,) = (unsafe { mem::transmute((&42, usize::MAX)) },); | |
e1599b0c XL |
39 | //~^ ERROR it is undefined behavior to use this value |
40 | // bad str | |
74b04a01 | 41 | const STR_LENGTH_PTR: &str = unsafe { mem::transmute((&42u8, &3)) }; |
2b03887a | 42 | //~^ ERROR evaluation of constant value failed |
e1599b0c | 43 | // bad str in user-defined unsized type |
74b04a01 | 44 | const MY_STR_LENGTH_PTR: &MyStr = unsafe { mem::transmute((&42u8, &3)) }; |
2b03887a | 45 | //~^ ERROR evaluation of constant value failed |
74b04a01 | 46 | const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize::MAX)) }; |
e1599b0c XL |
47 | //~^ ERROR it is undefined behavior to use this value |
48 | ||
f9f354fc XL |
49 | // uninitialized byte |
50 | const STR_NO_INIT: &str = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) }; | |
e1599b0c | 51 | //~^ ERROR it is undefined behavior to use this value |
f9f354fc XL |
52 | // uninitialized byte in user-defined str-like |
53 | const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) }; | |
e1599b0c XL |
54 | //~^ ERROR it is undefined behavior to use this value |
55 | ||
56 | // # slice | |
57 | // OK | |
74b04a01 | 58 | const SLICE_VALID: &[u8] = unsafe { mem::transmute((&42u8, 1usize)) }; |
e1599b0c | 59 | // bad slice: length uninit |
74b04a01 | 60 | const SLICE_LENGTH_UNINIT: &[u8] = unsafe { |
f2b60f7d FG |
61 | //~^ ERROR evaluation of constant value failed |
62 | //~| uninitialized | |
74b04a01 XL |
63 | let uninit_len = MaybeUninit::<usize> { uninit: () }; |
64 | mem::transmute((42, uninit_len)) | |
65 | }; | |
e1599b0c | 66 | // bad slice: length too big |
74b04a01 | 67 | const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) }; |
e1599b0c | 68 | //~^ ERROR it is undefined behavior to use this value |
5e7ed085 FG |
69 | // bad slice: length computation overflows |
70 | const SLICE_TOO_LONG_OVERFLOW: &[u32] = unsafe { mem::transmute((&42u32, isize::MAX)) }; | |
71 | //~^ ERROR it is undefined behavior to use this value | |
e1599b0c | 72 | // bad slice: length not an int |
74b04a01 | 73 | const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) }; |
2b03887a | 74 | //~^ ERROR evaluation of constant value failed |
74b04a01 XL |
75 | // bad slice box: length too big |
76 | const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) }; | |
77 | //~^ ERROR it is undefined behavior to use this value | |
78 | // bad slice box: length not an int | |
79 | const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) }; | |
2b03887a | 80 | //~^ ERROR evaluation of constant value failed |
e1599b0c XL |
81 | |
82 | // bad data *inside* the slice | |
74b04a01 | 83 | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }]; |
e1599b0c | 84 | //~^ ERROR it is undefined behavior to use this value |
487cf647 | 85 | //~| constant |
e1599b0c XL |
86 | |
87 | // good MySliceBool | |
88 | const MYSLICE_GOOD: &MySliceBool = &MySlice(true, [false]); | |
89 | // bad: sized field is not okay | |
74b04a01 | 90 | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]); |
e1599b0c | 91 | //~^ ERROR it is undefined behavior to use this value |
487cf647 | 92 | //~| constant |
e1599b0c | 93 | // bad: unsized part is not okay |
74b04a01 | 94 | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]); |
e1599b0c | 95 | //~^ ERROR it is undefined behavior to use this value |
487cf647 | 96 | //~| constant |
e1599b0c XL |
97 | |
98 | // # raw slice | |
74b04a01 XL |
99 | const RAW_SLICE_VALID: *const [u8] = unsafe { mem::transmute((&42u8, 1usize)) }; // ok |
100 | const RAW_SLICE_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, 999usize)) }; // ok because raw | |
101 | const RAW_SLICE_MUCH_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, usize::MAX)) }; // ok because raw | |
102 | const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe { | |
f2b60f7d FG |
103 | //~^ ERROR evaluation of constant value failed |
104 | //~| uninitialized | |
74b04a01 XL |
105 | let uninit_len = MaybeUninit::<usize> { uninit: () }; |
106 | mem::transmute((42, uninit_len)) | |
107 | }; | |
e1599b0c XL |
108 | |
109 | // # trait object | |
110 | // bad trait object | |
5869c6ff | 111 | const TRAIT_OBJ_SHORT_VTABLE_1: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u8))) }; |
e1599b0c | 112 | //~^ ERROR it is undefined behavior to use this value |
064997fb | 113 | //~| expected a vtable |
e1599b0c | 114 | // bad trait object |
5869c6ff | 115 | const TRAIT_OBJ_SHORT_VTABLE_2: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u64))) }; |
e1599b0c | 116 | //~^ ERROR it is undefined behavior to use this value |
064997fb | 117 | //~| expected a vtable |
e1599b0c | 118 | // bad trait object |
5869c6ff | 119 | const TRAIT_OBJ_INT_VTABLE: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, 4usize))) }; |
e1599b0c | 120 | //~^ ERROR it is undefined behavior to use this value |
064997fb | 121 | //~| expected a vtable |
f9f354fc | 122 | const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) }; |
064997fb FG |
123 | //~^ ERROR evaluation of constant value failed |
124 | //~| does not point to a vtable | |
f9f354fc | 125 | const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92u8, &[0usize; 8])) }; |
064997fb FG |
126 | //~^ ERROR evaluation of constant value failed |
127 | //~| does not point to a vtable | |
f9f354fc | 128 | const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u8, &[1usize; 8])) }; |
064997fb FG |
129 | //~^ ERROR evaluation of constant value failed |
130 | //~| does not point to a vtable | |
5869c6ff | 131 | const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &[&42u8; 8]))) }; |
f9f354fc | 132 | //~^ ERROR it is undefined behavior to use this value |
064997fb | 133 | //~| expected a vtable |
e1599b0c XL |
134 | |
135 | // bad data *inside* the trait object | |
74b04a01 | 136 | const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) }; |
e1599b0c | 137 | //~^ ERROR it is undefined behavior to use this value |
064997fb | 138 | //~| expected a boolean |
e1599b0c XL |
139 | |
140 | // # raw trait object | |
74b04a01 | 141 | const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) }; |
f2b60f7d | 142 | //~^ ERROR it is undefined behavior to use this value |
74b04a01 | 143 | const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) }; |
f2b60f7d | 144 | //~^ ERROR it is undefined behavior to use this value |
74b04a01 | 145 | const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) } as *const dyn Trait; // ok because raw |
e1599b0c | 146 | |
dfeec247 XL |
147 | // Const eval fails for these, so they need to be statics to error. |
148 | static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe { | |
74b04a01 | 149 | mem::transmute::<_, &dyn Trait>((&92u8, 0usize)) |
dfeec247 XL |
150 | //~^ ERROR could not evaluate static initializer |
151 | }; | |
152 | static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF: *const dyn Trait = unsafe { | |
74b04a01 | 153 | mem::transmute::<_, &dyn Trait>((&92u8, &3u64)) |
dfeec247 XL |
154 | //~^ ERROR could not evaluate static initializer |
155 | }; | |
156 | ||
74b04a01 | 157 | fn main() {} |