Skip to content

Commit 2339317

Browse files
committed
move snapshot handling into mod
1 parent de3c965 commit 2339317

File tree

10 files changed

+120
-117
lines changed

10 files changed

+120
-117
lines changed

compiler/rustc_infer/src/infer/mod.rs

+3-98
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ pub use self::SubregionOrigin::*;
77
pub use self::ValuePairs::*;
88
pub use relate::combine::ObligationEmittingRelation;
99
use rustc_data_structures::captures::Captures;
10-
use rustc_data_structures::undo_log::UndoLogs;
1110
use rustc_middle::infer::unify_key::EffectVarValue;
1211
use rustc_middle::infer::unify_key::{ConstVidKey, EffectVidKey};
1312

1413
use self::opaque_types::OpaqueTypeStorage;
15-
pub(crate) use self::undo_log::{InferCtxtUndoLogs, Snapshot, UndoLog};
1614

1715
use crate::traits::{
1816
self, ObligationCause, ObligationInspector, PredicateObligations, TraitEngine, TraitEngineExt,
@@ -50,28 +48,26 @@ use self::error_reporting::TypeErrCtxt;
5048
use self::free_regions::RegionRelations;
5149
use self::lexical_region_resolve::LexicalRegionResolutions;
5250
use self::region_constraints::{GenericKind, VarInfos, VerifyBound};
53-
use self::region_constraints::{
54-
RegionConstraintCollector, RegionConstraintStorage, RegionSnapshot,
55-
};
51+
use self::region_constraints::{RegionConstraintCollector, RegionConstraintStorage};
5652
pub use self::relate::combine::CombineFields;
5753
pub use self::relate::StructurallyRelateAliases;
54+
use self::snapshot::undo_log::InferCtxtUndoLogs;
5855
use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
5956

6057
pub mod at;
6158
pub mod canonical;
6259
pub mod error_reporting;
6360
pub mod free_regions;
6461
mod freshen;
65-
mod fudge;
6662
mod lexical_region_resolve;
6763
pub mod opaque_types;
6864
pub mod outlives;
6965
mod projection;
7066
pub mod region_constraints;
7167
mod relate;
7268
pub mod resolve;
69+
pub(crate) mod snapshot;
7370
pub mod type_variable;
74-
mod undo_log;
7571

7672
#[must_use]
7773
#[derive(Debug)]
@@ -738,13 +734,6 @@ impl<'tcx> InferOk<'tcx, ()> {
738734
}
739735
}
740736

741-
#[must_use = "once you start a snapshot, you should always consume it"]
742-
pub struct CombinedSnapshot<'tcx> {
743-
undo_snapshot: Snapshot<'tcx>,
744-
region_constraints_snapshot: RegionSnapshot,
745-
universe: ty::UniverseIndex,
746-
}
747-
748737
impl<'tcx> InferCtxt<'tcx> {
749738
pub fn dcx(&self) -> &'tcx DiagCtxt {
750739
self.tcx.dcx()
@@ -842,90 +831,6 @@ impl<'tcx> InferCtxt<'tcx> {
842831
}
843832
}
844833

845-
pub fn in_snapshot(&self) -> bool {
846-
UndoLogs::<UndoLog<'tcx>>::in_snapshot(&self.inner.borrow_mut().undo_log)
847-
}
848-
849-
pub fn num_open_snapshots(&self) -> usize {
850-
UndoLogs::<UndoLog<'tcx>>::num_open_snapshots(&self.inner.borrow_mut().undo_log)
851-
}
852-
853-
fn start_snapshot(&self) -> CombinedSnapshot<'tcx> {
854-
debug!("start_snapshot()");
855-
856-
let mut inner = self.inner.borrow_mut();
857-
858-
CombinedSnapshot {
859-
undo_snapshot: inner.undo_log.start_snapshot(),
860-
region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(),
861-
universe: self.universe(),
862-
}
863-
}
864-
865-
#[instrument(skip(self, snapshot), level = "debug")]
866-
fn rollback_to(&self, snapshot: CombinedSnapshot<'tcx>) {
867-
let CombinedSnapshot { undo_snapshot, region_constraints_snapshot, universe } = snapshot;
868-
869-
self.universe.set(universe);
870-
871-
let mut inner = self.inner.borrow_mut();
872-
inner.rollback_to(undo_snapshot);
873-
inner.unwrap_region_constraints().rollback_to(region_constraints_snapshot);
874-
}
875-
876-
#[instrument(skip(self, snapshot), level = "debug")]
877-
fn commit_from(&self, snapshot: CombinedSnapshot<'tcx>) {
878-
let CombinedSnapshot { undo_snapshot, region_constraints_snapshot: _, universe: _ } =
879-
snapshot;
880-
881-
self.inner.borrow_mut().commit(undo_snapshot);
882-
}
883-
884-
/// Execute `f` and commit the bindings if closure `f` returns `Ok(_)`.
885-
#[instrument(skip(self, f), level = "debug")]
886-
pub fn commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E>
887-
where
888-
F: FnOnce(&CombinedSnapshot<'tcx>) -> Result<T, E>,
889-
{
890-
let snapshot = self.start_snapshot();
891-
let r = f(&snapshot);
892-
debug!("commit_if_ok() -- r.is_ok() = {}", r.is_ok());
893-
match r {
894-
Ok(_) => {
895-
self.commit_from(snapshot);
896-
}
897-
Err(_) => {
898-
self.rollback_to(snapshot);
899-
}
900-
}
901-
r
902-
}
903-
904-
/// Execute `f` then unroll any bindings it creates.
905-
#[instrument(skip(self, f), level = "debug")]
906-
pub fn probe<R, F>(&self, f: F) -> R
907-
where
908-
F: FnOnce(&CombinedSnapshot<'tcx>) -> R,
909-
{
910-
let snapshot = self.start_snapshot();
911-
let r = f(&snapshot);
912-
self.rollback_to(snapshot);
913-
r
914-
}
915-
916-
/// Scan the constraints produced since `snapshot` and check whether
917-
/// we added any region constraints.
918-
pub fn region_constraints_added_in_snapshot(&self, snapshot: &CombinedSnapshot<'tcx>) -> bool {
919-
self.inner
920-
.borrow_mut()
921-
.unwrap_region_constraints()
922-
.region_constraints_added_in_snapshot(&snapshot.undo_snapshot)
923-
}
924-
925-
pub fn opaque_types_added_in_snapshot(&self, snapshot: &CombinedSnapshot<'tcx>) -> bool {
926-
self.inner.borrow().undo_log.opaque_types_in_snapshot(&snapshot.undo_snapshot)
927-
}
928-
929834
pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, expected: T, actual: T) -> bool
930835
where
931836
T: at::ToTrace<'tcx>,

compiler/rustc_infer/src/infer/opaque_types/table.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_data_structures::undo_log::UndoLogs;
22
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty};
33

4-
use crate::infer::{InferCtxtUndoLogs, UndoLog};
4+
use crate::infer::snapshot::undo_log::{InferCtxtUndoLogs, UndoLog};
55

66
use super::{OpaqueTypeDecl, OpaqueTypeMap};
77

compiler/rustc_infer/src/infer/outlives/obligations.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ use crate::infer::outlives::components::{push_outlives_components, Component};
6363
use crate::infer::outlives::env::RegionBoundPairs;
6464
use crate::infer::outlives::verify::VerifyBoundCx;
6565
use crate::infer::resolve::OpportunisticRegionResolver;
66-
use crate::infer::{
67-
self, GenericKind, InferCtxt, RegionObligation, SubregionOrigin, UndoLog, VerifyBound,
68-
};
66+
use crate::infer::snapshot::undo_log::UndoLog;
67+
use crate::infer::{self, GenericKind, InferCtxt, RegionObligation, SubregionOrigin, VerifyBound};
6968
use crate::traits::{ObligationCause, ObligationCauseCode};
7069
use rustc_data_structures::undo_log::UndoLogs;
7170
use rustc_middle::mir::ConstraintCategory;

compiler/rustc_infer/src/infer/region_constraints/leak_check.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use super::*;
2-
use crate::infer::CombinedSnapshot;
3-
use rustc_data_structures::{
4-
fx::FxIndexMap,
5-
graph::{scc::Sccs, vec_graph::VecGraph},
6-
};
2+
use crate::infer::snapshot::CombinedSnapshot;
3+
use rustc_data_structures::fx::FxIndexMap;
4+
use rustc_data_structures::graph::{scc::Sccs, vec_graph::VecGraph};
75
use rustc_index::Idx;
86
use rustc_middle::ty::error::TypeError;
97
use rustc_middle::ty::relate::RelateResult;

compiler/rustc_infer/src/infer/region_constraints/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
use self::CombineMapType::*;
44
use self::UndoLog::*;
55

6-
use super::{
7-
InferCtxtUndoLogs, MiscVariable, RegionVariableOrigin, Rollback, Snapshot, SubregionOrigin,
8-
};
6+
use super::{MiscVariable, RegionVariableOrigin, Rollback, SubregionOrigin};
7+
use crate::infer::snapshot::undo_log::{InferCtxtUndoLogs, Snapshot};
98

109
use rustc_data_structures::fx::FxHashMap;
1110
use rustc_data_structures::sync::Lrc;
@@ -360,7 +359,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
360359
///
361360
/// Not legal during a snapshot.
362361
pub fn into_infos_and_data(self) -> (VarInfos, RegionConstraintData<'tcx>) {
363-
assert!(!UndoLogs::<super::UndoLog<'_>>::in_snapshot(&self.undo_log));
362+
assert!(!UndoLogs::<UndoLog<'_>>::in_snapshot(&self.undo_log));
364363
(mem::take(&mut self.storage.var_infos), mem::take(&mut self.storage.data))
365364
}
366365

@@ -377,7 +376,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
377376
///
378377
/// Not legal during a snapshot.
379378
pub fn take_and_reset_data(&mut self) -> RegionConstraintData<'tcx> {
380-
assert!(!UndoLogs::<super::UndoLog<'_>>::in_snapshot(&self.undo_log));
379+
assert!(!UndoLogs::<UndoLog<'_>>::in_snapshot(&self.undo_log));
381380

382381
// If you add a new field to `RegionConstraintCollector`, you
383382
// should think carefully about whether it needs to be cleared

compiler/rustc_infer/src/infer/relate/higher_ranked.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Helper routines for higher-ranked things. See the `doc` module at
22
//! the end of the file for details.
33
4-
use crate::infer::CombinedSnapshot;
4+
use crate::infer::snapshot::CombinedSnapshot;
55
use crate::infer::InferCtxt;
66
use rustc_middle::ty::fold::FnMutDelegate;
77
use rustc_middle::ty::relate::RelateResult;

compiler/rustc_infer/src/infer/fudge.rs renamed to compiler/rustc_infer/src/infer/snapshot/fudge.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use rustc_middle::infer::unify_key::{ConstVariableOriginKind, ConstVariableValue
22
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
33
use rustc_middle::ty::{self, ConstVid, FloatVid, IntVid, RegionVid, Ty, TyCtxt, TyVid};
44

5-
use super::type_variable::TypeVariableOrigin;
6-
use super::InferCtxt;
7-
use super::{ConstVariableOrigin, RegionVariableOrigin, UnificationTable};
5+
use crate::infer::type_variable::TypeVariableOrigin;
6+
use crate::infer::InferCtxt;
7+
use crate::infer::{ConstVariableOrigin, RegionVariableOrigin, UnificationTable};
88

99
use rustc_data_structures::snapshot_vec as sv;
1010
use rustc_data_structures::unify as ut;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
use super::region_constraints::RegionSnapshot;
2+
use super::InferCtxt;
3+
use rustc_data_structures::undo_log::UndoLogs;
4+
use rustc_middle::ty;
5+
6+
mod fudge;
7+
pub(crate) mod undo_log;
8+
9+
use undo_log::{Snapshot, UndoLog};
10+
11+
#[must_use = "once you start a snapshot, you should always consume it"]
12+
pub struct CombinedSnapshot<'tcx> {
13+
pub(super) undo_snapshot: Snapshot<'tcx>,
14+
region_constraints_snapshot: RegionSnapshot,
15+
universe: ty::UniverseIndex,
16+
}
17+
18+
impl<'tcx> InferCtxt<'tcx> {
19+
pub fn in_snapshot(&self) -> bool {
20+
UndoLogs::<UndoLog<'tcx>>::in_snapshot(&self.inner.borrow_mut().undo_log)
21+
}
22+
23+
pub fn num_open_snapshots(&self) -> usize {
24+
UndoLogs::<UndoLog<'tcx>>::num_open_snapshots(&self.inner.borrow_mut().undo_log)
25+
}
26+
27+
fn start_snapshot(&self) -> CombinedSnapshot<'tcx> {
28+
debug!("start_snapshot()");
29+
30+
let mut inner = self.inner.borrow_mut();
31+
32+
CombinedSnapshot {
33+
undo_snapshot: inner.undo_log.start_snapshot(),
34+
region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(),
35+
universe: self.universe(),
36+
}
37+
}
38+
39+
#[instrument(skip(self, snapshot), level = "debug")]
40+
fn rollback_to(&self, snapshot: CombinedSnapshot<'tcx>) {
41+
let CombinedSnapshot { undo_snapshot, region_constraints_snapshot, universe } = snapshot;
42+
43+
self.universe.set(universe);
44+
45+
let mut inner = self.inner.borrow_mut();
46+
inner.rollback_to(undo_snapshot);
47+
inner.unwrap_region_constraints().rollback_to(region_constraints_snapshot);
48+
}
49+
50+
#[instrument(skip(self, snapshot), level = "debug")]
51+
fn commit_from(&self, snapshot: CombinedSnapshot<'tcx>) {
52+
let CombinedSnapshot { undo_snapshot, region_constraints_snapshot: _, universe: _ } =
53+
snapshot;
54+
55+
self.inner.borrow_mut().commit(undo_snapshot);
56+
}
57+
58+
/// Execute `f` and commit the bindings if closure `f` returns `Ok(_)`.
59+
#[instrument(skip(self, f), level = "debug")]
60+
pub fn commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E>
61+
where
62+
F: FnOnce(&CombinedSnapshot<'tcx>) -> Result<T, E>,
63+
{
64+
let snapshot = self.start_snapshot();
65+
let r = f(&snapshot);
66+
debug!("commit_if_ok() -- r.is_ok() = {}", r.is_ok());
67+
match r {
68+
Ok(_) => {
69+
self.commit_from(snapshot);
70+
}
71+
Err(_) => {
72+
self.rollback_to(snapshot);
73+
}
74+
}
75+
r
76+
}
77+
78+
/// Execute `f` then unroll any bindings it creates.
79+
#[instrument(skip(self, f), level = "debug")]
80+
pub fn probe<R, F>(&self, f: F) -> R
81+
where
82+
F: FnOnce(&CombinedSnapshot<'tcx>) -> R,
83+
{
84+
let snapshot = self.start_snapshot();
85+
let r = f(&snapshot);
86+
self.rollback_to(snapshot);
87+
r
88+
}
89+
90+
/// Scan the constraints produced since `snapshot` and check whether
91+
/// we added any region constraints.
92+
pub fn region_constraints_added_in_snapshot(&self, snapshot: &CombinedSnapshot<'tcx>) -> bool {
93+
self.inner
94+
.borrow_mut()
95+
.unwrap_region_constraints()
96+
.region_constraints_added_in_snapshot(&snapshot.undo_snapshot)
97+
}
98+
99+
pub fn opaque_types_added_in_snapshot(&self, snapshot: &CombinedSnapshot<'tcx>) -> bool {
100+
self.inner.borrow().undo_log.opaque_types_in_snapshot(&snapshot.undo_snapshot)
101+
}
102+
}

compiler/rustc_infer/src/traits/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::PredicateObligation;
44

5-
use crate::infer::InferCtxtUndoLogs;
5+
use crate::infer::snapshot::undo_log::InferCtxtUndoLogs;
66

77
use rustc_data_structures::{
88
snapshot_map::{self, SnapshotMapRef, SnapshotMapStorage},

0 commit comments

Comments
 (0)