From ba2f14e065ef4a798b23fad8b786616e79c59dd5 Mon Sep 17 00:00:00 2001 From: Chase Wilson Date: Sat, 4 Jun 2022 12:33:01 -0500 Subject: [PATCH 1/3] Fixed premature assertions that caused -Z randomize-layout to fail on alloc --- compiler/rustc_codegen_ssa/src/base.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index 420adec456f6..ecb7863304d6 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); From 857453d36c5b396d2009e08b38b672695c5cddba Mon Sep 17 00:00:00 2001 From: Chase Wilson Date: Sat, 4 Jun 2022 13:33:56 -0500 Subject: [PATCH 2/3] Added test for #97732 --- src/test/ui/unsized/issue-97732.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/test/ui/unsized/issue-97732.rs diff --git a/src/test/ui/unsized/issue-97732.rs b/src/test/ui/unsized/issue-97732.rs new file mode 100644 index 000000000000..172f0724d426 --- /dev/null +++ b/src/test/ui/unsized/issue-97732.rs @@ -0,0 +1,23 @@ +// 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() {} From 10336cf1624ada36e8ba64e851d4df3f2976da81 Mon Sep 17 00:00:00 2001 From: Chase Wilson Date: Mon, 6 Jun 2022 10:19:33 -0500 Subject: [PATCH 3/3] Update src/test/ui/unsized/issue-97732.rs Co-authored-by: Eduard-Mihai Burtescu --- src/test/ui/unsized/issue-97732.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/test/ui/unsized/issue-97732.rs b/src/test/ui/unsized/issue-97732.rs index 172f0724d426..72f765033969 100644 --- a/src/test/ui/unsized/issue-97732.rs +++ b/src/test/ui/unsized/issue-97732.rs @@ -15,9 +15,14 @@ impl CoerceUnsized> for BoxWithZstTail, -) -> BoxWithZstTail { + b: BoxWithZstTail, +) -> BoxWithZstTail { b } -fn main() {} +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()); +}