31
31
//! variable only once, and it does so as soon as it can, so it is reasonable to ask what the type
32
32
//! inferencer knows "so far".
33
33
34
- use std:: collections:: hash_map:: Entry ;
35
-
36
- use rustc_data_structures:: fx:: FxHashMap ;
37
34
use rustc_middle:: bug;
38
35
use rustc_middle:: ty:: {
39
36
self , Ty , TyCtxt , TypeFoldable , TypeFolder , TypeSuperFoldable , TypeVisitableExt ,
@@ -43,63 +40,11 @@ use super::InferCtxt;
43
40
44
41
pub struct TypeFreshener < ' a , ' tcx > {
45
42
infcx : & ' a InferCtxt < ' tcx > ,
46
- ty_freshen_count : u32 ,
47
- const_freshen_count : u32 ,
48
- ty_freshen_map : FxHashMap < ty:: InferTy , Ty < ' tcx > > ,
49
- const_freshen_map : FxHashMap < ty:: InferConst , ty:: Const < ' tcx > > ,
50
43
}
51
44
52
45
impl < ' a , ' tcx > TypeFreshener < ' a , ' tcx > {
53
46
pub fn new ( infcx : & ' a InferCtxt < ' tcx > ) -> TypeFreshener < ' a , ' tcx > {
54
- TypeFreshener {
55
- infcx,
56
- ty_freshen_count : 0 ,
57
- const_freshen_count : 0 ,
58
- ty_freshen_map : Default :: default ( ) ,
59
- const_freshen_map : Default :: default ( ) ,
60
- }
61
- }
62
-
63
- fn freshen_ty < F > ( & mut self , input : Result < Ty < ' tcx > , ty:: InferTy > , mk_fresh : F ) -> Ty < ' tcx >
64
- where
65
- F : FnOnce ( u32 ) -> Ty < ' tcx > ,
66
- {
67
- match input {
68
- Ok ( ty) => ty. fold_with ( self ) ,
69
- Err ( key) => match self . ty_freshen_map . entry ( key) {
70
- Entry :: Occupied ( entry) => * entry. get ( ) ,
71
- Entry :: Vacant ( entry) => {
72
- let index = self . ty_freshen_count ;
73
- self . ty_freshen_count += 1 ;
74
- let t = mk_fresh ( index) ;
75
- entry. insert ( t) ;
76
- t
77
- }
78
- } ,
79
- }
80
- }
81
-
82
- fn freshen_const < F > (
83
- & mut self ,
84
- input : Result < ty:: Const < ' tcx > , ty:: InferConst > ,
85
- freshener : F ,
86
- ) -> ty:: Const < ' tcx >
87
- where
88
- F : FnOnce ( u32 ) -> ty:: InferConst ,
89
- {
90
- match input {
91
- Ok ( ct) => ct. fold_with ( self ) ,
92
- Err ( key) => match self . const_freshen_map . entry ( key) {
93
- Entry :: Occupied ( entry) => * entry. get ( ) ,
94
- Entry :: Vacant ( entry) => {
95
- let index = self . const_freshen_count ;
96
- self . const_freshen_count += 1 ;
97
- let ct = ty:: Const :: new_infer ( self . infcx . tcx , freshener ( index) ) ;
98
- entry. insert ( ct) ;
99
- ct
100
- }
101
- } ,
102
- }
47
+ TypeFreshener { infcx }
103
48
}
104
49
}
105
50
@@ -145,25 +90,14 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
145
90
fn fold_const ( & mut self , ct : ty:: Const < ' tcx > ) -> ty:: Const < ' tcx > {
146
91
match ct. kind ( ) {
147
92
ty:: ConstKind :: Infer ( ty:: InferConst :: Var ( v) ) => {
148
- let mut inner = self . infcx . inner . borrow_mut ( ) ;
149
93
let input =
150
- inner. const_unification_table ( ) . probe_value ( v) . known ( ) . ok_or_else ( || {
151
- ty:: InferConst :: Var ( inner. const_unification_table ( ) . find ( v) . vid )
152
- } ) ;
153
- drop ( inner) ;
154
- self . freshen_const ( input, ty:: InferConst :: Fresh )
155
- }
156
- ty:: ConstKind :: Infer ( ty:: InferConst :: Fresh ( i) ) => {
157
- if i >= self . const_freshen_count {
158
- bug ! (
159
- "Encountered a freshend const with id {} \
160
- but our counter is only at {}",
161
- i,
162
- self . const_freshen_count,
163
- ) ;
94
+ self . infcx . inner . borrow_mut ( ) . const_unification_table ( ) . probe_value ( v) . known ( ) ;
95
+ match input {
96
+ Some ( ct) => ct. fold_with ( self ) ,
97
+ None => self . infcx . tcx . consts . fresh_const ,
164
98
}
165
- ct
166
99
}
100
+ ty:: ConstKind :: Infer ( ty:: InferConst :: Fresh ) => ct,
167
101
168
102
ty:: ConstKind :: Bound ( ..) | ty:: ConstKind :: Placeholder ( _) => {
169
103
bug ! ( "unexpected const {:?}" , ct)
@@ -184,54 +118,35 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
184
118
fn fold_infer_ty ( & mut self , v : ty:: InferTy ) -> Option < Ty < ' tcx > > {
185
119
match v {
186
120
ty:: TyVar ( v) => {
187
- let mut inner = self . infcx . inner . borrow_mut ( ) ;
188
- let input = inner
189
- . type_variables ( )
190
- . probe ( v)
191
- . known ( )
192
- . ok_or_else ( || ty:: TyVar ( inner. type_variables ( ) . root_var ( v) ) ) ;
193
- drop ( inner) ;
194
- Some ( self . freshen_ty ( input, |n| Ty :: new_fresh ( self . infcx . tcx , n) ) )
121
+ let value = self . infcx . inner . borrow_mut ( ) . type_variables ( ) . probe ( v) . known ( ) ;
122
+ Some ( match value {
123
+ Some ( ty) => ty. fold_with ( self ) ,
124
+ None => self . infcx . tcx . types . fresh_ty ,
125
+ } )
195
126
}
196
127
197
128
ty:: IntVar ( v) => {
198
- let mut inner = self . infcx . inner . borrow_mut ( ) ;
199
- let value = inner. int_unification_table ( ) . probe_value ( v) ;
200
- let input = match value {
201
- ty:: IntVarValue :: IntType ( ty) => Ok ( Ty :: new_int ( self . infcx . tcx , ty) ) ,
202
- ty:: IntVarValue :: UintType ( ty) => Ok ( Ty :: new_uint ( self . infcx . tcx , ty) ) ,
203
- ty:: IntVarValue :: Unknown => {
204
- Err ( ty:: IntVar ( inner. int_unification_table ( ) . find ( v) ) )
129
+ let value = self . infcx . inner . borrow_mut ( ) . int_unification_table ( ) . probe_value ( v) ;
130
+ Some ( match value {
131
+ ty:: IntVarValue :: IntType ( ty) => Ty :: new_int ( self . infcx . tcx , ty) . fold_with ( self ) ,
132
+ ty:: IntVarValue :: UintType ( ty) => {
133
+ Ty :: new_uint ( self . infcx . tcx , ty) . fold_with ( self )
205
134
}
206
- } ;
207
- drop ( inner) ;
208
- Some ( self . freshen_ty ( input, |n| Ty :: new_fresh_int ( self . infcx . tcx , n) ) )
135
+ ty:: IntVarValue :: Unknown => self . infcx . tcx . types . fresh_int_ty ,
136
+ } )
209
137
}
210
138
211
139
ty:: FloatVar ( v) => {
212
- let mut inner = self . infcx . inner . borrow_mut ( ) ;
213
- let value = inner. float_unification_table ( ) . probe_value ( v) ;
214
- let input = match value {
215
- ty:: FloatVarValue :: Known ( ty) => Ok ( Ty :: new_float ( self . infcx . tcx , ty) ) ,
216
- ty:: FloatVarValue :: Unknown => {
217
- Err ( ty:: FloatVar ( inner. float_unification_table ( ) . find ( v) ) )
140
+ let value = self . infcx . inner . borrow_mut ( ) . float_unification_table ( ) . probe_value ( v) ;
141
+ Some ( match value {
142
+ ty:: FloatVarValue :: Known ( ty) => {
143
+ Ty :: new_float ( self . infcx . tcx , ty) . fold_with ( self )
218
144
}
219
- } ;
220
- drop ( inner) ;
221
- Some ( self . freshen_ty ( input, |n| Ty :: new_fresh_float ( self . infcx . tcx , n) ) )
145
+ ty:: FloatVarValue :: Unknown => self . infcx . tcx . types . fresh_float_ty ,
146
+ } )
222
147
}
223
148
224
- ty:: FreshTy ( ct) | ty:: FreshIntTy ( ct) | ty:: FreshFloatTy ( ct) => {
225
- if ct >= self . ty_freshen_count {
226
- bug ! (
227
- "Encountered a freshend type with id {} \
228
- but our counter is only at {}",
229
- ct,
230
- self . ty_freshen_count
231
- ) ;
232
- }
233
- None
234
- }
149
+ ty:: FreshTy | ty:: FreshIntTy | ty:: FreshFloatTy => None ,
235
150
}
236
151
}
237
152
}
0 commit comments