@@ -65,59 +65,111 @@ fn main() {
65
65
|| mem:: uninitialized :: < !> ( ) ,
66
66
"attempted to instantiate uninhabited type `!`"
67
67
) ;
68
+ test_panic_msg (
69
+ || mem:: uninitialized :: < [ !; 2 ] > ( ) ,
70
+ "attempted to instantiate uninhabited type `[!; 2]`"
71
+ ) ;
68
72
test_panic_msg (
69
73
|| mem:: zeroed :: < !> ( ) ,
70
74
"attempted to instantiate uninhabited type `!`"
71
75
) ;
76
+ test_panic_msg (
77
+ || mem:: zeroed :: < [ !; 2 ] > ( ) ,
78
+ "attempted to instantiate uninhabited type `[!; 2]`"
79
+ ) ;
72
80
test_panic_msg (
73
81
|| MaybeUninit :: < !> :: uninit ( ) . assume_init ( ) ,
74
82
"attempted to instantiate uninhabited type `!`"
75
83
) ;
84
+ test_panic_msg (
85
+ || MaybeUninit :: < [ !; 2 ] > :: uninit ( ) . assume_init ( ) ,
86
+ "attempted to instantiate uninhabited type `[!; 2]`"
87
+ ) ;
76
88
77
89
test_panic_msg (
78
90
|| mem:: uninitialized :: < Foo > ( ) ,
79
91
"attempted to instantiate uninhabited type `Foo`"
80
92
) ;
93
+ test_panic_msg (
94
+ || mem:: uninitialized :: < [ Foo ; 2 ] > ( ) ,
95
+ "attempted to instantiate uninhabited type `[Foo; 2]`"
96
+ ) ;
81
97
test_panic_msg (
82
98
|| mem:: zeroed :: < Foo > ( ) ,
83
99
"attempted to instantiate uninhabited type `Foo`"
84
100
) ;
101
+ test_panic_msg (
102
+ || mem:: zeroed :: < [ Foo ; 2 ] > ( ) ,
103
+ "attempted to instantiate uninhabited type `[Foo; 2]`"
104
+ ) ;
85
105
test_panic_msg (
86
106
|| MaybeUninit :: < Foo > :: uninit ( ) . assume_init ( ) ,
87
107
"attempted to instantiate uninhabited type `Foo`"
88
108
) ;
109
+ test_panic_msg (
110
+ || MaybeUninit :: < [ Foo ; 2 ] > :: uninit ( ) . assume_init ( ) ,
111
+ "attempted to instantiate uninhabited type `[Foo; 2]`"
112
+ ) ;
89
113
90
114
test_panic_msg (
91
115
|| mem:: uninitialized :: < Bar > ( ) ,
92
116
"attempted to instantiate uninhabited type `Bar`"
93
117
) ;
118
+ test_panic_msg (
119
+ || mem:: uninitialized :: < [ Bar ; 4 ] > ( ) ,
120
+ "attempted to instantiate uninhabited type `[Bar; 4]`"
121
+ ) ;
94
122
test_panic_msg (
95
123
|| mem:: zeroed :: < Bar > ( ) ,
96
124
"attempted to instantiate uninhabited type `Bar`"
97
125
) ;
126
+ test_panic_msg (
127
+ || mem:: zeroed :: < [ Bar ; 4 ] > ( ) ,
128
+ "attempted to instantiate uninhabited type `[Bar; 4]`"
129
+ ) ;
98
130
test_panic_msg (
99
131
|| MaybeUninit :: < Bar > :: uninit ( ) . assume_init ( ) ,
100
132
"attempted to instantiate uninhabited type `Bar`"
101
133
) ;
134
+ test_panic_msg (
135
+ || MaybeUninit :: < [ Bar ; 4 ] > :: uninit ( ) . assume_init ( ) ,
136
+ "attempted to instantiate uninhabited type `[Bar; 4]`"
137
+ ) ;
102
138
103
- // Types that do not like zero-initialziation
139
+ // Types that do not like zero-initialization
104
140
test_panic_msg (
105
141
|| mem:: uninitialized :: < fn ( ) > ( ) ,
106
142
"attempted to leave type `fn()` uninitialized, which is invalid"
107
143
) ;
144
+ test_panic_msg (
145
+ || mem:: uninitialized :: < [ fn ( ) ; 2 ] > ( ) ,
146
+ "attempted to leave type `[fn(); 2]` uninitialized, which is invalid"
147
+ ) ;
108
148
test_panic_msg (
109
149
|| mem:: zeroed :: < fn ( ) > ( ) ,
110
150
"attempted to zero-initialize type `fn()`, which is invalid"
111
151
) ;
152
+ test_panic_msg (
153
+ || mem:: zeroed :: < [ fn ( ) ; 2 ] > ( ) ,
154
+ "attempted to zero-initialize type `[fn(); 2]`, which is invalid"
155
+ ) ;
112
156
113
157
test_panic_msg (
114
158
|| mem:: uninitialized :: < * const dyn Send > ( ) ,
115
159
"attempted to leave type `*const dyn std::marker::Send` uninitialized, which is invalid"
116
160
) ;
161
+ test_panic_msg (
162
+ || mem:: uninitialized :: < [ * const dyn Send ; 2 ] > ( ) ,
163
+ "attempted to leave type `[*const dyn std::marker::Send; 2]` uninitialized, which is invalid"
164
+ ) ;
117
165
test_panic_msg (
118
166
|| mem:: zeroed :: < * const dyn Send > ( ) ,
119
167
"attempted to zero-initialize type `*const dyn std::marker::Send`, which is invalid"
120
168
) ;
169
+ test_panic_msg (
170
+ || mem:: zeroed :: < [ * const dyn Send ; 2 ] > ( ) ,
171
+ "attempted to zero-initialize type `[*const dyn std::marker::Send; 2]`, which is invalid"
172
+ ) ;
121
173
122
174
/* FIXME(#66151) we conservatively do not error here yet.
123
175
test_panic_msg(
@@ -146,39 +198,73 @@ fn main() {
146
198
"attempted to leave type `(std::ptr::NonNull<u32>, u32, u32)` uninitialized, \
147
199
which is invalid"
148
200
) ;
201
+ test_panic_msg (
202
+ || mem:: uninitialized :: < [ ( NonNull < u32 > , u32 , u32 ) ; 2 ] > ( ) ,
203
+ "attempted to leave type `[(std::ptr::NonNull<u32>, u32, u32); 2]` uninitialized, \
204
+ which is invalid"
205
+ ) ;
149
206
test_panic_msg (
150
207
|| mem:: zeroed :: < ( NonNull < u32 > , u32 , u32 ) > ( ) ,
151
208
"attempted to zero-initialize type `(std::ptr::NonNull<u32>, u32, u32)`, \
152
209
which is invalid"
153
210
) ;
211
+ test_panic_msg (
212
+ || mem:: zeroed :: < [ ( NonNull < u32 > , u32 , u32 ) ; 2 ] > ( ) ,
213
+ "attempted to zero-initialize type `[(std::ptr::NonNull<u32>, u32, u32); 2]`, \
214
+ which is invalid"
215
+ ) ;
154
216
155
217
test_panic_msg (
156
218
|| mem:: uninitialized :: < OneVariant_NonZero > ( ) ,
157
219
"attempted to leave type `OneVariant_NonZero` uninitialized, \
158
220
which is invalid"
159
221
) ;
222
+ test_panic_msg (
223
+ || mem:: uninitialized :: < [ OneVariant_NonZero ; 2 ] > ( ) ,
224
+ "attempted to leave type `[OneVariant_NonZero; 2]` uninitialized, \
225
+ which is invalid"
226
+ ) ;
160
227
test_panic_msg (
161
228
|| mem:: zeroed :: < OneVariant_NonZero > ( ) ,
162
229
"attempted to zero-initialize type `OneVariant_NonZero`, \
163
230
which is invalid"
164
231
) ;
232
+ test_panic_msg (
233
+ || mem:: zeroed :: < [ OneVariant_NonZero ; 2 ] > ( ) ,
234
+ "attempted to zero-initialize type `[OneVariant_NonZero; 2]`, \
235
+ which is invalid"
236
+ ) ;
165
237
166
238
test_panic_msg (
167
239
|| mem:: uninitialized :: < NoNullVariant > ( ) ,
168
240
"attempted to leave type `NoNullVariant` uninitialized, \
169
241
which is invalid"
170
242
) ;
243
+ test_panic_msg (
244
+ || mem:: uninitialized :: < [ NoNullVariant ; 2 ] > ( ) ,
245
+ "attempted to leave type `[NoNullVariant; 2]` uninitialized, \
246
+ which is invalid"
247
+ ) ;
171
248
test_panic_msg (
172
249
|| mem:: zeroed :: < NoNullVariant > ( ) ,
173
250
"attempted to zero-initialize type `NoNullVariant`, \
174
251
which is invalid"
175
252
) ;
253
+ test_panic_msg (
254
+ || mem:: zeroed :: < [ NoNullVariant ; 2 ] > ( ) ,
255
+ "attempted to zero-initialize type `[NoNullVariant; 2]`, \
256
+ which is invalid"
257
+ ) ;
176
258
177
259
// Types that can be zero, but not uninit.
178
260
test_panic_msg (
179
261
|| mem:: uninitialized :: < bool > ( ) ,
180
262
"attempted to leave type `bool` uninitialized, which is invalid"
181
263
) ;
264
+ test_panic_msg (
265
+ || mem:: uninitialized :: < [ bool ; 2 ] > ( ) ,
266
+ "attempted to leave type `[bool; 2]` uninitialized, which is invalid"
267
+ ) ;
182
268
test_panic_msg (
183
269
|| mem:: uninitialized :: < LR > ( ) ,
184
270
"attempted to leave type `LR` uninitialized, which is invalid"
@@ -190,17 +276,31 @@ fn main() {
190
276
191
277
// Some things that should work.
192
278
let _val = mem:: zeroed :: < bool > ( ) ;
279
+ let _val = mem:: zeroed :: < [ bool ; 4 ] > ( ) ;
193
280
let _val = mem:: zeroed :: < LR > ( ) ;
281
+ let _val = mem:: zeroed :: < [ LR ; 8 ] > ( ) ;
194
282
let _val = mem:: zeroed :: < ManuallyDrop < LR > > ( ) ;
283
+ let _val = mem:: zeroed :: < [ ManuallyDrop < LR > ; 16 ] > ( ) ;
195
284
let _val = mem:: zeroed :: < OneVariant > ( ) ;
285
+ let _val = mem:: zeroed :: < [ OneVariant ; 2 ] > ( ) ;
196
286
let _val = mem:: zeroed :: < Option < & ' static i32 > > ( ) ;
287
+ let _val = mem:: zeroed :: < [ Option < & ' static i32 > ; 3 ] > ( ) ;
197
288
let _val = mem:: zeroed :: < MaybeUninit < NonNull < u32 > > > ( ) ;
289
+ let _val = mem:: zeroed :: < [ MaybeUninit < NonNull < u32 > > ; 32 ] > ( ) ;
290
+ let _val = mem:: zeroed :: < [ !; 0 ] > ( ) ;
198
291
let _val = mem:: uninitialized :: < MaybeUninit < bool > > ( ) ;
292
+ let _val = mem:: uninitialized :: < [ MaybeUninit < bool > ; 1 ] > ( ) ;
293
+ let _val = mem:: uninitialized :: < [ bool ; 0 ] > ( ) ;
294
+ let _val = mem:: uninitialized :: < [ !; 0 ] > ( ) ;
295
+ let _val = mem:: uninitialized :: < [ fn ( ) ; 0 ] > ( ) ;
296
+ let _val = mem:: uninitialized :: < [ * const dyn Send ; 0 ] > ( ) ;
199
297
200
298
// These are UB because they have not been officially blessed, but we await the resolution
201
299
// of <https://github.com/rust-lang/unsafe-code-guidelines/issues/71> before doing
202
300
// anything about that.
203
301
let _val = mem:: uninitialized :: < i32 > ( ) ;
302
+ let _val = mem:: uninitialized :: < [ i32 ; 1 ] > ( ) ;
204
303
let _val = mem:: uninitialized :: < * const ( ) > ( ) ;
304
+ let _val = mem:: uninitialized :: < [ * const ( ) ; 2 ] > ( ) ;
205
305
}
206
306
}
0 commit comments