Skip to content

Commit 7e25139

Browse files
committed
Auto merge of #46428 - eddyb:scalar-pair-unpacking, r=arielb1
rustc: don't unpack newtypes of scalar-pairs with mismatched alignment. This PR fixes a potential problem where a packed newtype of a pair was also considered a pair, even though it didn't have the required alignment of the pair. cc @oli-obk It's possible miri hit something like this, with an unstable feature, but it's more general.
2 parents 16ba459 + d455955 commit 7e25139

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/librustc/ty/layout.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,9 @@ impl<'a, 'tcx> LayoutDetails {
10791079
// We have exactly one non-ZST field.
10801080
(Some((i, field)), None, None) => {
10811081
// Field fills the struct and it has a scalar or scalar pair ABI.
1082-
if offsets[i].bytes() == 0 && size == field.size {
1082+
if offsets[i].bytes() == 0 &&
1083+
align.abi() == field.align.abi() &&
1084+
size == field.size {
10831085
match field.abi {
10841086
// For plain scalars we can't unpack newtypes
10851087
// for `#[repr(C)]`, as that affects C ABIs.

src/test/codegen/packed.rs

+11
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,14 @@ pub fn pkd_pair(pair1: &mut PackedPair, pair2: &mut PackedPair) {
5757
// CHECK: call void @llvm.memcpy.{{.*}}(i8* %{{.*}}, i8* %{{.*}}, i{{[0-9]+}} 5, i32 1, i1 false)
5858
*pair2 = *pair1;
5959
}
60+
61+
#[repr(packed)]
62+
#[derive(Copy, Clone)]
63+
pub struct PackedNestedPair((u32, u32));
64+
65+
// CHECK-LABEL: @pkd_nested_pair
66+
#[no_mangle]
67+
pub fn pkd_nested_pair(pair1: &mut PackedNestedPair, pair2: &mut PackedNestedPair) {
68+
// CHECK: call void @llvm.memcpy.{{.*}}(i8* %{{.*}}, i8* %{{.*}}, i{{[0-9]+}} 8, i32 1, i1 false)
69+
*pair2 = *pair1;
70+
}

0 commit comments

Comments
 (0)