Skip to content

Commit

Permalink
refactor: Move probe_fudge into fudge.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Westerlind committed May 5, 2020
1 parent 6f495f3 commit 4a2a6bc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 42 deletions.
42 changes: 40 additions & 2 deletions src/librustc_infer/infer/fudge.rs
@@ -1,9 +1,10 @@
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
use rustc_middle::ty::{self, ConstVid, FloatVid, IntVid, RegionVid, Ty, TyCtxt, TyVid};

use super::type_variable::TypeVariableOrigin;
use super::region_constraints::RegionSnapshot;
use super::type_variable::{self, TypeVariableOrigin};
use super::InferCtxt;
use super::{ConstVariableOrigin, RegionVariableOrigin, UnificationTable};
use super::{CombinedSnapshot, ConstVariableOrigin, RegionVariableOrigin, UnificationTable};

use rustc_data_structures::snapshot_vec as sv;
use rustc_data_structures::unify as ut;
Expand Down Expand Up @@ -35,7 +36,44 @@ fn const_vars_since_snapshot<'tcx>(
)
}

/// Extends `CombinedSnapshot` by tracking which variables were added in the snapshot
#[must_use = "once you start a snapshot, you should always consume it"]
struct FudgeSnapshot<'a, 'tcx> {
snapshot: CombinedSnapshot<'a, 'tcx>,
region_constraints_snapshot: RegionSnapshot,
type_snapshot: type_variable::Snapshot<'tcx>,
const_var_len: usize,
int_var_len: usize,
float_var_len: usize,
}

impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
/// Like `probe` but provides information about which variables were created in the snapshot,
/// allowing for inference fudging
fn probe_fudge<R, F>(&self, f: F) -> R
where
F: FnOnce(&FudgeSnapshot<'a, 'tcx>) -> R,
{
debug!("probe()");
let snapshot = self.start_fudge_snapshot();
let r = f(&snapshot);
self.rollback_to("probe", snapshot.snapshot);
r
}

fn start_fudge_snapshot(&self) -> FudgeSnapshot<'a, 'tcx> {
let snapshot = self.start_snapshot();
let mut inner = self.inner.borrow_mut();
FudgeSnapshot {
snapshot,
type_snapshot: inner.type_variables().snapshot(),
const_var_len: inner.const_unification_table().len(),
int_var_len: inner.int_unification_table().len(),
float_var_len: inner.float_unification_table().len(),
region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(),
}
}

/// This rather funky routine is used while processing expected
/// types. What happens here is that we want to propagate a
/// coercion through the return type of a fn to its
Expand Down
41 changes: 1 addition & 40 deletions src/librustc_infer/infer/mod.rs
Expand Up @@ -45,9 +45,7 @@ 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, RegionSnapshot,
};
use self::region_constraints::{RegionConstraintCollector, RegionConstraintStorage};
use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};

pub mod at;
Expand Down Expand Up @@ -705,17 +703,6 @@ impl<'tcx> InferOk<'tcx, ()> {
}
}

/// Extends `CombinedSnapshot` by tracking which variables were added in the snapshot
#[must_use = "once you start a snapshot, you should always consume it"]
pub struct FudgeSnapshot<'a, 'tcx> {
snapshot: CombinedSnapshot<'a, 'tcx>,
region_constraints_snapshot: RegionSnapshot,
type_snapshot: type_variable::Snapshot<'tcx>,
const_var_len: usize,
int_var_len: usize,
float_var_len: usize,
}

#[must_use = "once you start a snapshot, you should always consume it"]
pub struct CombinedSnapshot<'a, 'tcx> {
undo_snapshot: Snapshot<'tcx>,
Expand Down Expand Up @@ -831,19 +818,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
result
}

fn start_fudge_snapshot(&self) -> FudgeSnapshot<'a, 'tcx> {
let snapshot = self.start_snapshot();
let mut inner = self.inner.borrow_mut();
FudgeSnapshot {
snapshot,
type_snapshot: inner.type_variables().snapshot(),
const_var_len: inner.const_unification_table().len(),
int_var_len: inner.int_unification_table().len(),
float_var_len: inner.float_unification_table().len(),
region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(),
}
}

fn start_snapshot(&self) -> CombinedSnapshot<'a, 'tcx> {
debug!("start_snapshot()");

Expand Down Expand Up @@ -926,19 +900,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
r
}

/// Like `probe` but provides information about which variables were created in the snapshot,
/// allowing for inference fudging
pub fn probe_fudge<R, F>(&self, f: F) -> R
where
F: FnOnce(&FudgeSnapshot<'a, 'tcx>) -> R,
{
debug!("probe()");
let snapshot = self.start_fudge_snapshot();
let r = f(&snapshot);
self.rollback_to("probe", snapshot.snapshot);
r
}

/// If `should_skip` is true, then execute `f` then unroll any bindings it creates.
pub fn probe_maybe_skip_leak_check<R, F>(&self, should_skip: bool, f: F) -> R
where
Expand Down

0 comments on commit 4a2a6bc

Please sign in to comment.