Skip to content

Commit 7cfb9e0

Browse files
committed
Auto merge of #151040 - moulins:public-variant-layout, r=makai410
Don't expose redundant information in `rustc_public`'s `LayoutShape` Enum variant layouts don't need to store a full `LayoutShape`; just storing the fields offsets is enough and all other information can be inferred from the parent layout: - size, align and ABI don't make much sense for individual variants and should generally be taken from the parent layout instead; - variants always have `fields: FieldsShape::Arbitrary { .. }` and `variant: VariantShape::Single { .. }`. In principle, the same refactor could be done on `rustc_abi::Layout` (see [this comment](#113988 (comment))) but I prefer starting with this smaller change first.
2 parents 4742769 + 3777ebc commit 7cfb9e0

File tree

2 files changed

+28
-3
lines changed
  • compiler/rustc_public/src

2 files changed

+28
-3
lines changed

compiler/rustc_public/src/abi.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,27 @@ pub enum VariantsShape {
188188
tag: Scalar,
189189
tag_encoding: TagEncoding,
190190
tag_field: usize,
191-
variants: Vec<LayoutShape>,
191+
variants: Vec<VariantFields>,
192192
},
193193
}
194194

195+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
196+
pub struct VariantFields {
197+
/// Offsets for the first byte of each field,
198+
/// ordered to match the source definition order.
199+
/// I.e.: It follows the same order as [super::ty::VariantDef::fields()].
200+
/// This vector does not go in increasing order.
201+
pub offsets: Vec<Size>,
202+
}
203+
204+
impl VariantFields {
205+
pub fn fields_by_offset_order(&self) -> Vec<FieldIdx> {
206+
let mut indices = (0..self.offsets.len()).collect::<Vec<_>>();
207+
indices.sort_by_key(|idx| self.offsets[*idx]);
208+
indices
209+
}
210+
}
211+
195212
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
196213
pub enum TagEncoding {
197214
/// The tag directly stores the discriminant, but possibly with a smaller layout

compiler/rustc_public/src/unstable/convert/stable/abi.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_target::callconv;
1111
use crate::abi::{
1212
AddressSpace, ArgAbi, CallConvention, FieldsShape, FloatLength, FnAbi, IntegerLength,
1313
IntegerType, Layout, LayoutShape, PassMode, Primitive, ReprFlags, ReprOptions, Scalar,
14-
TagEncoding, TyAndLayout, ValueAbi, VariantsShape, WrappingRange,
14+
TagEncoding, TyAndLayout, ValueAbi, VariantFields, VariantsShape, WrappingRange,
1515
};
1616
use crate::compiler_interface::BridgeTys;
1717
use crate::target::MachineSize as Size;
@@ -213,7 +213,15 @@ impl<'tcx> Stable<'tcx> for rustc_abi::Variants<rustc_abi::FieldIdx, rustc_abi::
213213
tag: tag.stable(tables, cx),
214214
tag_encoding: tag_encoding.stable(tables, cx),
215215
tag_field: tag_field.stable(tables, cx),
216-
variants: variants.iter().as_slice().stable(tables, cx),
216+
variants: variants
217+
.iter()
218+
.map(|v| match &v.fields {
219+
rustc_abi::FieldsShape::Arbitrary { offsets, .. } => VariantFields {
220+
offsets: offsets.iter().as_slice().stable(tables, cx),
221+
},
222+
_ => panic!("variant layout should be Arbitrary"),
223+
})
224+
.collect(),
217225
}
218226
}
219227
}

0 commit comments

Comments
 (0)