@@ -6,13 +6,14 @@ pub use self::RegionVariableOrigin::*;
66pub use self :: SubregionOrigin :: * ;
77pub use self :: ValuePairs :: * ;
88
9+ pub ( crate ) use self :: undo_log:: { InferCtxtUndoLogs , Snapshot , UndoLog } ;
10+
911use crate :: traits:: { self , ObligationCause , PredicateObligations , TraitEngine } ;
1012
1113use rustc_ast:: ast;
1214use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
13- use rustc_data_structures:: snapshot_vec as sv;
1415use rustc_data_structures:: sync:: Lrc ;
15- use rustc_data_structures:: undo_log:: { Rollback , Snapshots , UndoLogs } ;
16+ use rustc_data_structures:: undo_log:: { Rollback , Snapshots } ;
1617use rustc_data_structures:: unify as ut;
1718use rustc_errors:: DiagnosticBuilder ;
1819use rustc_hir as hir;
@@ -69,6 +70,7 @@ pub mod region_constraints;
6970pub mod resolve;
7071mod sub;
7172pub mod type_variable;
73+ mod undo_log;
7274
7375use crate :: infer:: canonical:: OriginalQueryValues ;
7476pub use rustc_middle:: infer:: unify_key;
@@ -85,6 +87,10 @@ pub type Bound<T> = Option<T>;
8587pub type UnitResult < ' tcx > = RelateResult < ' tcx , ( ) > ; // "unify result"
8688pub type FixupResult < ' tcx , T > = Result < T , FixupError < ' tcx > > ; // "fixup result"
8789
90+ pub ( crate ) type UnificationTable < ' a , ' tcx , T > = ut:: UnificationTable <
91+ ut:: InPlace < T , & ' a mut ut:: UnificationStorage < T > , & ' a mut InferCtxtUndoLogs < ' tcx > > ,
92+ > ;
93+
8894/// How we should handle region solving.
8995///
9096/// This is used so that the region values inferred by HIR region solving are
@@ -270,228 +276,6 @@ impl<'tcx> InferCtxtInner<'tcx> {
270276 }
271277}
272278
273- pub struct Snapshot < ' tcx > {
274- undo_len : usize ,
275- _marker : PhantomData < & ' tcx ( ) > ,
276- }
277-
278- pub ( crate ) enum UndoLog < ' tcx > {
279- TypeVariables ( type_variable:: UndoLog < ' tcx > ) ,
280- ConstUnificationTable ( sv:: UndoLog < ut:: Delegate < ty:: ConstVid < ' tcx > > > ) ,
281- IntUnificationTable ( sv:: UndoLog < ut:: Delegate < ty:: IntVid > > ) ,
282- FloatUnificationTable ( sv:: UndoLog < ut:: Delegate < ty:: FloatVid > > ) ,
283- RegionConstraintCollector ( region_constraints:: UndoLog < ' tcx > ) ,
284- RegionUnificationTable ( sv:: UndoLog < ut:: Delegate < ty:: RegionVid > > ) ,
285- ProjectionCache ( traits:: UndoLog < ' tcx > ) ,
286- PushRegionObligation ,
287- }
288-
289- impl < ' tcx > From < region_constraints:: UndoLog < ' tcx > > for UndoLog < ' tcx > {
290- fn from ( l : region_constraints:: UndoLog < ' tcx > ) -> Self {
291- UndoLog :: RegionConstraintCollector ( l)
292- }
293- }
294-
295- impl < ' tcx > From < sv:: UndoLog < ut:: Delegate < type_variable:: TyVidEqKey < ' tcx > > > > for UndoLog < ' tcx > {
296- fn from ( l : sv:: UndoLog < ut:: Delegate < type_variable:: TyVidEqKey < ' tcx > > > ) -> Self {
297- UndoLog :: TypeVariables ( type_variable:: UndoLog :: EqRelation ( l) )
298- }
299- }
300-
301- impl < ' tcx > From < sv:: UndoLog < ut:: Delegate < ty:: TyVid > > > for UndoLog < ' tcx > {
302- fn from ( l : sv:: UndoLog < ut:: Delegate < ty:: TyVid > > ) -> Self {
303- UndoLog :: TypeVariables ( type_variable:: UndoLog :: SubRelation ( l) )
304- }
305- }
306-
307- impl < ' tcx > From < sv:: UndoLog < type_variable:: Delegate > > for UndoLog < ' tcx > {
308- fn from ( l : sv:: UndoLog < type_variable:: Delegate > ) -> Self {
309- UndoLog :: TypeVariables ( type_variable:: UndoLog :: Values ( l) )
310- }
311- }
312-
313- impl < ' tcx > From < type_variable:: Instantiate > for UndoLog < ' tcx > {
314- fn from ( l : type_variable:: Instantiate ) -> Self {
315- UndoLog :: TypeVariables ( type_variable:: UndoLog :: from ( l) )
316- }
317- }
318-
319- impl From < type_variable:: UndoLog < ' tcx > > for UndoLog < ' tcx > {
320- fn from ( t : type_variable:: UndoLog < ' tcx > ) -> Self {
321- Self :: TypeVariables ( t)
322- }
323- }
324-
325- impl < ' tcx > From < sv:: UndoLog < ut:: Delegate < ty:: ConstVid < ' tcx > > > > for UndoLog < ' tcx > {
326- fn from ( l : sv:: UndoLog < ut:: Delegate < ty:: ConstVid < ' tcx > > > ) -> Self {
327- Self :: ConstUnificationTable ( l)
328- }
329- }
330-
331- impl < ' tcx > From < sv:: UndoLog < ut:: Delegate < ty:: IntVid > > > for UndoLog < ' tcx > {
332- fn from ( l : sv:: UndoLog < ut:: Delegate < ty:: IntVid > > ) -> Self {
333- Self :: IntUnificationTable ( l)
334- }
335- }
336-
337- impl < ' tcx > From < sv:: UndoLog < ut:: Delegate < ty:: FloatVid > > > for UndoLog < ' tcx > {
338- fn from ( l : sv:: UndoLog < ut:: Delegate < ty:: FloatVid > > ) -> Self {
339- Self :: FloatUnificationTable ( l)
340- }
341- }
342-
343- impl < ' tcx > From < sv:: UndoLog < ut:: Delegate < ty:: RegionVid > > > for UndoLog < ' tcx > {
344- fn from ( l : sv:: UndoLog < ut:: Delegate < ty:: RegionVid > > ) -> Self {
345- Self :: RegionUnificationTable ( l)
346- }
347- }
348-
349- impl < ' tcx > From < traits:: UndoLog < ' tcx > > for UndoLog < ' tcx > {
350- fn from ( l : traits:: UndoLog < ' tcx > ) -> Self {
351- Self :: ProjectionCache ( l)
352- }
353- }
354-
355- pub ( crate ) type UnificationTable < ' a , ' tcx , T > = ut:: UnificationTable <
356- ut:: InPlace < T , & ' a mut ut:: UnificationStorage < T > , & ' a mut InferCtxtUndoLogs < ' tcx > > ,
357- > ;
358-
359- struct RollbackView < ' tcx , ' a > {
360- type_variables : & ' a mut type_variable:: TypeVariableStorage < ' tcx > ,
361- const_unification_table : & ' a mut ut:: UnificationStorage < ty:: ConstVid < ' tcx > > ,
362- int_unification_table : & ' a mut ut:: UnificationStorage < ty:: IntVid > ,
363- float_unification_table : & ' a mut ut:: UnificationStorage < ty:: FloatVid > ,
364- region_constraints : & ' a mut RegionConstraintStorage < ' tcx > ,
365- projection_cache : & ' a mut traits:: ProjectionCacheStorage < ' tcx > ,
366- region_obligations : & ' a mut Vec < ( hir:: HirId , RegionObligation < ' tcx > ) > ,
367- }
368-
369- impl < ' tcx > Rollback < UndoLog < ' tcx > > for RollbackView < ' tcx , ' _ > {
370- fn reverse ( & mut self , undo : UndoLog < ' tcx > ) {
371- match undo {
372- UndoLog :: TypeVariables ( undo) => self . type_variables . reverse ( undo) ,
373- UndoLog :: ConstUnificationTable ( undo) => self . const_unification_table . reverse ( undo) ,
374- UndoLog :: IntUnificationTable ( undo) => self . int_unification_table . reverse ( undo) ,
375- UndoLog :: FloatUnificationTable ( undo) => self . float_unification_table . reverse ( undo) ,
376- UndoLog :: RegionConstraintCollector ( undo) => self . region_constraints . reverse ( undo) ,
377- UndoLog :: RegionUnificationTable ( undo) => {
378- self . region_constraints . unification_table . reverse ( undo)
379- }
380- UndoLog :: ProjectionCache ( undo) => self . projection_cache . reverse ( undo) ,
381- UndoLog :: PushRegionObligation => {
382- self . region_obligations . pop ( ) ;
383- }
384- }
385- }
386- }
387-
388- pub ( crate ) struct InferCtxtUndoLogs < ' tcx > {
389- logs : Vec < UndoLog < ' tcx > > ,
390- num_open_snapshots : usize ,
391- }
392-
393- impl Default for InferCtxtUndoLogs < ' _ > {
394- fn default ( ) -> Self {
395- Self { logs : Default :: default ( ) , num_open_snapshots : Default :: default ( ) }
396- }
397- }
398-
399- impl < ' tcx , T > UndoLogs < T > for InferCtxtUndoLogs < ' tcx >
400- where
401- UndoLog < ' tcx > : From < T > ,
402- {
403- fn num_open_snapshots ( & self ) -> usize {
404- self . num_open_snapshots
405- }
406- fn push ( & mut self , undo : T ) {
407- if self . in_snapshot ( ) {
408- self . logs . push ( undo. into ( ) )
409- }
410- }
411- fn clear ( & mut self ) {
412- self . logs . clear ( ) ;
413- self . num_open_snapshots = 0 ;
414- }
415- fn extend < J > ( & mut self , undos : J )
416- where
417- Self : Sized ,
418- J : IntoIterator < Item = T > ,
419- {
420- if self . in_snapshot ( ) {
421- self . logs . extend ( undos. into_iter ( ) . map ( UndoLog :: from) )
422- }
423- }
424- }
425-
426- impl < ' tcx > Snapshots < UndoLog < ' tcx > > for InferCtxtUndoLogs < ' tcx > {
427- type Snapshot = Snapshot < ' tcx > ;
428- fn actions_since_snapshot ( & self , snapshot : & Self :: Snapshot ) -> & [ UndoLog < ' tcx > ] {
429- & self . logs [ snapshot. undo_len ..]
430- }
431-
432- fn start_snapshot ( & mut self ) -> Self :: Snapshot {
433- self . num_open_snapshots += 1 ;
434- Snapshot { undo_len : self . logs . len ( ) , _marker : PhantomData }
435- }
436-
437- fn rollback_to < R > ( & mut self , values : impl FnOnce ( ) -> R , snapshot : Self :: Snapshot )
438- where
439- R : Rollback < UndoLog < ' tcx > > ,
440- {
441- debug ! ( "rollback_to({})" , snapshot. undo_len) ;
442- self . assert_open_snapshot ( & snapshot) ;
443-
444- if self . logs . len ( ) > snapshot. undo_len {
445- let mut values = values ( ) ;
446- while self . logs . len ( ) > snapshot. undo_len {
447- values. reverse ( self . logs . pop ( ) . unwrap ( ) ) ;
448- }
449- }
450-
451- if self . num_open_snapshots == 1 {
452- // The root snapshot. It's safe to clear the undo log because
453- // there's no snapshot further out that we might need to roll back
454- // to.
455- assert ! ( snapshot. undo_len == 0 ) ;
456- self . logs . clear ( ) ;
457- }
458-
459- self . num_open_snapshots -= 1 ;
460- }
461-
462- fn commit ( & mut self , snapshot : Self :: Snapshot ) {
463- debug ! ( "commit({})" , snapshot. undo_len) ;
464-
465- if self . num_open_snapshots == 1 {
466- // The root snapshot. It's safe to clear the undo log because
467- // there's no snapshot further out that we might need to roll back
468- // to.
469- assert ! ( snapshot. undo_len == 0 ) ;
470- self . logs . clear ( ) ;
471- }
472-
473- self . num_open_snapshots -= 1 ;
474- }
475- }
476-
477- impl < ' tcx > InferCtxtUndoLogs < ' tcx > {
478- pub ( crate ) fn region_constraints (
479- & self ,
480- after : usize ,
481- ) -> impl Iterator < Item = & ' _ region_constraints:: UndoLog < ' tcx > > + Clone {
482- self . logs [ after..] . iter ( ) . filter_map ( |log| match log {
483- UndoLog :: RegionConstraintCollector ( log) => Some ( log) ,
484- _ => None ,
485- } )
486- }
487-
488- fn assert_open_snapshot ( & self , snapshot : & Snapshot < ' tcx > ) {
489- // Failures here may indicate a failure to follow a stack discipline.
490- assert ! ( self . logs. len( ) >= snapshot. undo_len) ;
491- assert ! ( self . num_open_snapshots > 0 ) ;
492- }
493- }
494-
495279pub struct InferCtxt < ' a , ' tcx > {
496280 pub tcx : TyCtxt < ' tcx > ,
497281
@@ -1097,7 +881,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
1097881 ..
1098882 } = & mut * self . inner . borrow_mut ( ) ;
1099883 undo_log. rollback_to (
1100- || RollbackView {
884+ || undo_log :: RollbackView {
1101885 type_variables,
1102886 const_unification_table,
1103887 int_unification_table,
0 commit comments