Skip to content

Commit bcffdf1

Browse files
committed
Auto merge of #49383 - nox:scalarpair, r=eddyb
Allow niche-filling dataful variants to be represented as a ScalarPair r? @eddyb
2 parents e58df0d + bda718f commit bcffdf1

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Diff for: src/librustc/ty/layout.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -1517,10 +1517,21 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
15171517
let offset = st[i].fields.offset(field_index) + offset;
15181518
let size = st[i].size;
15191519

1520-
let abi = if offset.bytes() == 0 && niche.value.size(dl) == size {
1521-
Abi::Scalar(niche.clone())
1522-
} else {
1523-
Abi::Aggregate { sized: true }
1520+
let abi = match st[i].abi {
1521+
Abi::Scalar(_) => Abi::Scalar(niche.clone()),
1522+
Abi::ScalarPair(ref first, ref second) => {
1523+
// We need to use scalar_unit to reset the
1524+
// valid range to the maximal one for that
1525+
// primitive, because only the niche is
1526+
// guaranteed to be initialised, not the
1527+
// other primitive.
1528+
if offset.bytes() == 0 {
1529+
Abi::ScalarPair(niche.clone(), scalar_unit(second.value))
1530+
} else {
1531+
Abi::ScalarPair(scalar_unit(first.value), niche.clone())
1532+
}
1533+
}
1534+
_ => Abi::Aggregate { sized: true },
15241535
};
15251536

15261537
return Ok(tcx.intern_layout(LayoutDetails {

Diff for: src/test/codegen/function-arguments.rs

+6
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ pub fn trait_borrow(_: &Drop) {
133133
pub fn trait_box(_: Box<Drop>) {
134134
}
135135

136+
// CHECK: { i8*, i8* } @trait_option(i8* noalias %x.0, i8* %x.1)
137+
#[no_mangle]
138+
pub fn trait_option(x: Option<Box<Drop>>) -> Option<Box<Drop>> {
139+
x
140+
}
141+
136142
// CHECK: { [0 x i16]*, [[USIZE]] } @return_slice([0 x i16]* noalias nonnull readonly %x.0, [[USIZE]] %x.1)
137143
#[no_mangle]
138144
pub fn return_slice(x: &[u16]) -> &[u16] {

0 commit comments

Comments
 (0)