@@ -75,12 +75,28 @@ struct TypeVariableData {
7575 diverging : bool
7676}
7777
78- #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
79- enum TypeVariableValue < ' tcx > {
78+ #[ derive( Copy , Clone , Debug ) ]
79+ pub enum TypeVariableValue < ' tcx > {
8080 Known { value : Ty < ' tcx > } ,
8181 Unknown ,
8282}
8383
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+
84100pub struct Snapshot < ' tcx > {
85101 /// number of variables at the time of the snapshot
86102 num_vars : usize ,
@@ -121,8 +137,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
121137 ///
122138 /// Precondition: neither `a` nor `b` are known.
123139 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 ( ) ) ;
126142 self . eq_relations . union ( a, b) ;
127143 self . sub_relations . union ( a, b) ;
128144 }
@@ -131,8 +147,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
131147 ///
132148 /// Precondition: neither `a` nor `b` are known.
133149 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 ( ) ) ;
136152 self . sub_relations . union ( a, b) ;
137153 }
138154
@@ -141,8 +157,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
141157 /// Precondition: `vid` must not have been previously instantiated.
142158 pub fn instantiate ( & mut self , vid : ty:: TyVid , ty : Ty < ' tcx > ) {
143159 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 ( ) ,
146162 "instantiating type variable `{:?}` twice: new-value = {:?}, old-value={:?}" ,
147163 vid, ty, self . eq_relations. probe_value( vid) ) ;
148164 self . eq_relations . union_value ( vid, TypeVariableValue :: Known { value : ty } ) ;
@@ -208,12 +224,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
208224
209225 /// Retrieves the type to which `vid` has been instantiated, if
210226 /// 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)
217229 }
218230
219231 /// If `t` is a type-inference variable, and it has been
@@ -223,8 +235,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
223235 match t. sty {
224236 ty:: TyInfer ( ty:: TyVar ( v) ) => {
225237 match self . probe ( v) {
226- None => t,
227- Some ( u ) => u
238+ TypeVariableValue :: Unknown { .. } => t,
239+ TypeVariableValue :: Known { value } => value ,
228240 }
229241 }
230242 _ => t,
@@ -310,12 +322,9 @@ impl<'tcx> TypeVariableTable<'tcx> {
310322 // use the less efficient algorithm for now.
311323 let mut escaping_types = Vec :: with_capacity ( snapshot. num_vars ) ;
312324 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 ..
314326 . 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.
319328 debug ! ( "types_escaping_snapshot = {:?}" , escaping_types) ;
320329 escaping_types
321330 }
@@ -326,10 +335,9 @@ impl<'tcx> TypeVariableTable<'tcx> {
326335 ( 0 ..self . var_data . len ( ) )
327336 . filter_map ( |i| {
328337 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 ,
333341 }
334342 } )
335343 . collect ( )
0 commit comments