Skip to content

Commit

Permalink
fix computing the offset of an unsized field in a packed struct
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Dec 18, 2023
1 parent 1ab05b6 commit 973dd56
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 0 additions & 2 deletions scripts/test_rustc_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/unsize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
19 changes: 13 additions & 6 deletions src/value_and_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 973dd56

Please sign in to comment.