11//! See `README.md`.
22
33use self :: CombineMapType :: * ;
4- use self :: UndoLog :: * ;
54
6- use super :: {
7- InferCtxtUndoLogs , MiscVariable , RegionVariableOrigin , Rollback , Snapshot , SubregionOrigin ,
8- } ;
5+ use super :: { InferCtxtUndoLogs , MiscVariable , RegionVariableOrigin , SubregionOrigin } ;
96
10- use rustc_data_structures:: fx:: FxHashMap ;
7+ use rustc_data_structures:: fx:: FxIndexMap ;
118use rustc_data_structures:: intern:: Interned ;
129use rustc_data_structures:: sync:: Lrc ;
1310use rustc_data_structures:: undo_log:: UndoLogs ;
@@ -20,7 +17,6 @@ use rustc_middle::ty::{ReBound, ReVar};
2017use rustc_middle:: ty:: { Region , RegionVid } ;
2118use rustc_span:: Span ;
2219
23- use std:: collections:: BTreeMap ;
2420use std:: ops:: Range ;
2521use std:: { cmp, fmt, mem} ;
2622
@@ -90,7 +86,7 @@ pub type VarInfos = IndexVec<RegionVid, RegionVariableInfo>;
9086pub struct RegionConstraintData < ' tcx > {
9187 /// Constraints of the form `A <= B`, where either `A` or `B` can
9288 /// be a region variable (or neither, as it happens).
93- pub constraints : BTreeMap < Constraint < ' tcx > , SubregionOrigin < ' tcx > > ,
89+ pub constraints : Vec < ( Constraint < ' tcx > , SubregionOrigin < ' tcx > ) > ,
9490
9591 /// Constraints of the form `R0 member of [R1, ..., Rn]`, meaning that
9692 /// `R0` must be equal to one of the regions `R1..Rn`. These occur
@@ -267,28 +263,13 @@ pub(crate) struct TwoRegions<'tcx> {
267263 b : Region < ' tcx > ,
268264}
269265
270- #[ derive( Copy , Clone , PartialEq ) ]
271- pub ( crate ) enum UndoLog < ' tcx > {
272- /// We added `RegionVid`.
273- AddVar ( RegionVid ) ,
274-
275- /// We added the given `constraint`.
276- AddConstraint ( Constraint < ' tcx > ) ,
277-
278- /// We added the given `verify`.
279- AddVerify ( usize ) ,
280-
281- /// We added a GLB/LUB "combination variable".
282- AddCombination ( CombineMapType , TwoRegions < ' tcx > ) ,
283- }
284-
285266#[ derive( Copy , Clone , PartialEq ) ]
286267pub ( crate ) enum CombineMapType {
287268 Lub ,
288269 Glb ,
289270}
290271
291- type CombineMap < ' tcx > = FxHashMap < TwoRegions < ' tcx > , RegionVid > ;
272+ type CombineMap < ' tcx > = FxIndexMap < TwoRegions < ' tcx > , RegionVid > ;
292273
293274#[ derive( Debug , Clone , Copy ) ]
294275pub struct RegionVariableInfo {
@@ -298,6 +279,11 @@ pub struct RegionVariableInfo {
298279
299280pub struct RegionSnapshot {
300281 any_unifications : bool ,
282+ vars_len : usize ,
283+ constraints_len : usize ,
284+ verifys_len : usize ,
285+ glbs_len : usize ,
286+ lubs_len : usize ,
301287}
302288
303289impl < ' tcx > RegionConstraintStorage < ' tcx > {
@@ -312,28 +298,6 @@ impl<'tcx> RegionConstraintStorage<'tcx> {
312298 ) -> RegionConstraintCollector < ' a , ' tcx > {
313299 RegionConstraintCollector { storage : self , undo_log }
314300 }
315-
316- fn rollback_undo_entry ( & mut self , undo_entry : UndoLog < ' tcx > ) {
317- match undo_entry {
318- AddVar ( vid) => {
319- self . var_infos . pop ( ) . unwrap ( ) ;
320- assert_eq ! ( self . var_infos. len( ) , vid. index( ) ) ;
321- }
322- AddConstraint ( ref constraint) => {
323- self . data . constraints . remove ( constraint) ;
324- }
325- AddVerify ( index) => {
326- self . data . verifys . pop ( ) ;
327- assert_eq ! ( self . data. verifys. len( ) , index) ;
328- }
329- AddCombination ( Glb , ref regions) => {
330- self . glbs . remove ( regions) ;
331- }
332- AddCombination ( Lub , ref regions) => {
333- self . lubs . remove ( regions) ;
334- }
335- }
336- }
337301}
338302
339303impl < ' tcx > RegionConstraintCollector < ' _ , ' tcx > {
@@ -407,12 +371,32 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
407371
408372 pub ( super ) fn start_snapshot ( & mut self ) -> RegionSnapshot {
409373 debug ! ( "RegionConstraintCollector: start_snapshot" ) ;
410- RegionSnapshot { any_unifications : self . any_unifications }
374+ RegionSnapshot {
375+ any_unifications : self . any_unifications ,
376+ vars_len : self . var_infos . len ( ) ,
377+ constraints_len : self . data . constraints . len ( ) ,
378+ verifys_len : self . data . verifys . len ( ) ,
379+ glbs_len : self . glbs . len ( ) ,
380+ lubs_len : self . lubs . len ( ) ,
381+ }
411382 }
412383
413384 pub ( super ) fn rollback_to ( & mut self , snapshot : RegionSnapshot ) {
414385 debug ! ( "RegionConstraintCollector: rollback_to({:?})" , snapshot) ;
415- self . any_unifications = snapshot. any_unifications ;
386+ let RegionSnapshot {
387+ any_unifications,
388+ vars_len,
389+ constraints_len,
390+ verifys_len,
391+ glbs_len,
392+ lubs_len,
393+ } = snapshot;
394+ self . any_unifications = any_unifications;
395+ self . var_infos . truncate ( vars_len) ;
396+ self . data . constraints . truncate ( constraints_len) ;
397+ self . data . verifys . truncate ( verifys_len) ;
398+ self . glbs . truncate ( glbs_len) ;
399+ self . lubs . truncate ( lubs_len) ;
416400 }
417401
418402 pub ( super ) fn new_region_var (
@@ -424,7 +408,6 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
424408
425409 let u_vid = self . unification_table_mut ( ) . new_key ( UnifiedRegion :: new ( None ) ) ;
426410 assert_eq ! ( vid, u_vid. vid) ;
427- self . undo_log . push ( AddVar ( vid) ) ;
428411 debug ! ( "created new region variable {:?} in {:?} with origin {:?}" , vid, universe, origin) ;
429412 vid
430413 }
@@ -442,15 +425,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
442425 fn add_constraint ( & mut self , constraint : Constraint < ' tcx > , origin : SubregionOrigin < ' tcx > ) {
443426 // cannot add constraints once regions are resolved
444427 debug ! ( "RegionConstraintCollector: add_constraint({:?})" , constraint) ;
445-
446- // never overwrite an existing (constraint, origin) - only insert one if it isn't
447- // present in the map yet. This prevents origins from outside the snapshot being
448- // replaced with "less informative" origins e.g., during calls to `can_eq`
449- let undo_log = & mut self . undo_log ;
450- self . storage . data . constraints . entry ( constraint) . or_insert_with ( || {
451- undo_log. push ( AddConstraint ( constraint) ) ;
452- origin
453- } ) ;
428+ self . storage . data . constraints . push ( ( constraint, origin) ) ;
454429 }
455430
456431 fn add_verify ( & mut self , verify : Verify < ' tcx > ) {
@@ -464,9 +439,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
464439 return ;
465440 }
466441
467- let index = self . data . verifys . len ( ) ;
468442 self . data . verifys . push ( verify) ;
469- self . undo_log . push ( AddVerify ( index) ) ;
470443 }
471444
472445 pub ( super ) fn make_eqregion (
@@ -638,6 +611,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
638611 b : Region < ' tcx > ,
639612 origin : SubregionOrigin < ' tcx > ,
640613 ) -> Region < ' tcx > {
614+ //assert!(!UndoLogs::<super::UndoLog<'_>>::in_snapshot(&self.undo_log));
641615 let vars = TwoRegions { a, b } ;
642616 if let Some ( & c) = self . combine_map ( t) . get ( & vars) {
643617 return ty:: Region :: new_var ( tcx, c) ;
@@ -647,7 +621,6 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
647621 let c_universe = cmp:: max ( a_universe, b_universe) ;
648622 let c = self . new_region_var ( c_universe, MiscVariable ( origin. span ( ) ) ) ;
649623 self . combine_map ( t) . insert ( vars, c) ;
650- self . undo_log . push ( AddCombination ( t, vars) ) ;
651624 let new_r = ty:: Region :: new_var ( tcx, c) ;
652625 for old_r in [ a, b] {
653626 match t {
@@ -686,10 +659,16 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
686659 }
687660
688661 /// See `InferCtxt::region_constraints_added_in_snapshot`.
689- pub fn region_constraints_added_in_snapshot ( & self , mark : & Snapshot < ' tcx > ) -> bool {
690- self . undo_log
691- . region_constraints_in_snapshot ( mark)
692- . any ( |& elt| matches ! ( elt, AddConstraint ( _) ) )
662+ pub fn region_constraints_added_in_snapshot ( & self , snapshot : & RegionSnapshot ) -> bool {
663+ self . data . constraints . len ( ) != snapshot. constraints_len
664+ }
665+
666+ fn region_constraints_in_snapshot (
667+ & self ,
668+ snapshot : & RegionSnapshot ,
669+ ) -> impl Iterator < Item = & Constraint < ' tcx > > {
670+ assert_eq ! ( snapshot. verifys_len, self . data. verifys. len( ) ) ;
671+ self . data . constraints [ snapshot. constraints_len ..] . iter ( ) . map ( |( constraint, _) | constraint)
693672 }
694673
695674 #[ inline]
@@ -774,9 +753,3 @@ impl<'tcx> RegionConstraintData<'tcx> {
774753 constraints. is_empty ( ) && member_constraints. is_empty ( ) && verifys. is_empty ( )
775754 }
776755}
777-
778- impl < ' tcx > Rollback < UndoLog < ' tcx > > for RegionConstraintStorage < ' tcx > {
779- fn reverse ( & mut self , undo : UndoLog < ' tcx > ) {
780- self . rollback_undo_entry ( undo)
781- }
782- }
0 commit comments