Skip to content

Commit

Permalink
Restore the snapshot/rollback optimization for region constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Westerlind committed May 5, 2020
1 parent f7f6245 commit 3f85338
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
30 changes: 23 additions & 7 deletions src/librustc_infer/infer/mod.rs
Expand Up @@ -45,7 +45,9 @@ use self::free_regions::RegionRelations;
use self::lexical_region_resolve::LexicalRegionResolutions;
use self::outlives::env::OutlivesEnvironment;
use self::region_constraints::{GenericKind, RegionConstraintData, VarInfos, VerifyBound};
use self::region_constraints::{RegionConstraintCollector, RegionConstraintStorage};
use self::region_constraints::{
RegionConstraintCollector, RegionConstraintStorage, RegionSnapshot,
};
use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};

pub mod at;
Expand Down Expand Up @@ -265,7 +267,7 @@ impl<'tcx> InferCtxtInner<'tcx> {
self.const_unification_storage.with_log(&mut self.undo_log)
}

pub fn unwrap_region_constraints(&mut self) -> RegionConstraintCollector<'tcx, '_> {
pub fn unwrap_region_constraints(&mut self) -> RegionConstraintCollector<'_, 'tcx> {
self.region_constraint_storage
.as_mut()
.expect("region constraints already solved")
Expand Down Expand Up @@ -706,6 +708,7 @@ impl<'tcx> InferOk<'tcx, ()> {
#[must_use = "once you start a snapshot, you should always consume it"]
pub struct CombinedSnapshot<'a, 'tcx> {
undo_snapshot: Snapshot<'tcx>,
region_constraints_snapshot: RegionSnapshot,
universe: ty::UniverseIndex,
was_in_snapshot: bool,
_in_progress_tables: Option<Ref<'a, ty::TypeckTables<'tcx>>>,
Expand Down Expand Up @@ -827,6 +830,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

CombinedSnapshot {
undo_snapshot: inner.undo_log.start_snapshot(),
region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(),
universe: self.universe(),
was_in_snapshot: in_snapshot,
// Borrow tables "in progress" (i.e., during typeck)
Expand All @@ -837,19 +841,31 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

fn rollback_to(&self, cause: &str, snapshot: CombinedSnapshot<'a, 'tcx>) {
debug!("rollback_to(cause={})", cause);
let CombinedSnapshot { undo_snapshot, universe, was_in_snapshot, _in_progress_tables } =
snapshot;
let CombinedSnapshot {
undo_snapshot,
region_constraints_snapshot,
universe,
was_in_snapshot,
_in_progress_tables,
} = snapshot;

self.in_snapshot.set(was_in_snapshot);
self.universe.set(universe);

self.inner.borrow_mut().rollback_to(undo_snapshot);
let mut inner = self.inner.borrow_mut();
inner.rollback_to(undo_snapshot);
inner.unwrap_region_constraints().rollback_to(region_constraints_snapshot);
}

fn commit_from(&self, snapshot: CombinedSnapshot<'a, 'tcx>) {
debug!("commit_from()");
let CombinedSnapshot { undo_snapshot, universe: _, was_in_snapshot, _in_progress_tables } =
snapshot;
let CombinedSnapshot {
undo_snapshot,
region_constraints_snapshot: _,
universe: _,
was_in_snapshot,
_in_progress_tables,
} = snapshot;

self.in_snapshot.set(was_in_snapshot);

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_infer/infer/region_constraints/leak_check.rs
Expand Up @@ -4,7 +4,7 @@ use rustc_data_structures::undo_log::UndoLogs;
use rustc_middle::ty::error::TypeError;
use rustc_middle::ty::relate::RelateResult;

impl<'tcx> RegionConstraintCollector<'tcx, '_> {
impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
/// Searches region constraints created since `snapshot` that
/// affect one of the placeholders in `placeholder_map`, returning
/// an error if any of the placeholders are related to another
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_infer/infer/region_constraints/mod.rs
Expand Up @@ -61,19 +61,19 @@ pub struct RegionConstraintStorage<'tcx> {
any_unifications: bool,
}

pub struct RegionConstraintCollector<'tcx, 'a> {
pub struct RegionConstraintCollector<'a, 'tcx> {
storage: &'a mut RegionConstraintStorage<'tcx>,
undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
}

impl std::ops::Deref for RegionConstraintCollector<'tcx, '_> {
impl std::ops::Deref for RegionConstraintCollector<'_, 'tcx> {
type Target = RegionConstraintStorage<'tcx>;
fn deref(&self) -> &RegionConstraintStorage<'tcx> {
self.storage
}
}

impl std::ops::DerefMut for RegionConstraintCollector<'tcx, '_> {
impl std::ops::DerefMut for RegionConstraintCollector<'_, 'tcx> {
fn deref_mut(&mut self) -> &mut RegionConstraintStorage<'tcx> {
self.storage
}
Expand Down Expand Up @@ -348,7 +348,7 @@ impl<'tcx> RegionConstraintStorage<'tcx> {
pub(crate) fn with_log<'a>(
&'a mut self,
undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
) -> RegionConstraintCollector<'tcx, 'a> {
) -> RegionConstraintCollector<'a, 'tcx> {
RegionConstraintCollector { storage: self, undo_log }
}

Expand Down Expand Up @@ -381,7 +381,7 @@ impl<'tcx> RegionConstraintStorage<'tcx> {
}
}

impl<'tcx> RegionConstraintCollector<'tcx, '_> {
impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
pub fn num_region_vars(&self) -> usize {
self.var_infos.len()
}
Expand Down

0 comments on commit 3f85338

Please sign in to comment.