@@ -635,9 +635,11 @@ pub const FAT_PTR_EXTRA: usize = 1;
635
635
/// Describes how the fields of a type are located in memory.
636
636
#[ derive( PartialEq , Eq , Hash , Debug ) ]
637
637
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 {
641
643
stride : Size ,
642
644
count : u64
643
645
} ,
@@ -664,16 +666,10 @@ pub enum FieldPlacement {
664
666
}
665
667
666
668
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
-
674
669
pub fn count ( & self ) -> usize {
675
670
match * self {
676
- FieldPlacement :: Linear { count, .. } => {
671
+ FieldPlacement :: Union ( count) => count,
672
+ FieldPlacement :: Array { count, .. } => {
677
673
let usize_count = count as usize ;
678
674
assert_eq ! ( usize_count as u64 , count) ;
679
675
usize_count
@@ -684,7 +680,8 @@ impl FieldPlacement {
684
680
685
681
pub fn offset ( & self , i : usize ) -> Size {
686
682
match * self {
687
- FieldPlacement :: Linear { stride, count } => {
683
+ FieldPlacement :: Union ( _) => Size :: from_bytes ( 0 ) ,
684
+ FieldPlacement :: Array { stride, count } => {
688
685
let i = i as u64 ;
689
686
assert ! ( i < count) ;
690
687
stride * i
@@ -695,7 +692,8 @@ impl FieldPlacement {
695
692
696
693
pub fn memory_index ( & self , i : usize ) -> usize {
697
694
match * self {
698
- FieldPlacement :: Linear { .. } => i,
695
+ FieldPlacement :: Union ( _) |
696
+ FieldPlacement :: Array { .. } => i,
699
697
FieldPlacement :: Arbitrary { ref memory_index, .. } => {
700
698
let r = memory_index[ i] ;
701
699
assert_eq ! ( r as usize as u32 , r) ;
@@ -727,7 +725,8 @@ impl FieldPlacement {
727
725
728
726
( 0 ..self . count ( ) ) . map ( move |i| {
729
727
match * self {
730
- FieldPlacement :: Linear { .. } => i,
728
+ FieldPlacement :: Union ( _) |
729
+ FieldPlacement :: Array { .. } => i,
731
730
FieldPlacement :: Arbitrary { .. } => {
732
731
if use_small { inverse_small[ i] as usize }
733
732
else { inverse_big[ i] as usize }
@@ -945,7 +944,7 @@ impl<'a, 'tcx> Layout {
945
944
let scalar = |value| {
946
945
tcx. intern_layout ( CachedLayout {
947
946
layout : Layout :: Scalar ,
948
- fields : FieldPlacement :: union ( 0 ) ,
947
+ fields : FieldPlacement :: Union ( 0 ) ,
949
948
abi : Abi :: Scalar ( value)
950
949
} )
951
950
} ;
@@ -1118,12 +1117,12 @@ impl<'a, 'tcx> Layout {
1118
1117
1119
1118
// Effectively a (ptr, meta) tuple.
1120
1119
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) ;
1126
1121
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
+ } ;
1127
1126
Ok ( tcx. intern_layout ( CachedLayout {
1128
1127
layout : Layout :: FatPointer ,
1129
1128
fields,
@@ -1182,7 +1181,7 @@ impl<'a, 'tcx> Layout {
1182
1181
1183
1182
tcx. intern_layout ( CachedLayout {
1184
1183
layout : Layout :: Array ,
1185
- fields : FieldPlacement :: Linear {
1184
+ fields : FieldPlacement :: Array {
1186
1185
stride : element_size,
1187
1186
count
1188
1187
} ,
@@ -1199,7 +1198,7 @@ impl<'a, 'tcx> Layout {
1199
1198
let element = cx. layout_of ( element) ?;
1200
1199
tcx. intern_layout ( CachedLayout {
1201
1200
layout : Layout :: Array ,
1202
- fields : FieldPlacement :: Linear {
1201
+ fields : FieldPlacement :: Array {
1203
1202
stride : element. size ( dl) ,
1204
1203
count : 0
1205
1204
} ,
@@ -1215,7 +1214,7 @@ impl<'a, 'tcx> Layout {
1215
1214
ty:: TyStr => {
1216
1215
tcx. intern_layout ( CachedLayout {
1217
1216
layout : Layout :: Array ,
1218
- fields : FieldPlacement :: Linear {
1217
+ fields : FieldPlacement :: Array {
1219
1218
stride : Size :: from_bytes ( 1 ) ,
1220
1219
count : 0
1221
1220
} ,
@@ -1283,7 +1282,7 @@ impl<'a, 'tcx> Layout {
1283
1282
} ;
1284
1283
tcx. intern_layout ( CachedLayout {
1285
1284
layout : Layout :: Vector ,
1286
- fields : FieldPlacement :: Linear {
1285
+ fields : FieldPlacement :: Array {
1287
1286
stride : element. size ( tcx) ,
1288
1287
count
1289
1288
} ,
@@ -1340,7 +1339,7 @@ impl<'a, 'tcx> Layout {
1340
1339
1341
1340
return Ok ( tcx. intern_layout ( CachedLayout {
1342
1341
layout : Layout :: UntaggedUnion ,
1343
- fields : FieldPlacement :: union ( variants[ 0 ] . len ( ) ) ,
1342
+ fields : FieldPlacement :: Union ( variants[ 0 ] . len ( ) ) ,
1344
1343
abi : Abi :: Aggregate {
1345
1344
sized : true ,
1346
1345
packed,
@@ -2282,7 +2281,10 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for FieldPlacement {
2282
2281
mem:: discriminant ( self ) . hash_stable ( hcx, hasher) ;
2283
2282
2284
2283
match * self {
2285
- Linear { count, stride } => {
2284
+ Union ( count) => {
2285
+ count. hash_stable ( hcx, hasher) ;
2286
+ }
2287
+ Array { count, stride } => {
2286
2288
count. hash_stable ( hcx, hasher) ;
2287
2289
stride. hash_stable ( hcx, hasher) ;
2288
2290
}
0 commit comments