@@ -4,6 +4,7 @@ pub use Primitive::*;
4
4
use crate :: spec:: Target ;
5
5
6
6
use std:: convert:: { TryFrom , TryInto } ;
7
+ use std:: num:: NonZeroUsize ;
7
8
use std:: ops:: { Add , AddAssign , Deref , Mul , Range , RangeInclusive , Sub } ;
8
9
9
10
use rustc_index:: vec:: { Idx , IndexVec } ;
@@ -619,10 +620,11 @@ impl Scalar {
619
620
/// Describes how the fields of a type are located in memory.
620
621
#[ derive( PartialEq , Eq , Hash , Debug , HashStable_Generic ) ]
621
622
pub enum FieldsShape {
623
+ /// Scalar primitives and `!`, which never have fields.
624
+ Primitive ,
625
+
622
626
/// All fields start at no offset. The `usize` is the field count.
623
- ///
624
- /// In the case of primitives the number of fields is `0`.
625
- Union ( usize ) ,
627
+ Union ( NonZeroUsize ) ,
626
628
627
629
/// Array/vector-like placement, with all fields of identical types.
628
630
Array { stride : Size , count : u64 } ,
@@ -660,7 +662,8 @@ pub enum FieldsShape {
660
662
impl FieldsShape {
661
663
pub fn count ( & self ) -> usize {
662
664
match * self {
663
- FieldsShape :: Union ( count) => count,
665
+ FieldsShape :: Primitive => 0 ,
666
+ FieldsShape :: Union ( count) => count. get ( ) ,
664
667
FieldsShape :: Array { count, .. } => {
665
668
let usize_count = count as usize ;
666
669
assert_eq ! ( usize_count as u64 , count) ;
@@ -672,8 +675,16 @@ impl FieldsShape {
672
675
673
676
pub fn offset ( & self , i : usize ) -> Size {
674
677
match * self {
678
+ FieldsShape :: Primitive => {
679
+ unreachable ! ( "FieldsShape::offset: `Primitive`s have no fields" )
680
+ }
675
681
FieldsShape :: Union ( count) => {
676
- assert ! ( i < count, "tried to access field {} of union with {} fields" , i, count) ;
682
+ assert ! (
683
+ i < count. get( ) ,
684
+ "tried to access field {} of union with {} fields" ,
685
+ i,
686
+ count
687
+ ) ;
677
688
Size :: ZERO
678
689
}
679
690
FieldsShape :: Array { stride, count } => {
@@ -687,6 +698,9 @@ impl FieldsShape {
687
698
688
699
pub fn memory_index ( & self , i : usize ) -> usize {
689
700
match * self {
701
+ FieldsShape :: Primitive => {
702
+ unreachable ! ( "FieldsShape::memory_index: `Primitive`s have no fields" )
703
+ }
690
704
FieldsShape :: Union ( _) | FieldsShape :: Array { .. } => i,
691
705
FieldsShape :: Arbitrary { ref memory_index, .. } => {
692
706
let r = memory_index[ i] ;
@@ -718,7 +732,7 @@ impl FieldsShape {
718
732
}
719
733
720
734
( 0 ..self . count ( ) ) . map ( move |i| match * self {
721
- FieldsShape :: Union ( _) | FieldsShape :: Array { .. } => i,
735
+ FieldsShape :: Primitive | FieldsShape :: Union ( _) | FieldsShape :: Array { .. } => i,
722
736
FieldsShape :: Arbitrary { .. } => {
723
737
if use_small {
724
738
inverse_small[ i] as usize
@@ -887,7 +901,6 @@ impl Niche {
887
901
#[ derive( PartialEq , Eq , Hash , Debug , HashStable_Generic ) ]
888
902
pub struct Layout {
889
903
/// Says where the fields are located within the layout.
890
- /// Primitives and uninhabited enums appear as unions without fields.
891
904
pub fields : FieldsShape ,
892
905
893
906
/// Encodes information about multi-variant layouts.
@@ -923,7 +936,7 @@ impl Layout {
923
936
let align = scalar. value . align ( cx) ;
924
937
Layout {
925
938
variants : Variants :: Single { index : VariantIdx :: new ( 0 ) } ,
926
- fields : FieldsShape :: Union ( 0 ) ,
939
+ fields : FieldsShape :: Primitive ,
927
940
abi : Abi :: Scalar ( scalar) ,
928
941
largest_niche,
929
942
size,
0 commit comments