diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index 420adec456f61..ecb7863304d69 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -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); diff --git a/src/test/ui/unsized/issue-97732.rs b/src/test/ui/unsized/issue-97732.rs new file mode 100644 index 0000000000000..72f765033969a --- /dev/null +++ b/src/test/ui/unsized/issue-97732.rs @@ -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(Box, ()); + +impl CoerceUnsized> for BoxWithZstTail where + Box: CoerceUnsized> +{ +} + +pub fn noop_dyn_upcast_with_zst_tail( + b: BoxWithZstTail, +) -> BoxWithZstTail { + b +} + +fn main() { + let original = "foo"; + let boxed = BoxWithZstTail(Box::new(original) as Box, ()); + let noop_upcasted = noop_dyn_upcast_with_zst_tail(boxed); + assert_eq!(original, noop_upcasted.0.to_string()); +}