4
4
*/
5
5
6
6
use 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 } ;
9
9
use crate :: ty:: layout:: VariantIdx ;
10
10
use crate :: hir;
11
11
use crate :: ty:: util:: IntTypeExt ;
@@ -16,8 +16,7 @@ pub enum PlaceTy<'tcx> {
16
16
Ty { ty : Ty < ' tcx > } ,
17
17
18
18
/// Downcast to a particular variant of an enum.
19
- Downcast { adt_def : & ' tcx AdtDef ,
20
- substs : SubstsRef < ' tcx > ,
19
+ Downcast { ty : Ty < ' tcx > ,
21
20
variant_index : VariantIdx } ,
22
21
}
23
22
@@ -30,12 +29,10 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
30
29
PlaceTy :: Ty { ty }
31
30
}
32
31
33
- pub fn to_ty ( & self , tcx : TyCtxt < ' a , ' gcx , ' tcx > ) -> Ty < ' tcx > {
32
+ pub fn to_ty ( & self ) -> Ty < ' tcx > {
34
33
match * self {
35
- PlaceTy :: Ty { ty } =>
36
- ty,
37
- PlaceTy :: Downcast { adt_def, substs, variant_index : _ } =>
38
- tcx. mk_adt ( adt_def, substs) ,
34
+ PlaceTy :: Ty { ty } => ty,
35
+ PlaceTy :: Downcast { ty, variant_index : _ } => ty,
39
36
}
40
37
}
41
38
@@ -48,21 +45,18 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
48
45
/// Note that the resulting type has not been normalized.
49
46
pub fn field_ty ( self , tcx : TyCtxt < ' a , ' gcx , ' tcx > , f : & Field ) -> Ty < ' tcx >
50
47
{
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 } , _) => {
48
+ let answer = match self . to_ty ( ) . sty {
49
+ ty:: TyKind :: Adt ( adt_def, substs) => {
50
+ let variant_index = match & self {
51
+ PlaceTy :: Ty { .. } => VariantIdx :: new ( 0 ) ,
52
+ PlaceTy :: Downcast { ty : _, variant_index } => * variant_index,
53
+ } ;
56
54
let variant_def = & adt_def. variants [ variant_index] ;
57
55
let field_def = & variant_def. fields [ f. index ( ) ] ;
58
56
field_def. ty ( tcx, substs)
59
57
}
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
- }
58
+ ty:: Tuple ( ref tys) => tys[ f. index ( ) ] ,
59
+ _ => bug ! ( "extracting field of non-tuple non-adt: {:?}" , self ) ,
66
60
} ;
67
61
debug ! ( "field_ty self: {:?} f: {:?} yields: {:?}" , self , f, answer) ;
68
62
answer
@@ -94,7 +88,7 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
94
88
{
95
89
let answer = match * elem {
96
90
ProjectionElem :: Deref => {
97
- let ty = self . to_ty ( tcx )
91
+ let ty = self . to_ty ( )
98
92
. builtin_deref ( true )
99
93
. unwrap_or_else ( || {
100
94
bug ! ( "deref projection of non-dereferencable ty {:?}" , self )
@@ -106,10 +100,10 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
106
100
}
107
101
ProjectionElem :: Index ( _) | ProjectionElem :: ConstantIndex { .. } =>
108
102
PlaceTy :: Ty {
109
- ty : self . to_ty ( tcx ) . builtin_index ( ) . unwrap ( )
103
+ ty : self . to_ty ( ) . builtin_index ( ) . unwrap ( )
110
104
} ,
111
105
ProjectionElem :: Subslice { from, to } => {
112
- let ty = self . to_ty ( tcx ) ;
106
+ let ty = self . to_ty ( ) ;
113
107
PlaceTy :: Ty {
114
108
ty : match ty. sty {
115
109
ty:: Array ( inner, size) => {
@@ -124,19 +118,19 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
124
118
}
125
119
}
126
120
}
127
- ProjectionElem :: Downcast ( _name, index) =>
128
- match self . to_ty ( tcx) . sty {
129
- ty:: Adt ( adt_def, substs) => {
121
+ ProjectionElem :: Downcast ( _name, index) => {
122
+ let ty = self . to_ty ( ) ;
123
+ match & ty. sty {
124
+ ty:: Adt ( adt_def, _substs) => {
130
125
assert ! ( adt_def. is_enum( ) ) ;
131
126
assert ! ( index. as_usize( ) < adt_def. variants. len( ) ) ;
132
- PlaceTy :: Downcast { adt_def,
133
- substs,
134
- variant_index : index }
135
127
}
136
128
_ => {
137
129
bug ! ( "cannot downcast non-ADT type: `{:?}`" , self )
138
130
}
139
- } ,
131
+ } ;
132
+ PlaceTy :: Downcast { ty, variant_index : index }
133
+ }
140
134
ProjectionElem :: Field ( ref f, ref fty) =>
141
135
PlaceTy :: Ty { ty : handle_field ( & self , f, fty) } ,
142
136
} ;
@@ -148,7 +142,7 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
148
142
EnumTypeFoldableImpl ! {
149
143
impl <' tcx> TypeFoldable <' tcx> for PlaceTy <' tcx> {
150
144
( PlaceTy :: Ty ) { ty } ,
151
- ( PlaceTy :: Downcast ) { adt_def , substs , variant_index } ,
145
+ ( PlaceTy :: Downcast ) { ty , variant_index } ,
152
146
}
153
147
}
154
148
@@ -185,7 +179,7 @@ impl<'tcx> Place<'tcx> {
185
179
match place {
186
180
Place :: Projection ( ref proj) => match proj. elem {
187
181
ProjectionElem :: Field ( field, _ty) => {
188
- let base_ty = proj. base . ty ( mir, * tcx) . to_ty ( * tcx ) ;
182
+ let base_ty = proj. base . ty ( mir, * tcx) . to_ty ( ) ;
189
183
190
184
if ( base_ty. is_closure ( ) || base_ty. is_generator ( ) ) &&
191
185
( !by_ref || mir. upvar_decls [ field. index ( ) ] . by_ref )
@@ -217,7 +211,7 @@ impl<'tcx> Rvalue<'tcx> {
217
211
tcx. mk_array ( operand. ty ( local_decls, tcx) , count)
218
212
}
219
213
Rvalue :: Ref ( reg, bk, ref place) => {
220
- let place_ty = place. ty ( local_decls, tcx) . to_ty ( tcx ) ;
214
+ let place_ty = place. ty ( local_decls, tcx) . to_ty ( ) ;
221
215
tcx. mk_ref ( reg,
222
216
ty:: TypeAndMut {
223
217
ty : place_ty,
@@ -243,7 +237,7 @@ impl<'tcx> Rvalue<'tcx> {
243
237
operand. ty ( local_decls, tcx)
244
238
}
245
239
Rvalue :: Discriminant ( ref place) => {
246
- let ty = place. ty ( local_decls, tcx) . to_ty ( tcx ) ;
240
+ let ty = place. ty ( local_decls, tcx) . to_ty ( ) ;
247
241
if let ty:: Adt ( adt_def, _) = ty. sty {
248
242
adt_def. repr . discr_type ( ) . to_ty ( tcx)
249
243
} else {
@@ -292,7 +286,7 @@ impl<'tcx> Operand<'tcx> {
292
286
{
293
287
match self {
294
288
& Operand :: Copy ( ref l) |
295
- & Operand :: Move ( ref l) => l. ty ( local_decls, tcx) . to_ty ( tcx ) ,
289
+ & Operand :: Move ( ref l) => l. ty ( local_decls, tcx) . to_ty ( ) ,
296
290
& Operand :: Constant ( ref c) => c. ty ,
297
291
}
298
292
}
0 commit comments