44 */
55
66use crate :: mir:: * ;
7- use crate :: ty:: subst:: { Subst , SubstsRef } ;
8- use crate :: ty:: { self , AdtDef , Ty , TyCtxt } ;
7+ use crate :: ty:: subst:: Subst ;
8+ use crate :: ty:: { self , Ty , TyCtxt } ;
99use crate :: ty:: layout:: VariantIdx ;
1010use crate :: hir;
1111use crate :: ty:: util:: IntTypeExt ;
1212
1313#[ derive( Copy , Clone , Debug ) ]
14- pub enum PlaceTy < ' tcx > {
15- /// Normal type.
16- Ty { ty : Ty < ' tcx > } ,
17-
18- /// Downcast to a particular variant of an enum.
19- Downcast { adt_def : & ' tcx AdtDef ,
20- substs : SubstsRef < ' tcx > ,
21- variant_index : VariantIdx } ,
14+ pub struct PlaceTy < ' tcx > {
15+ pub ty : Ty < ' tcx > ,
16+ /// Downcast to a particular variant of an enum, if included.
17+ pub variant_index : Option < VariantIdx > ,
2218}
2319
2420static_assert ! ( PLACE_TY_IS_3_PTRS_LARGE :
@@ -27,16 +23,7 @@ static_assert!(PLACE_TY_IS_3_PTRS_LARGE:
2723
2824impl < ' a , ' gcx , ' tcx > PlaceTy < ' tcx > {
2925 pub fn from_ty ( ty : Ty < ' tcx > ) -> PlaceTy < ' tcx > {
30- PlaceTy :: Ty { ty }
31- }
32-
33- pub fn to_ty ( & self , tcx : TyCtxt < ' a , ' gcx , ' tcx > ) -> Ty < ' tcx > {
34- match * self {
35- PlaceTy :: Ty { ty } =>
36- ty,
37- PlaceTy :: Downcast { adt_def, substs, variant_index : _ } =>
38- tcx. mk_adt ( adt_def, substs) ,
39- }
26+ PlaceTy { ty, variant_index : None }
4027 }
4128
4229 /// `place_ty.field_ty(tcx, f)` computes the type at a given field
@@ -48,21 +35,20 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
4835 /// Note that the resulting type has not been normalized.
4936 pub fn field_ty ( self , tcx : TyCtxt < ' a , ' gcx , ' tcx > , f : & Field ) -> Ty < ' tcx >
5037 {
51- // Pass `0` here so it can be used as a "default" variant_index in first arm below
52- let answer = match ( self , VariantIdx :: new ( 0 ) ) {
53- ( PlaceTy :: Ty {
54- ty : & ty:: TyS { sty : ty:: TyKind :: Adt ( adt_def, substs) , .. } } , variant_index) |
55- ( PlaceTy :: Downcast { adt_def, substs, variant_index } , _) => {
56- let variant_def = & adt_def. variants [ variant_index] ;
38+ let answer = match self . ty . sty {
39+ ty:: TyKind :: Adt ( adt_def, substs) => {
40+ let variant_def = match self . variant_index {
41+ None => adt_def. non_enum_variant ( ) ,
42+ Some ( variant_index) => {
43+ assert ! ( adt_def. is_enum( ) ) ;
44+ & adt_def. variants [ variant_index]
45+ }
46+ } ;
5747 let field_def = & variant_def. fields [ f. index ( ) ] ;
5848 field_def. ty ( tcx, substs)
5949 }
60- ( PlaceTy :: Ty { ty } , _) => {
61- match ty. sty {
62- ty:: Tuple ( ref tys) => tys[ f. index ( ) ] ,
63- _ => bug ! ( "extracting field of non-tuple non-adt: {:?}" , self ) ,
64- }
65- }
50+ ty:: Tuple ( ref tys) => tys[ f. index ( ) ] ,
51+ _ => bug ! ( "extracting field of non-tuple non-adt: {:?}" , self ) ,
6652 } ;
6753 debug ! ( "field_ty self: {:?} f: {:?} yields: {:?}" , self , f, answer) ;
6854 answer
@@ -94,61 +80,43 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
9480 {
9581 let answer = match * elem {
9682 ProjectionElem :: Deref => {
97- let ty = self . to_ty ( tcx )
83+ let ty = self . ty
9884 . builtin_deref ( true )
9985 . unwrap_or_else ( || {
10086 bug ! ( "deref projection of non-dereferencable ty {:?}" , self )
10187 } )
10288 . ty ;
103- PlaceTy :: Ty {
104- ty,
105- }
89+ PlaceTy :: from_ty ( ty)
10690 }
10791 ProjectionElem :: Index ( _) | ProjectionElem :: ConstantIndex { .. } =>
108- PlaceTy :: Ty {
109- ty : self . to_ty ( tcx) . builtin_index ( ) . unwrap ( )
110- } ,
92+ PlaceTy :: from_ty ( self . ty . builtin_index ( ) . unwrap ( ) ) ,
11193 ProjectionElem :: Subslice { from, to } => {
112- let ty = self . to_ty ( tcx) ;
113- PlaceTy :: Ty {
114- ty : match ty. sty {
115- ty:: Array ( inner, size) => {
116- let size = size. unwrap_usize ( tcx) ;
117- let len = size - ( from as u64 ) - ( to as u64 ) ;
118- tcx. mk_array ( inner, len)
119- }
120- ty:: Slice ( ..) => ty,
121- _ => {
122- bug ! ( "cannot subslice non-array type: `{:?}`" , self )
123- }
124- }
125- }
126- }
127- ProjectionElem :: Downcast ( _name, index) =>
128- match self . to_ty ( tcx) . sty {
129- ty:: Adt ( adt_def, substs) => {
130- assert ! ( adt_def. is_enum( ) ) ;
131- assert ! ( index. as_usize( ) < adt_def. variants. len( ) ) ;
132- PlaceTy :: Downcast { adt_def,
133- substs,
134- variant_index : index }
94+ PlaceTy :: from_ty ( match self . ty . sty {
95+ ty:: Array ( inner, size) => {
96+ let size = size. unwrap_usize ( tcx) ;
97+ let len = size - ( from as u64 ) - ( to as u64 ) ;
98+ tcx. mk_array ( inner, len)
13599 }
100+ ty:: Slice ( ..) => self . ty ,
136101 _ => {
137- bug ! ( "cannot downcast non-ADT type: `{:?}`" , self )
102+ bug ! ( "cannot subslice non-array type: `{:?}`" , self )
138103 }
139- } ,
104+ } )
105+ }
106+ ProjectionElem :: Downcast ( _name, index) =>
107+ PlaceTy { ty : self . ty , variant_index : Some ( index) } ,
140108 ProjectionElem :: Field ( ref f, ref fty) =>
141- PlaceTy :: Ty { ty : handle_field ( & self , f, fty) } ,
109+ PlaceTy :: from_ty ( handle_field ( & self , f, fty) ) ,
142110 } ;
143111 debug ! ( "projection_ty self: {:?} elem: {:?} yields: {:?}" , self , elem, answer) ;
144112 answer
145113 }
146114}
147115
148- EnumTypeFoldableImpl ! {
116+ BraceStructTypeFoldableImpl ! {
149117 impl <' tcx> TypeFoldable <' tcx> for PlaceTy <' tcx> {
150- ( PlaceTy :: Ty ) { ty } ,
151- ( PlaceTy :: Downcast ) { adt_def , substs , variant_index } ,
118+ ty ,
119+ variant_index,
152120 }
153121}
154122
@@ -158,9 +126,9 @@ impl<'tcx> Place<'tcx> {
158126 {
159127 match * self {
160128 Place :: Base ( PlaceBase :: Local ( index) ) =>
161- PlaceTy :: Ty { ty : local_decls. local_decls ( ) [ index] . ty } ,
129+ PlaceTy :: from_ty ( local_decls. local_decls ( ) [ index] . ty ) ,
162130 Place :: Base ( PlaceBase :: Static ( ref data) ) =>
163- PlaceTy :: Ty { ty : data. ty } ,
131+ PlaceTy :: from_ty ( data. ty ) ,
164132 Place :: Projection ( ref proj) =>
165133 proj. base . ty ( local_decls, tcx) . projection_ty ( tcx, & proj. elem ) ,
166134 }
@@ -185,7 +153,7 @@ impl<'tcx> Place<'tcx> {
185153 match place {
186154 Place :: Projection ( ref proj) => match proj. elem {
187155 ProjectionElem :: Field ( field, _ty) => {
188- let base_ty = proj. base . ty ( mir, * tcx) . to_ty ( * tcx ) ;
156+ let base_ty = proj. base . ty ( mir, * tcx) . ty ;
189157
190158 if ( base_ty. is_closure ( ) || base_ty. is_generator ( ) ) &&
191159 ( !by_ref || mir. upvar_decls [ field. index ( ) ] . by_ref )
@@ -217,7 +185,7 @@ impl<'tcx> Rvalue<'tcx> {
217185 tcx. mk_array ( operand. ty ( local_decls, tcx) , count)
218186 }
219187 Rvalue :: Ref ( reg, bk, ref place) => {
220- let place_ty = place. ty ( local_decls, tcx) . to_ty ( tcx ) ;
188+ let place_ty = place. ty ( local_decls, tcx) . ty ;
221189 tcx. mk_ref ( reg,
222190 ty:: TypeAndMut {
223191 ty : place_ty,
@@ -243,7 +211,7 @@ impl<'tcx> Rvalue<'tcx> {
243211 operand. ty ( local_decls, tcx)
244212 }
245213 Rvalue :: Discriminant ( ref place) => {
246- let ty = place. ty ( local_decls, tcx) . to_ty ( tcx ) ;
214+ let ty = place. ty ( local_decls, tcx) . ty ;
247215 if let ty:: Adt ( adt_def, _) = ty. sty {
248216 adt_def. repr . discr_type ( ) . to_ty ( tcx)
249217 } else {
@@ -292,7 +260,7 @@ impl<'tcx> Operand<'tcx> {
292260 {
293261 match self {
294262 & Operand :: Copy ( ref l) |
295- & Operand :: Move ( ref l) => l. ty ( local_decls, tcx) . to_ty ( tcx ) ,
263+ & Operand :: Move ( ref l) => l. ty ( local_decls, tcx) . ty ,
296264 & Operand :: Constant ( ref c) => c. ty ,
297265 }
298266 }
0 commit comments