Skip to content

Commit 5ee555c

Browse files
committed
Only panic for refs to slices and str, not dyn trait
1 parent a007e7b commit 5ee555c

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

compiler/rustc_middle/src/ty/layout.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -3564,10 +3564,16 @@ where
35643564
// fix (0.11, 0.12, 0.13) would panic, as they make uninit &[u8] and &str.
35653565
InitKind::Uninit => {
35663566
if let ty::Ref(_, inner, _) = this.ty.kind() {
3567-
let penv = ty::ParamEnv::reveal_all().and(*inner);
3568-
if let Ok(l) = cx.tcx().layout_of(penv) {
3569-
return l.layout.align().abi == Align::ONE
3570-
&& l.layout.size() == Size::ZERO;
3567+
if let ty::Slice(slice_inner) = inner.kind() {
3568+
let penv = ty::ParamEnv::reveal_all().and(*slice_inner);
3569+
3570+
if let Ok(l) = cx.tcx().layout_of(penv) {
3571+
return l.layout.align().abi == Align::ONE;
3572+
}
3573+
}
3574+
3575+
if ty::Str == *inner.kind() {
3576+
return true;
35713577
}
35723578
}
35733579
}

src/test/ui/intrinsics/panic-uninitialized-zeroed.rs

+32-17
Original file line numberDiff line numberDiff line change
@@ -284,25 +284,23 @@ fn main() {
284284
"attempted to leave type `[core::ptr::non_null::NonNull<()>; 1]` uninitialized, which is invalid"
285285
);
286286

287-
test_strict_panic_msg(
288-
|| mem::zeroed::<LR_NonZero>(),
289-
"attempted to zero-initialize type `LR_NonZero`, which is invalid"
287+
test_panic_msg(
288+
|| mem::uninitialized::<(&[u8], &str, &())>(),
289+
"attempted to leave type `(&[u8], &str, &())` uninitialized, which is invalid"
290290
);
291291

292-
test_strict_panic_msg(
293-
|| mem::zeroed::<[LR_NonZero; 1]>(),
294-
"attempted to zero-initialize type `[LR_NonZero; 1]`, which is invalid"
292+
test_panic_msg(
293+
|| mem::uninitialized::<[&(); 1]>(),
294+
"attempted to leave type `[&(); 1]` uninitialized, which is invalid"
295295
);
296296

297-
test_strict_panic_msg(
298-
|| mem::zeroed::<ManuallyDrop<LR_NonZero>>(),
299-
"attempted to zero-initialize type `core::mem::manually_drop::ManuallyDrop<LR_NonZero>`, \
300-
which is invalid"
297+
test_panic_msg(
298+
|| mem::uninitialized::<[&dyn Send; 1]>(),
299+
"attempted to leave type `[&dyn core::marker::Send; 1]` uninitialized, which is invalid"
301300
);
302-
303301
test_panic_msg(
304-
|| mem::uninitialized::<(&'static [u8], &'static str)>(),
305-
"attempted to leave type `(&[u8], &str)` uninitialized, which is invalid"
302+
|| mem::uninitialized::<[*const dyn Send; 1]>(),
303+
"attempted to leave type `[*const dyn core::marker::Send; 1]` uninitialized, which is invalid"
306304
);
307305

308306
// Some things that should work.
@@ -339,19 +337,36 @@ fn main() {
339337
"attempted to leave type `[i32; 1]` uninitialized, which is invalid"
340338
);
341339

340+
// These are UB, but making them panic breaks too many crates at the moment.
342341
test_strict_panic_msg(
343-
|| mem::uninitialized::<[(&'static [u8], &'static str, &()); 1]>(),
344-
"attempted to leave type `[(&[u8], &str, &()); 1]` uninitialized, which is invalid"
342+
|| mem::zeroed::<[(&[u8], &str); 1]>(),
343+
"attempted to zero-initialize type `[(&[u8], &str); 1]`, which is invalid"
345344
);
346345

347346
test_strict_panic_msg(
348-
|| mem::zeroed::<[(&'static [u8], &'static str); 1]>(),
349-
"attempted to zero-initialize type `[(&[u8], &str); 1]`, which is invalid"
347+
|| mem::uninitialized::<[(&[u8], &str); 1]>(),
348+
"attempted to leave type `[(&[u8], &str); 1]` uninitialized, which is invalid"
350349
);
351350

352351
test_strict_panic_msg(
353352
|| mem::zeroed::<[NonNull<()>; 1]>(),
354353
"attempted to zero-initialize type `[core::ptr::non_null::NonNull<()>; 1]`, which is invalid"
355354
);
355+
356+
test_strict_panic_msg(
357+
|| mem::zeroed::<LR_NonZero>(),
358+
"attempted to zero-initialize type `LR_NonZero`, which is invalid"
359+
);
360+
361+
test_strict_panic_msg(
362+
|| mem::zeroed::<[LR_NonZero; 1]>(),
363+
"attempted to zero-initialize type `[LR_NonZero; 1]`, which is invalid"
364+
);
365+
366+
test_strict_panic_msg(
367+
|| mem::zeroed::<ManuallyDrop<LR_NonZero>>(),
368+
"attempted to zero-initialize type `core::mem::manually_drop::ManuallyDrop<LR_NonZero>`, \
369+
which is invalid"
370+
);
356371
}
357372
}

0 commit comments

Comments
 (0)