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