@@ -20,6 +20,7 @@ pub use self::VarValue::*;
2020use super :: { RegionVariableOrigin , SubregionOrigin , TypeTrace , MiscVariable } ;
2121
2222use rustc_data_structures:: graph:: { self , Direction , NodeIndex } ;
23+ use rustc_data_structures:: unify:: { self , UnificationTable } ;
2324use middle:: free_region:: FreeRegionMap ;
2425use middle:: ty:: { self , Ty } ;
2526use middle:: ty:: { BoundRegion , FreeRegion , Region , RegionVid } ;
@@ -234,15 +235,16 @@ pub struct RegionVarBindings<'a, 'tcx: 'a> {
234235 // bound on a variable and so forth, which can never be rolled
235236 // back.
236237 undo_log : RefCell < Vec < UndoLogEntry > > ,
238+ unification_table : RefCell < UnificationTable < ty:: RegionVid > > ,
237239
238240 // This contains the results of inference. It begins as an empty
239241 // option and only acquires a value after inference is complete.
240242 values : RefCell < Option < Vec < VarValue > > > ,
241243}
242244
243- #[ derive( Debug ) ]
244245pub struct RegionSnapshot {
245246 length : usize ,
247+ region_snapshot : unify:: Snapshot < ty:: RegionVid > ,
246248 skolemization_count : u32 ,
247249}
248250
@@ -260,6 +262,7 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> {
260262 skolemization_count : Cell :: new ( 0 ) ,
261263 bound_count : Cell :: new ( 0 ) ,
262264 undo_log : RefCell :: new ( Vec :: new ( ) ) ,
265+ unification_table : RefCell :: new ( UnificationTable :: new ( ) ) ,
263266 }
264267 }
265268
@@ -273,6 +276,7 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> {
273276 self . undo_log . borrow_mut ( ) . push ( OpenSnapshot ) ;
274277 RegionSnapshot {
275278 length : length,
279+ region_snapshot : self . unification_table . borrow_mut ( ) . snapshot ( ) ,
276280 skolemization_count : self . skolemization_count . get ( ) ,
277281 }
278282 }
@@ -289,6 +293,7 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> {
289293 ( * undo_log) [ snapshot. length ] = CommitedSnapshot ;
290294 }
291295 self . skolemization_count . set ( snapshot. skolemization_count ) ;
296+ self . unification_table . borrow_mut ( ) . commit ( snapshot. region_snapshot ) ;
292297 }
293298
294299 pub fn rollback_to ( & self , snapshot : RegionSnapshot ) {
@@ -328,6 +333,8 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> {
328333 let c = undo_log. pop ( ) . unwrap ( ) ;
329334 assert ! ( c == OpenSnapshot ) ;
330335 self . skolemization_count . set ( snapshot. skolemization_count ) ;
336+ self . unification_table . borrow_mut ( )
337+ . rollback_to ( snapshot. region_snapshot ) ;
331338 }
332339
333340 pub fn num_vars ( & self ) -> u32 {
@@ -340,7 +347,8 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> {
340347 pub fn new_region_var ( & self , origin : RegionVariableOrigin ) -> RegionVid {
341348 let id = self . num_vars ( ) ;
342349 self . var_origins . borrow_mut ( ) . push ( origin. clone ( ) ) ;
343- let vid = RegionVid { index : id } ;
350+ let vid = self . unification_table . borrow_mut ( ) . new_key ( ( ) ) ;
351+ assert_eq ! ( vid. index, id) ;
344352 if self . in_snapshot ( ) {
345353 self . undo_log . borrow_mut ( ) . push ( AddVar ( vid) ) ;
346354 }
@@ -460,6 +468,10 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> {
460468 // equating regions.
461469 self . make_subregion ( origin. clone ( ) , sub, sup) ;
462470 self . make_subregion ( origin, sup, sub) ;
471+
472+ if let ( ty:: ReVar ( sub) , ty:: ReVar ( sup) ) = ( sub, sup) {
473+ self . unification_table . borrow_mut ( ) . union ( sub, sup) ;
474+ }
463475 }
464476 }
465477
@@ -568,6 +580,10 @@ impl<'a, 'tcx> RegionVarBindings<'a, 'tcx> {
568580 }
569581 }
570582
583+ pub fn opportunistic_resolve_var ( & self , rid : RegionVid ) -> ty:: Region {
584+ ty:: ReVar ( self . unification_table . borrow_mut ( ) . find ( rid) )
585+ }
586+
571587 fn combine_map ( & self , t : CombineMapType ) -> & RefCell < CombineMap > {
572588 match t {
573589 Glb => & self . glbs ,
@@ -1312,6 +1328,13 @@ impl<'tcx> fmt::Debug for RegionAndOrigin<'tcx> {
13121328 }
13131329}
13141330
1331+ impl fmt:: Debug for RegionSnapshot {
1332+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1333+ write ! ( f, "RegionSnapshot(length={},skolemization={})" ,
1334+ self . length, self . skolemization_count)
1335+ }
1336+ }
1337+
13151338impl < ' tcx > fmt:: Debug for GenericKind < ' tcx > {
13161339 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
13171340 match * self {
0 commit comments