@@ -12,18 +12,23 @@ use rustc_target::abi::VariantIdx;
1212#[ derive( Copy , Clone , Debug , TypeFoldable ) ]
1313pub struct PlaceTy < ' tcx > {
1414 pub ty : Ty < ' tcx > ,
15- /// Downcast to a particular variant of an enum, if included.
16- pub variant_index : Option < VariantIdx > ,
1715}
1816
1917// At least on 64 bit systems, `PlaceTy` should not be larger than two or three pointers.
2018#[ cfg( all( target_arch = "x86_64" , target_pointer_width = "64" ) ) ]
21- static_assert_size ! ( PlaceTy <' _>, 16 ) ;
19+ static_assert_size ! ( PlaceTy <' _>, 8 ) ;
2220
2321impl < ' tcx > PlaceTy < ' tcx > {
2422 #[ inline]
2523 pub fn from_ty ( ty : Ty < ' tcx > ) -> PlaceTy < ' tcx > {
26- PlaceTy { ty, variant_index : None }
24+ PlaceTy { ty }
25+ }
26+
27+ pub fn variant_index ( self ) -> Option < VariantIdx > {
28+ match self . ty . kind ( ) {
29+ ty:: Variant ( _, index) => Some ( * index) ,
30+ _ => None ,
31+ }
2732 }
2833
2934 /// `place_ty.field_ty(tcx, f)` computes the type at a given field
@@ -35,15 +40,16 @@ impl<'tcx> PlaceTy<'tcx> {
3540 /// Note that the resulting type has not been normalized.
3641 pub fn field_ty ( self , tcx : TyCtxt < ' tcx > , f : & Field ) -> Ty < ' tcx > {
3742 let answer = match self . ty . kind ( ) {
43+ ty:: Variant ( ty, variant_index) => match ty. kind ( ) {
44+ ty:: Adt ( adt_def, substs) => {
45+ assert ! ( adt_def. is_enum( ) ) ;
46+ let field_def = & adt_def. variants [ * variant_index] . fields [ f. index ( ) ] ;
47+ field_def. ty ( tcx, substs)
48+ }
49+ _ => bug ! ( "unexpected type: {}" , ty) ,
50+ } ,
3851 ty:: Adt ( adt_def, substs) => {
39- let variant_def = match self . variant_index {
40- None => adt_def. non_enum_variant ( ) ,
41- Some ( variant_index) => {
42- assert ! ( adt_def. is_enum( ) ) ;
43- & adt_def. variants [ variant_index]
44- }
45- } ;
46- let field_def = & variant_def. fields [ f. index ( ) ] ;
52+ let field_def = & adt_def. non_enum_variant ( ) . fields [ f. index ( ) ] ;
4753 field_def. ty ( tcx, substs)
4854 }
4955 ty:: Tuple ( ref tys) => tys[ f. index ( ) ] . expect_ty ( ) ,
@@ -103,7 +109,7 @@ impl<'tcx> PlaceTy<'tcx> {
103109 } )
104110 }
105111 ProjectionElem :: Downcast ( _name, index) => {
106- PlaceTy { ty : self . ty , variant_index : Some ( index) }
112+ PlaceTy { ty : tcx . mk_ty ( ty :: Variant ( self . ty , index) ) }
107113 }
108114 ProjectionElem :: Field ( ref f, ref fty) => PlaceTy :: from_ty ( handle_field ( & self , f, fty) ) ,
109115 } ;
0 commit comments