Skip to content

Commit fad9954

Browse files
committed
rustc: split layout::FieldPlacement::Linear back into Union and Array.
1 parent 18d54aa commit fad9954

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed

src/librustc/ty/layout.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,11 @@ pub const FAT_PTR_EXTRA: usize = 1;
635635
/// Describes how the fields of a type are located in memory.
636636
#[derive(PartialEq, Eq, Hash, Debug)]
637637
pub enum FieldPlacement {
638-
/// Array-like placement. Can also express
639-
/// unions, by using a stride of zero bytes.
640-
Linear {
638+
/// All fields start at no offset. The `usize` is the field count.
639+
Union(usize),
640+
641+
/// Array/vector-like placement, with all fields of identical types.
642+
Array {
641643
stride: Size,
642644
count: u64
643645
},
@@ -664,16 +666,10 @@ pub enum FieldPlacement {
664666
}
665667

666668
impl FieldPlacement {
667-
pub fn union(count: usize) -> Self {
668-
FieldPlacement::Linear {
669-
stride: Size::from_bytes(0),
670-
count: count as u64
671-
}
672-
}
673-
674669
pub fn count(&self) -> usize {
675670
match *self {
676-
FieldPlacement::Linear { count, .. } => {
671+
FieldPlacement::Union(count) => count,
672+
FieldPlacement::Array { count, .. } => {
677673
let usize_count = count as usize;
678674
assert_eq!(usize_count as u64, count);
679675
usize_count
@@ -684,7 +680,8 @@ impl FieldPlacement {
684680

685681
pub fn offset(&self, i: usize) -> Size {
686682
match *self {
687-
FieldPlacement::Linear { stride, count } => {
683+
FieldPlacement::Union(_) => Size::from_bytes(0),
684+
FieldPlacement::Array { stride, count } => {
688685
let i = i as u64;
689686
assert!(i < count);
690687
stride * i
@@ -695,7 +692,8 @@ impl FieldPlacement {
695692

696693
pub fn memory_index(&self, i: usize) -> usize {
697694
match *self {
698-
FieldPlacement::Linear { .. } => i,
695+
FieldPlacement::Union(_) |
696+
FieldPlacement::Array { .. } => i,
699697
FieldPlacement::Arbitrary { ref memory_index, .. } => {
700698
let r = memory_index[i];
701699
assert_eq!(r as usize as u32, r);
@@ -727,7 +725,8 @@ impl FieldPlacement {
727725

728726
(0..self.count()).map(move |i| {
729727
match *self {
730-
FieldPlacement::Linear { .. } => i,
728+
FieldPlacement::Union(_) |
729+
FieldPlacement::Array { .. } => i,
731730
FieldPlacement::Arbitrary { .. } => {
732731
if use_small { inverse_small[i] as usize }
733732
else { inverse_big[i] as usize }
@@ -945,7 +944,7 @@ impl<'a, 'tcx> Layout {
945944
let scalar = |value| {
946945
tcx.intern_layout(CachedLayout {
947946
layout: Layout::Scalar,
948-
fields: FieldPlacement::union(0),
947+
fields: FieldPlacement::Union(0),
949948
abi: Abi::Scalar(value)
950949
})
951950
};
@@ -1118,12 +1117,12 @@ impl<'a, 'tcx> Layout {
11181117

11191118
// Effectively a (ptr, meta) tuple.
11201119
let align = Pointer.align(dl).max(metadata.align(dl));
1121-
let fields = FieldPlacement::Linear {
1122-
stride: Pointer.size(dl),
1123-
count: 2
1124-
};
1125-
let meta_offset = fields.offset(1);
1120+
let meta_offset = Pointer.size(dl);
11261121
assert_eq!(meta_offset, meta_offset.abi_align(metadata.align(dl)));
1122+
let fields = FieldPlacement::Arbitrary {
1123+
offsets: vec![Size::from_bytes(0), meta_offset],
1124+
memory_index: vec![0, 1]
1125+
};
11271126
Ok(tcx.intern_layout(CachedLayout {
11281127
layout: Layout::FatPointer,
11291128
fields,
@@ -1182,7 +1181,7 @@ impl<'a, 'tcx> Layout {
11821181

11831182
tcx.intern_layout(CachedLayout {
11841183
layout: Layout::Array,
1185-
fields: FieldPlacement::Linear {
1184+
fields: FieldPlacement::Array {
11861185
stride: element_size,
11871186
count
11881187
},
@@ -1199,7 +1198,7 @@ impl<'a, 'tcx> Layout {
11991198
let element = cx.layout_of(element)?;
12001199
tcx.intern_layout(CachedLayout {
12011200
layout: Layout::Array,
1202-
fields: FieldPlacement::Linear {
1201+
fields: FieldPlacement::Array {
12031202
stride: element.size(dl),
12041203
count: 0
12051204
},
@@ -1215,7 +1214,7 @@ impl<'a, 'tcx> Layout {
12151214
ty::TyStr => {
12161215
tcx.intern_layout(CachedLayout {
12171216
layout: Layout::Array,
1218-
fields: FieldPlacement::Linear {
1217+
fields: FieldPlacement::Array {
12191218
stride: Size::from_bytes(1),
12201219
count: 0
12211220
},
@@ -1283,7 +1282,7 @@ impl<'a, 'tcx> Layout {
12831282
};
12841283
tcx.intern_layout(CachedLayout {
12851284
layout: Layout::Vector,
1286-
fields: FieldPlacement::Linear {
1285+
fields: FieldPlacement::Array {
12871286
stride: element.size(tcx),
12881287
count
12891288
},
@@ -1340,7 +1339,7 @@ impl<'a, 'tcx> Layout {
13401339

13411340
return Ok(tcx.intern_layout(CachedLayout {
13421341
layout: Layout::UntaggedUnion,
1343-
fields: FieldPlacement::union(variants[0].len()),
1342+
fields: FieldPlacement::Union(variants[0].len()),
13441343
abi: Abi::Aggregate {
13451344
sized: true,
13461345
packed,
@@ -2282,7 +2281,10 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for FieldPlacement {
22822281
mem::discriminant(self).hash_stable(hcx, hasher);
22832282

22842283
match *self {
2285-
Linear { count, stride } => {
2284+
Union(count) => {
2285+
count.hash_stable(hcx, hasher);
2286+
}
2287+
Array { count, stride } => {
22862288
count.hash_stable(hcx, hasher);
22872289
stride.hash_stable(hcx, hasher);
22882290
}

src/librustc_trans/abi.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use type_::Type;
3535

3636
use rustc::hir;
3737
use rustc::ty::{self, Ty};
38-
use rustc::ty::layout::{self, Align, Layout, Size, FullLayout};
38+
use rustc::ty::layout::{self, Align, Size, FullLayout};
3939
use rustc::ty::layout::{HasDataLayout, LayoutOf};
4040
use rustc_back::PanicStrategy;
4141

@@ -307,19 +307,18 @@ impl<'tcx> LayoutExt<'tcx> for FullLayout<'tcx> {
307307
}
308308

309309
layout::Abi::Aggregate { .. } => {
310-
if let Layout::Array { .. } = *self.layout {
311-
if self.fields.count() > 0 {
312-
return self.field(ccx, 0).homogeneous_aggregate(ccx);
313-
}
314-
}
315-
316310
let mut total = Size::from_bytes(0);
317311
let mut result = None;
318312

319313
let is_union = match *self.fields {
320-
layout::FieldPlacement::Linear { stride, .. } => {
321-
stride.bytes() == 0
314+
layout::FieldPlacement::Array { count, .. } => {
315+
if count > 0 {
316+
return self.field(ccx, 0).homogeneous_aggregate(ccx);
317+
} else {
318+
return None;
319+
}
322320
}
321+
layout::FieldPlacement::Union(_) => true,
323322
layout::FieldPlacement::Arbitrary { .. } => false
324323
};
325324

0 commit comments

Comments
 (0)