@@ -4,6 +4,7 @@ pub use Primitive::*;
44use crate :: spec:: Target ;
55
66use std:: convert:: { TryFrom , TryInto } ;
7+ use std:: num:: NonZeroUsize ;
78use std:: ops:: { Add , AddAssign , Deref , Mul , Range , RangeInclusive , Sub } ;
89
910use rustc_index:: vec:: { Idx , IndexVec } ;
@@ -619,10 +620,11 @@ impl Scalar {
619620/// Describes how the fields of a type are located in memory.
620621#[ derive( PartialEq , Eq , Hash , Debug , HashStable_Generic ) ]
621622pub enum FieldsShape {
623+ /// Scalar primitives and `!`, which never have fields.
624+ Primitive ,
625+
622626 /// 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 ) ,
626628
627629 /// Array/vector-like placement, with all fields of identical types.
628630 Array { stride : Size , count : u64 } ,
@@ -660,7 +662,8 @@ pub enum FieldsShape {
660662impl FieldsShape {
661663 pub fn count ( & self ) -> usize {
662664 match * self {
663- FieldsShape :: Union ( count) => count,
665+ FieldsShape :: Primitive => 0 ,
666+ FieldsShape :: Union ( count) => count. get ( ) ,
664667 FieldsShape :: Array { count, .. } => {
665668 let usize_count = count as usize ;
666669 assert_eq ! ( usize_count as u64 , count) ;
@@ -672,8 +675,16 @@ impl FieldsShape {
672675
673676 pub fn offset ( & self , i : usize ) -> Size {
674677 match * self {
678+ FieldsShape :: Primitive => {
679+ unreachable ! ( "FieldsShape::offset: `Primitive`s have no fields" )
680+ }
675681 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+ ) ;
677688 Size :: ZERO
678689 }
679690 FieldsShape :: Array { stride, count } => {
@@ -687,6 +698,9 @@ impl FieldsShape {
687698
688699 pub fn memory_index ( & self , i : usize ) -> usize {
689700 match * self {
701+ FieldsShape :: Primitive => {
702+ unreachable ! ( "FieldsShape::memory_index: `Primitive`s have no fields" )
703+ }
690704 FieldsShape :: Union ( _) | FieldsShape :: Array { .. } => i,
691705 FieldsShape :: Arbitrary { ref memory_index, .. } => {
692706 let r = memory_index[ i] ;
@@ -718,7 +732,7 @@ impl FieldsShape {
718732 }
719733
720734 ( 0 ..self . count ( ) ) . map ( move |i| match * self {
721- FieldsShape :: Union ( _) | FieldsShape :: Array { .. } => i,
735+ FieldsShape :: Primitive | FieldsShape :: Union ( _) | FieldsShape :: Array { .. } => i,
722736 FieldsShape :: Arbitrary { .. } => {
723737 if use_small {
724738 inverse_small[ i] as usize
@@ -887,7 +901,6 @@ impl Niche {
887901#[ derive( PartialEq , Eq , Hash , Debug , HashStable_Generic ) ]
888902pub struct Layout {
889903 /// Says where the fields are located within the layout.
890- /// Primitives and uninhabited enums appear as unions without fields.
891904 pub fields : FieldsShape ,
892905
893906 /// Encodes information about multi-variant layouts.
@@ -923,7 +936,7 @@ impl Layout {
923936 let align = scalar. value . align ( cx) ;
924937 Layout {
925938 variants : Variants :: Single { index : VariantIdx :: new ( 0 ) } ,
926- fields : FieldsShape :: Union ( 0 ) ,
939+ fields : FieldsShape :: Primitive ,
927940 abi : Abi :: Scalar ( scalar) ,
928941 largest_niche,
929942 size,
0 commit comments