@@ -78,12 +78,28 @@ struct TypeVariableData {
7878 diverging : bool
7979}
8080
81- #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
82- enum TypeVariableValue < ' tcx > {
81+ #[ derive( Copy , Clone , Debug ) ]
82+ pub enum TypeVariableValue < ' tcx > {
8383 Known { value : Ty < ' tcx > } ,
8484 Unknown ,
8585}
8686
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+
87103pub struct Snapshot < ' tcx > {
88104 /// number of variables at the time of the snapshot
89105 num_vars : usize ,
@@ -124,8 +140,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
124140 ///
125141 /// Precondition: neither `a` nor `b` are known.
126142 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 ( ) ) ;
129145 self . eq_relations . union ( a, b) ;
130146 self . sub_relations . union ( a, b) ;
131147 }
@@ -134,8 +150,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
134150 ///
135151 /// Precondition: neither `a` nor `b` are known.
136152 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 ( ) ) ;
139155 self . sub_relations . union ( a, b) ;
140156 }
141157
@@ -144,8 +160,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
144160 /// Precondition: `vid` must not have been previously instantiated.
145161 pub fn instantiate ( & mut self , vid : ty:: TyVid , ty : Ty < ' tcx > ) {
146162 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 ( ) ,
149165 "instantiating type variable `{:?}` twice: new-value = {:?}, old-value={:?}" ,
150166 vid, ty, self . eq_relations. probe_value( vid) ) ;
151167 self . eq_relations . union_value ( vid, TypeVariableValue :: Known { value : ty } ) ;
@@ -211,12 +227,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
211227
212228 /// Retrieves the type to which `vid` has been instantiated, if
213229 /// 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)
220232 }
221233
222234 /// If `t` is a type-inference variable, and it has been
@@ -226,8 +238,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
226238 match t. sty {
227239 ty:: TyInfer ( ty:: TyVar ( v) ) => {
228240 match self . probe ( v) {
229- None => t,
230- Some ( u ) => u
241+ TypeVariableValue :: Unknown { .. } => t,
242+ TypeVariableValue :: Known { value } => value ,
231243 }
232244 }
233245 _ => t,
@@ -313,12 +325,9 @@ impl<'tcx> TypeVariableTable<'tcx> {
313325 // use the less efficient algorithm for now.
314326 let mut escaping_types = Vec :: with_capacity ( snapshot. num_vars ) ;
315327 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 ..
317329 . 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.
322331 debug ! ( "types_escaping_snapshot = {:?}" , escaping_types) ;
323332 escaping_types
324333 }
@@ -329,10 +338,9 @@ impl<'tcx> TypeVariableTable<'tcx> {
329338 ( 0 ..self . var_data . len ( ) )
330339 . filter_map ( |i| {
331340 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 ,
336344 }
337345 } )
338346 . collect ( )
0 commit comments