]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-eval/ub-wide-ptr.rs
Update const_forget.rs
[rust.git] / src / test / ui / consts / const-eval / ub-wide-ptr.rs
1 // ignore-tidy-linelength
2 #![allow(unused)]
3 #![allow(const_err)] // make sure we cannot allow away the errors tested here
4
5 // normalize-stderr-test "offset \d+" -> "offset N"
6 // normalize-stderr-test "allocation \d+" -> "allocation N"
7 // normalize-stderr-test "size \d+" -> "size N"
8
9 #[repr(C)]
10 union BoolTransmute {
11   val: u8,
12   bl: bool,
13 }
14
15 #[repr(C)]
16 #[derive(Copy, Clone)]
17 struct SliceRepr {
18     ptr: *const u8,
19     len: usize,
20 }
21
22 #[repr(C)]
23 #[derive(Copy, Clone)]
24 struct BadSliceRepr {
25     ptr: *const u8,
26     len: &'static u8,
27 }
28
29 #[repr(C)]
30 union SliceTransmute {
31     repr: SliceRepr,
32     bad: BadSliceRepr,
33     addr: usize,
34     slice: &'static [u8],
35     raw_slice: *const [u8],
36     str: &'static str,
37     my_str: &'static MyStr,
38     my_slice: &'static MySliceBool,
39 }
40
41 #[repr(C)]
42 #[derive(Copy, Clone)]
43 struct DynRepr {
44     ptr: *const u8,
45     vtable: *const u8,
46 }
47
48 #[repr(C)]
49 #[derive(Copy, Clone)]
50 struct DynRepr2 {
51     ptr: *const u8,
52     vtable: *const u64,
53 }
54
55 #[repr(C)]
56 #[derive(Copy, Clone)]
57 struct BadDynRepr {
58     ptr: *const u8,
59     vtable: usize,
60 }
61
62 #[repr(C)]
63 union DynTransmute {
64     repr: DynRepr,
65     repr2: DynRepr2,
66     bad: BadDynRepr,
67     addr: usize,
68     rust: &'static dyn Trait,
69     raw_rust: *const dyn Trait,
70 }
71
72 trait Trait {}
73 impl Trait for bool {}
74
75 // custom unsized type
76 struct MyStr(str);
77
78 // custom unsized type with sized fields
79 struct MySlice<T: ?Sized>(bool, T);
80 type MySliceBool = MySlice<[bool]>;
81
82 // # str
83 // OK
84 const STR_VALID: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.str};
85 // bad 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
88 // bad str
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
94
95 // invalid UTF-8
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
101
102 // # slice
103 // OK
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
114
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
118
119 // good MySliceBool
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
127
128 // # raw slice
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
134
135 // # trait object
136 // bad trait object
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
139 // bad trait object
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
142 // bad trait object
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
145
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
149
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
156
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
161 };
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
165 };
166
167 fn main() {
168     let _ = RAW_TRAIT_OBJ_VTABLE_NULL;
169     let _ = RAW_TRAIT_OBJ_VTABLE_INVALID;
170 }