Rollup merge of #97738 - Kixiron:zst-panic, r=eddyb

Fix ICEs from zsts within unsized types with non-zero offsets

- Fixes #97732
- Fixes ICEs while compiling `alloc` with `-Z randomize-layout`

r? ``@eddyb``
This commit is contained in:
Dylan DPC 2022-06-07 11:41:08 +02:00 committed by GitHub
commit 62c260de8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -216,11 +216,12 @@ pub fn unsize_ptr<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
let mut result = None;
for i in 0..src_layout.fields.count() {
let src_f = src_layout.field(bx.cx(), i);
assert_eq!(src_layout.fields.offset(i).bytes(), 0);
assert_eq!(dst_layout.fields.offset(i).bytes(), 0);
if src_f.is_zst() {
continue;
}
assert_eq!(src_layout.fields.offset(i).bytes(), 0);
assert_eq!(dst_layout.fields.offset(i).bytes(), 0);
assert_eq!(src_layout.size, src_f.size);
let dst_f = dst_layout.field(bx.cx(), i);

View File

@ -0,0 +1,28 @@
// check-pass
#![feature(coerce_unsized)]
// Ensure that unsizing structs that contain ZSTs at non-zero offsets don't ICE
use std::ops::CoerceUnsized;
#[repr(C)]
pub struct BoxWithZstTail<T: ?Sized>(Box<T>, ());
impl<S: ?Sized, T: ?Sized> CoerceUnsized<BoxWithZstTail<T>> for BoxWithZstTail<S> where
Box<S>: CoerceUnsized<Box<T>>
{
}
pub fn noop_dyn_upcast_with_zst_tail(
b: BoxWithZstTail<dyn ToString + Send>,
) -> BoxWithZstTail<dyn ToString> {
b
}
fn main() {
let original = "foo";
let boxed = BoxWithZstTail(Box::new(original) as Box<dyn ToString + Send>, ());
let noop_upcasted = noop_dyn_upcast_with_zst_tail(boxed);
assert_eq!(original, noop_upcasted.0.to_string());
}