@@ -339,6 +339,16 @@ impl Integer {
339339 }
340340 }
341341
342+ pub fn align ( & self , dl : & TargetDataLayout ) -> Align {
343+ match * self {
344+ I1 => dl. i1_align ,
345+ I8 => dl. i8_align ,
346+ I16 => dl. i16_align ,
347+ I32 => dl. i32_align ,
348+ I64 => dl. i64_align ,
349+ }
350+ }
351+
342352 pub fn to_ty < ' a , ' tcx > ( & self , tcx : & ty:: TyCtxt < ' a , ' tcx , ' tcx > ,
343353 signed : bool ) -> Ty < ' tcx > {
344354 match ( * self , signed) {
@@ -377,6 +387,18 @@ impl Integer {
377387 }
378388 }
379389
390+ //Find the smallest integer with the given alignment.
391+ pub fn for_abi_align ( dl : & TargetDataLayout , align : Align ) -> Option < Integer > {
392+ let wanted = align. abi ( ) ;
393+ for & candidate in & [ I8 , I16 , I32 , I64 ] {
394+ let ty = Int ( candidate) ;
395+ if wanted == ty. align ( dl) . abi ( ) && wanted == ty. size ( dl) . bytes ( ) {
396+ return Some ( candidate) ;
397+ }
398+ }
399+ None
400+ }
401+
380402 /// Get the Integer type from an attr::IntType.
381403 pub fn from_attr ( dl : & TargetDataLayout , ity : attr:: IntType ) -> Integer {
382404 match ity {
@@ -1149,20 +1171,7 @@ impl<'a, 'gcx, 'tcx> Layout {
11491171 // won't be so conservative.
11501172
11511173 // Use the initial field alignment
1152- let wanted = start_align. abi ( ) ;
1153- let mut ity = min_ity;
1154- for & candidate in & [ I16 , I32 , I64 ] {
1155- let ty = Int ( candidate) ;
1156- if wanted == ty. align ( dl) . abi ( ) && wanted == ty. size ( dl) . bytes ( ) {
1157- ity = candidate;
1158- break ;
1159- }
1160- }
1161-
1162- // FIXME(eddyb) conservative only to avoid diverging from trans::adt.
1163- if align. abi ( ) != start_align. abi ( ) {
1164- ity = min_ity;
1165- }
1174+ let mut ity = Integer :: for_abi_align ( dl, start_align) . unwrap_or ( min_ity) ;
11661175
11671176 // If the alignment is not larger than the chosen discriminant size,
11681177 // don't use the alignment as the final size.
0 commit comments