@@ -12,18 +12,23 @@ use rustc_target::abi::VariantIdx;
12
12
#[ derive( Copy , Clone , Debug , TypeFoldable ) ]
13
13
pub struct PlaceTy < ' tcx > {
14
14
pub ty : Ty < ' tcx > ,
15
- /// Downcast to a particular variant of an enum, if included.
16
- pub variant_index : Option < VariantIdx > ,
17
15
}
18
16
19
17
// At least on 64 bit systems, `PlaceTy` should not be larger than two or three pointers.
20
18
#[ cfg( all( target_arch = "x86_64" , target_pointer_width = "64" ) ) ]
21
- static_assert_size ! ( PlaceTy <' _>, 16 ) ;
19
+ static_assert_size ! ( PlaceTy <' _>, 8 ) ;
22
20
23
21
impl < ' tcx > PlaceTy < ' tcx > {
24
22
#[ inline]
25
23
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
+ }
27
32
}
28
33
29
34
/// `place_ty.field_ty(tcx, f)` computes the type at a given field
@@ -35,15 +40,16 @@ impl<'tcx> PlaceTy<'tcx> {
35
40
/// Note that the resulting type has not been normalized.
36
41
pub fn field_ty ( self , tcx : TyCtxt < ' tcx > , f : & Field ) -> Ty < ' tcx > {
37
42
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
+ } ,
38
51
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 ( ) ] ;
47
53
field_def. ty ( tcx, substs)
48
54
}
49
55
ty:: Tuple ( ref tys) => tys[ f. index ( ) ] . expect_ty ( ) ,
@@ -103,7 +109,7 @@ impl<'tcx> PlaceTy<'tcx> {
103
109
} )
104
110
}
105
111
ProjectionElem :: Downcast ( _name, index) => {
106
- PlaceTy { ty : self . ty , variant_index : Some ( index) }
112
+ PlaceTy { ty : tcx . mk_ty ( ty :: Variant ( self . ty , index) ) }
107
113
}
108
114
ProjectionElem :: Field ( ref f, ref fty) => PlaceTy :: from_ty ( handle_field ( & self , f, fty) ) ,
109
115
} ;
0 commit comments