@@ -78,12 +78,28 @@ struct TypeVariableData {
78
78
diverging : bool
79
79
}
80
80
81
- #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
82
- enum TypeVariableValue < ' tcx > {
81
+ #[ derive( Copy , Clone , Debug ) ]
82
+ pub enum TypeVariableValue < ' tcx > {
83
83
Known { value : Ty < ' tcx > } ,
84
84
Unknown ,
85
85
}
86
86
87
+ impl < ' tcx > TypeVariableValue < ' tcx > {
88
+ pub fn known ( & self ) -> Option < Ty < ' tcx > > {
89
+ match * self {
90
+ TypeVariableValue :: Unknown { .. } => None ,
91
+ TypeVariableValue :: Known { value } => Some ( value) ,
92
+ }
93
+ }
94
+
95
+ pub fn is_unknown ( & self ) -> bool {
96
+ match * self {
97
+ TypeVariableValue :: Unknown { .. } => true ,
98
+ TypeVariableValue :: Known { .. } => false ,
99
+ }
100
+ }
101
+ }
102
+
87
103
pub struct Snapshot < ' tcx > {
88
104
/// number of variables at the time of the snapshot
89
105
num_vars : usize ,
@@ -124,8 +140,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
124
140
///
125
141
/// Precondition: neither `a` nor `b` are known.
126
142
pub fn equate ( & mut self , a : ty:: TyVid , b : ty:: TyVid ) {
127
- debug_assert ! ( self . probe( a) . is_none ( ) ) ;
128
- debug_assert ! ( self . probe( b) . is_none ( ) ) ;
143
+ debug_assert ! ( self . probe( a) . is_unknown ( ) ) ;
144
+ debug_assert ! ( self . probe( b) . is_unknown ( ) ) ;
129
145
self . eq_relations . union ( a, b) ;
130
146
self . sub_relations . union ( a, b) ;
131
147
}
@@ -134,8 +150,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
134
150
///
135
151
/// Precondition: neither `a` nor `b` are known.
136
152
pub fn sub ( & mut self , a : ty:: TyVid , b : ty:: TyVid ) {
137
- debug_assert ! ( self . probe( a) . is_none ( ) ) ;
138
- debug_assert ! ( self . probe( b) . is_none ( ) ) ;
153
+ debug_assert ! ( self . probe( a) . is_unknown ( ) ) ;
154
+ debug_assert ! ( self . probe( b) . is_unknown ( ) ) ;
139
155
self . sub_relations . union ( a, b) ;
140
156
}
141
157
@@ -144,8 +160,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
144
160
/// Precondition: `vid` must not have been previously instantiated.
145
161
pub fn instantiate ( & mut self , vid : ty:: TyVid , ty : Ty < ' tcx > ) {
146
162
let vid = self . root_var ( vid) ;
147
- debug_assert ! ( self . probe( vid) . is_none ( ) ) ;
148
- debug_assert ! ( self . eq_relations. probe_value( vid) == TypeVariableValue :: Unknown ,
163
+ debug_assert ! ( self . probe( vid) . is_unknown ( ) ) ;
164
+ debug_assert ! ( self . eq_relations. probe_value( vid) . is_unknown ( ) ,
149
165
"instantiating type variable `{:?}` twice: new-value = {:?}, old-value={:?}" ,
150
166
vid, ty, self . eq_relations. probe_value( vid) ) ;
151
167
self . eq_relations . union_value ( vid, TypeVariableValue :: Known { value : ty } ) ;
@@ -211,12 +227,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
211
227
212
228
/// Retrieves the type to which `vid` has been instantiated, if
213
229
/// any.
214
- pub fn probe ( & mut self , vid : ty:: TyVid ) -> Option < Ty < ' tcx > > {
215
- let vid = self . root_var ( vid) ;
216
- match self . eq_relations . probe_value ( vid) {
217
- TypeVariableValue :: Unknown => None ,
218
- TypeVariableValue :: Known { value } => Some ( value)
219
- }
230
+ pub fn probe ( & mut self , vid : ty:: TyVid ) -> TypeVariableValue < ' tcx > {
231
+ self . eq_relations . probe_value ( vid)
220
232
}
221
233
222
234
/// If `t` is a type-inference variable, and it has been
@@ -226,8 +238,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
226
238
match t. sty {
227
239
ty:: TyInfer ( ty:: TyVar ( v) ) => {
228
240
match self . probe ( v) {
229
- None => t,
230
- Some ( u ) => u
241
+ TypeVariableValue :: Unknown { .. } => t,
242
+ TypeVariableValue :: Known { value } => value ,
231
243
}
232
244
}
233
245
_ => t,
@@ -313,12 +325,9 @@ impl<'tcx> TypeVariableTable<'tcx> {
313
325
// use the less efficient algorithm for now.
314
326
let mut escaping_types = Vec :: with_capacity ( snapshot. num_vars ) ;
315
327
escaping_types. extend (
316
- ( 0 ..snapshot. num_vars ) // for all variables that pre-exist the snapshot. ..
328
+ ( 0 ..snapshot. num_vars ) // for all variables that pre-exist the snapshot, collect ..
317
329
. map ( |i| ty:: TyVid { index : i as u32 } )
318
- . filter_map ( |vid| match self . eq_relations . probe_value ( vid) {
319
- TypeVariableValue :: Unknown => None ,
320
- TypeVariableValue :: Known { value } => Some ( value) ,
321
- } ) ) ; // ...collect what types they've been instantiated with.
330
+ . filter_map ( |vid| self . probe ( vid) . known ( ) ) ) ; // ..types they are instantiated with.
322
331
debug ! ( "types_escaping_snapshot = {:?}" , escaping_types) ;
323
332
escaping_types
324
333
}
@@ -329,10 +338,9 @@ impl<'tcx> TypeVariableTable<'tcx> {
329
338
( 0 ..self . var_data . len ( ) )
330
339
. filter_map ( |i| {
331
340
let vid = ty:: TyVid { index : i as u32 } ;
332
- if self . probe ( vid) . is_some ( ) {
333
- None
334
- } else {
335
- Some ( vid)
341
+ match self . probe ( vid) {
342
+ TypeVariableValue :: Unknown { .. } => Some ( vid) ,
343
+ TypeVariableValue :: Known { .. } => None ,
336
344
}
337
345
} )
338
346
. collect ( )
0 commit comments