Skip to content

Commit

Permalink
Rollup merge of rust-lang#97738 - Kixiron:zst-panic, r=eddyb
Browse files Browse the repository at this point in the history
Fix ICEs from zsts within unsized types with non-zero offsets

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

r? `@eddyb`
  • Loading branch information
Dylan-DPC authored Jun 7, 2022
2 parents 4d14dc3 + 10336cf commit 0ffca47
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions src/test/ui/unsized/issue-97732.rs
Original file line number Diff line number Diff line change
@@ -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());
}

0 comments on commit 0ffca47

Please sign in to comment.