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 ;
12
12
13
13
#[ 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 > ,
22
18
}
23
19
24
20
static_assert ! ( PLACE_TY_IS_3_PTRS_LARGE :
@@ -27,16 +23,7 @@ static_assert!(PLACE_TY_IS_3_PTRS_LARGE:
27
23
28
24
impl < ' a , ' gcx , ' tcx > PlaceTy < ' tcx > {
29
25
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 }
40
27
}
41
28
42
29
/// `place_ty.field_ty(tcx, f)` computes the type at a given field
@@ -48,21 +35,20 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
48
35
/// Note that the resulting type has not been normalized.
49
36
pub fn field_ty ( self , tcx : TyCtxt < ' a , ' gcx , ' tcx > , f : & Field ) -> Ty < ' tcx >
50
37
{
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
+ } ;
57
47
let field_def = & variant_def. fields [ f. index ( ) ] ;
58
48
field_def. ty ( tcx, substs)
59
49
}
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 ) ,
66
52
} ;
67
53
debug ! ( "field_ty self: {:?} f: {:?} yields: {:?}" , self , f, answer) ;
68
54
answer
@@ -94,61 +80,43 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
94
80
{
95
81
let answer = match * elem {
96
82
ProjectionElem :: Deref => {
97
- let ty = self . to_ty ( tcx )
83
+ let ty = self . ty
98
84
. builtin_deref ( true )
99
85
. unwrap_or_else ( || {
100
86
bug ! ( "deref projection of non-dereferencable ty {:?}" , self )
101
87
} )
102
88
. ty ;
103
- PlaceTy :: Ty {
104
- ty,
105
- }
89
+ PlaceTy :: from_ty ( ty)
106
90
}
107
91
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 ( ) ) ,
111
93
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)
135
99
}
100
+ ty:: Slice ( ..) => self . ty ,
136
101
_ => {
137
- bug ! ( "cannot downcast non-ADT type: `{:?}`" , self )
102
+ bug ! ( "cannot subslice non-array type: `{:?}`" , self )
138
103
}
139
- } ,
104
+ } )
105
+ }
106
+ ProjectionElem :: Downcast ( _name, index) =>
107
+ PlaceTy { ty : self . ty , variant_index : Some ( index) } ,
140
108
ProjectionElem :: Field ( ref f, ref fty) =>
141
- PlaceTy :: Ty { ty : handle_field ( & self , f, fty) } ,
109
+ PlaceTy :: from_ty ( handle_field ( & self , f, fty) ) ,
142
110
} ;
143
111
debug ! ( "projection_ty self: {:?} elem: {:?} yields: {:?}" , self , elem, answer) ;
144
112
answer
145
113
}
146
114
}
147
115
148
- EnumTypeFoldableImpl ! {
116
+ BraceStructTypeFoldableImpl ! {
149
117
impl <' tcx> TypeFoldable <' tcx> for PlaceTy <' tcx> {
150
- ( PlaceTy :: Ty ) { ty } ,
151
- ( PlaceTy :: Downcast ) { adt_def , substs , variant_index } ,
118
+ ty ,
119
+ variant_index,
152
120
}
153
121
}
154
122
@@ -158,9 +126,9 @@ impl<'tcx> Place<'tcx> {
158
126
{
159
127
match * self {
160
128
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 ) ,
162
130
Place :: Base ( PlaceBase :: Static ( ref data) ) =>
163
- PlaceTy :: Ty { ty : data. ty } ,
131
+ PlaceTy :: from_ty ( data. ty ) ,
164
132
Place :: Projection ( ref proj) =>
165
133
proj. base . ty ( local_decls, tcx) . projection_ty ( tcx, & proj. elem ) ,
166
134
}
@@ -185,7 +153,7 @@ impl<'tcx> Place<'tcx> {
185
153
match place {
186
154
Place :: Projection ( ref proj) => match proj. elem {
187
155
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 ;
189
157
190
158
if ( base_ty. is_closure ( ) || base_ty. is_generator ( ) ) &&
191
159
( !by_ref || mir. upvar_decls [ field. index ( ) ] . by_ref )
@@ -217,7 +185,7 @@ impl<'tcx> Rvalue<'tcx> {
217
185
tcx. mk_array ( operand. ty ( local_decls, tcx) , count)
218
186
}
219
187
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 ;
221
189
tcx. mk_ref ( reg,
222
190
ty:: TypeAndMut {
223
191
ty : place_ty,
@@ -243,7 +211,7 @@ impl<'tcx> Rvalue<'tcx> {
243
211
operand. ty ( local_decls, tcx)
244
212
}
245
213
Rvalue :: Discriminant ( ref place) => {
246
- let ty = place. ty ( local_decls, tcx) . to_ty ( tcx ) ;
214
+ let ty = place. ty ( local_decls, tcx) . ty ;
247
215
if let ty:: Adt ( adt_def, _) = ty. sty {
248
216
adt_def. repr . discr_type ( ) . to_ty ( tcx)
249
217
} else {
@@ -292,7 +260,7 @@ impl<'tcx> Operand<'tcx> {
292
260
{
293
261
match self {
294
262
& 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 ,
296
264
& Operand :: Constant ( ref c) => c. ty ,
297
265
}
298
266
}
0 commit comments