Skip to content

Commit 204c915

Browse files
author
Markus Westerlind
committed
refactor: Extract the undo log to its own modules
1 parent bc7f7b2 commit 204c915

File tree

4 files changed

+272
-230
lines changed

4 files changed

+272
-230
lines changed

src/librustc_infer/infer/mod.rs

Lines changed: 9 additions & 225 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ pub use self::RegionVariableOrigin::*;
66
pub use self::SubregionOrigin::*;
77
pub use self::ValuePairs::*;
88

9+
pub(crate) use self::undo_log::{InferCtxtUndoLogs, Snapshot, UndoLog};
10+
911
use crate::traits::{self, ObligationCause, PredicateObligations, TraitEngine};
1012

1113
use rustc_ast::ast;
1214
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
13-
use rustc_data_structures::snapshot_vec as sv;
1415
use rustc_data_structures::sync::Lrc;
15-
use rustc_data_structures::undo_log::{Rollback, Snapshots, UndoLogs};
16+
use rustc_data_structures::undo_log::{Rollback, Snapshots};
1617
use rustc_data_structures::unify as ut;
1718
use rustc_errors::DiagnosticBuilder;
1819
use rustc_hir as hir;
@@ -69,6 +70,7 @@ pub mod region_constraints;
6970
pub mod resolve;
7071
mod sub;
7172
pub mod type_variable;
73+
mod undo_log;
7274

7375
use crate::infer::canonical::OriginalQueryValues;
7476
pub use rustc_middle::infer::unify_key;
@@ -85,6 +87,10 @@ pub type Bound<T> = Option<T>;
8587
pub type UnitResult<'tcx> = RelateResult<'tcx, ()>; // "unify result"
8688
pub 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-
495279
pub 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,

src/librustc_infer/infer/region_constraints/leak_check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<'tcx> RegionConstraintCollector<'tcx, '_> {
4848
let mut taint_set = TaintSet::new(TaintDirections::both(), placeholder_region);
4949
taint_set.fixed_point(
5050
tcx,
51-
self.undo_log.region_constraints(0),
51+
self.undo_log.region_constraints(),
5252
&self.storage.data.verifys,
5353
);
5454
let tainted_regions = taint_set.into_set();

src/librustc_infer/infer/region_constraints/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,6 @@ impl<'tcx> RegionConstraintCollector<'tcx, '_> {
500500

501501
let constraints_to_kill: Vec<usize> = self
502502
.undo_log
503-
.logs
504503
.iter()
505504
.enumerate()
506505
.rev()
@@ -514,7 +513,7 @@ impl<'tcx> RegionConstraintCollector<'tcx, '_> {
514513
.collect();
515514

516515
for index in constraints_to_kill {
517-
let undo_entry = match &mut self.undo_log.logs[index] {
516+
let undo_entry = match &mut self.undo_log[index] {
518517
super::UndoLog::RegionConstraintCollector(undo_entry) => {
519518
mem::replace(undo_entry, Purged)
520519
}
@@ -790,9 +789,9 @@ impl<'tcx> RegionConstraintCollector<'tcx, '_> {
790789
}
791790

792791
/// See [`RegionInference::region_constraints_added_in_snapshot`].
793-
pub fn region_constraints_added_in_snapshot(&self, mark: &Snapshot<'_>) -> Option<bool> {
792+
pub fn region_constraints_added_in_snapshot(&self, mark: &Snapshot<'tcx>) -> Option<bool> {
794793
self.undo_log
795-
.region_constraints(mark.undo_len)
794+
.region_constraints_in_snapshot(mark)
796795
.map(|&elt| match elt {
797796
AddConstraint(constraint) => Some(constraint.involves_placeholders()),
798797
_ => None,

0 commit comments

Comments
 (0)