fix computing the offset of an unsized field in a packed struct

cc rust-lang/rust#118540
Fixes rust-lang/rustc_codegen_cranelift#1435
This commit is contained in:
bjorn3 2023-12-18 18:12:29 +00:00
parent 1ab05b6cbe
commit 973dd562e8
3 changed files with 14 additions and 10 deletions

View File

@ -140,8 +140,6 @@ rm -r tests/run-make/extern-fn-explicit-align # argument alignment not yet suppo
rm tests/ui/codegen/subtyping-enforces-type-equality.rs # assert_assignable bug with Coroutine's
rm tests/ui/packed/issue-118537-field-offset-ice.rs # rust-lang/rust#118540
# bugs in the test suite
# ======================
rm tests/ui/backtrace.rs # TODO warning

View File

@ -274,8 +274,7 @@ pub(crate) fn size_and_align_of<'tcx>(
} else {
// We have to dynamically compute `min(unsized_align, packed)`.
let packed = fx.bcx.ins().iconst(fx.pointer_type, packed.bytes() as i64);
let cmp =
fx.bcx.ins().icmp(IntCC::UnsignedGreaterThan, unsized_align, packed);
let cmp = fx.bcx.ins().icmp(IntCC::UnsignedLessThan, unsized_align, packed);
unsized_align = fx.bcx.ins().select(cmp, unsized_align, packed);
}
}

View File

@ -25,15 +25,22 @@ fn codegen_field<'tcx>(
}
match field_layout.ty.kind() {
ty::Slice(..) | ty::Str => simple(fx),
ty::Adt(def, _) if def.repr().packed() => {
assert_eq!(layout.align.abi.bytes(), 1);
simple(fx)
}
_ => {
// We have to align the offset for DST's
let unaligned_offset = field_offset.bytes();
let (_, unsized_align) = crate::unsize::size_and_align_of(fx, field_layout, extra);
// Get the alignment of the field
let (_, mut unsized_align) = crate::unsize::size_and_align_of(fx, field_layout, extra);
// For packed types, we need to cap alignment.
if let ty::Adt(def, _) = layout.ty.kind() {
if let Some(packed) = def.repr().pack {
let packed = fx.bcx.ins().iconst(fx.pointer_type, packed.bytes() as i64);
let cmp = fx.bcx.ins().icmp(IntCC::UnsignedLessThan, unsized_align, packed);
unsized_align = fx.bcx.ins().select(cmp, unsized_align, packed);
}
}
// Bump the unaligned offset up to the appropriate alignment
let one = fx.bcx.ins().iconst(fx.pointer_type, 1);
let align_sub_1 = fx.bcx.ins().isub(unsized_align, one);
let and_lhs = fx.bcx.ins().iadd_imm(align_sub_1, unaligned_offset as i64);