@@ -57,6 +57,13 @@ enum LR_NonZero {
57
57
58
58
struct ZeroSized ;
59
59
60
+ #[ allow( dead_code) ]
61
+ #[ repr( i32 ) ]
62
+ enum ZeroIsValid {
63
+ Zero ( u8 ) = 0 ,
64
+ One ( NonNull < ( ) > ) = 1 ,
65
+ }
66
+
60
67
fn test_panic_msg < T > ( op : impl ( FnOnce ( ) -> T ) + panic:: UnwindSafe , msg : & str ) {
61
68
let err = panic:: catch_unwind ( op) . err ( ) ;
62
69
assert_eq ! (
@@ -152,33 +159,12 @@ fn main() {
152
159
"attempted to zero-initialize type `*const dyn core::marker::Send`, which is invalid"
153
160
) ;
154
161
155
- /* FIXME(#66151) we conservatively do not error here yet.
156
- test_panic_msg(
157
- || mem::uninitialized::<LR_NonZero>(),
158
- "attempted to leave type `LR_NonZero` uninitialized, which is invalid"
159
- );
160
- test_panic_msg(
161
- || mem::zeroed::<LR_NonZero>(),
162
- "attempted to zero-initialize type `LR_NonZero`, which is invalid"
163
- );
164
-
165
- test_panic_msg(
166
- || mem::uninitialized::<ManuallyDrop<LR_NonZero>>(),
167
- "attempted to leave type `std::mem::ManuallyDrop<LR_NonZero>` uninitialized, \
168
- which is invalid"
169
- );
170
- test_panic_msg(
171
- || mem::zeroed::<ManuallyDrop<LR_NonZero>>(),
172
- "attempted to zero-initialize type `std::mem::ManuallyDrop<LR_NonZero>`, \
173
- which is invalid"
174
- );
175
- */
176
-
177
162
test_panic_msg (
178
163
|| mem:: uninitialized :: < ( NonNull < u32 > , u32 , u32 ) > ( ) ,
179
164
"attempted to leave type `(core::ptr::non_null::NonNull<u32>, u32, u32)` uninitialized, \
180
165
which is invalid"
181
166
) ;
167
+
182
168
test_panic_msg (
183
169
|| mem:: zeroed :: < ( NonNull < u32 > , u32 , u32 ) > ( ) ,
184
170
"attempted to zero-initialize type `(core::ptr::non_null::NonNull<u32>, u32, u32)`, \
@@ -196,11 +182,23 @@ fn main() {
196
182
which is invalid"
197
183
) ;
198
184
185
+ test_panic_msg (
186
+ || mem:: uninitialized :: < LR_NonZero > ( ) ,
187
+ "attempted to leave type `LR_NonZero` uninitialized, which is invalid"
188
+ ) ;
189
+
190
+ test_panic_msg (
191
+ || mem:: uninitialized :: < ManuallyDrop < LR_NonZero > > ( ) ,
192
+ "attempted to leave type `core::mem::manually_drop::ManuallyDrop<LR_NonZero>` uninitialized, \
193
+ which is invalid"
194
+ ) ;
195
+
199
196
test_panic_msg (
200
197
|| mem:: uninitialized :: < NoNullVariant > ( ) ,
201
198
"attempted to leave type `NoNullVariant` uninitialized, \
202
199
which is invalid"
203
200
) ;
201
+
204
202
test_panic_msg (
205
203
|| mem:: zeroed :: < NoNullVariant > ( ) ,
206
204
"attempted to zero-initialize type `NoNullVariant`, \
@@ -212,10 +210,12 @@ fn main() {
212
210
|| mem:: uninitialized :: < bool > ( ) ,
213
211
"attempted to leave type `bool` uninitialized, which is invalid"
214
212
) ;
213
+
215
214
test_panic_msg (
216
215
|| mem:: uninitialized :: < LR > ( ) ,
217
216
"attempted to leave type `LR` uninitialized, which is invalid"
218
217
) ;
218
+
219
219
test_panic_msg (
220
220
|| mem:: uninitialized :: < ManuallyDrop < LR > > ( ) ,
221
221
"attempted to leave type `core::mem::manually_drop::ManuallyDrop<LR>` uninitialized, which is invalid"
@@ -229,6 +229,7 @@ fn main() {
229
229
let _val = mem:: zeroed :: < Option < & ' static i32 > > ( ) ;
230
230
let _val = mem:: zeroed :: < MaybeUninit < NonNull < u32 > > > ( ) ;
231
231
let _val = mem:: zeroed :: < [ !; 0 ] > ( ) ;
232
+ let _val = mem:: zeroed :: < ZeroIsValid > ( ) ;
232
233
let _val = mem:: uninitialized :: < MaybeUninit < bool > > ( ) ;
233
234
let _val = mem:: uninitialized :: < [ !; 0 ] > ( ) ;
234
235
let _val = mem:: uninitialized :: < ( ) > ( ) ;
@@ -259,12 +260,32 @@ fn main() {
259
260
|| mem:: zeroed :: < [ NonNull < ( ) > ; 1 ] > ( ) ,
260
261
"attempted to zero-initialize type `[core::ptr::non_null::NonNull<()>; 1]`, which is invalid"
261
262
) ;
263
+
264
+ // FIXME(#66151) we conservatively do not error here yet (by default).
265
+ test_panic_msg (
266
+ || mem:: zeroed :: < LR_NonZero > ( ) ,
267
+ "attempted to zero-initialize type `LR_NonZero`, which is invalid"
268
+ ) ;
269
+
270
+ test_panic_msg (
271
+ || mem:: zeroed :: < ManuallyDrop < LR_NonZero > > ( ) ,
272
+ "attempted to zero-initialize type `core::mem::manually_drop::ManuallyDrop<LR_NonZero>`, \
273
+ which is invalid"
274
+ ) ;
262
275
} else {
263
276
// These are UB because they have not been officially blessed, but we await the resolution
264
277
// of <https://github.com/rust-lang/unsafe-code-guidelines/issues/71> before doing
265
278
// anything about that.
266
279
let _val = mem:: uninitialized :: < i32 > ( ) ;
267
280
let _val = mem:: uninitialized :: < * const ( ) > ( ) ;
281
+
282
+ // These are UB, but best to test them to ensure we don't become unintentionally
283
+ // stricter.
284
+
285
+ // It's currently unchecked to create invalid enums and values inside arrays.
286
+ let _val = mem:: zeroed :: < LR_NonZero > ( ) ;
287
+ let _val = mem:: zeroed :: < [ LR_NonZero ; 1 ] > ( ) ;
288
+ let _val = mem:: zeroed :: < [ NonNull < ( ) > ; 1 ] > ( ) ;
268
289
}
269
290
}
270
291
}
0 commit comments