Skip to content

Commit

Permalink
refactor: Replace probe_fudge by an explict call for the lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Westerlind committed May 5, 2020
1 parent 4a2a6bc commit f7f6245
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 59 deletions.
54 changes: 18 additions & 36 deletions src/librustc_infer/infer/fudge.rs
@@ -1,10 +1,9 @@
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
use rustc_middle::ty::{self, ConstVid, FloatVid, IntVid, RegionVid, Ty, TyCtxt, TyVid};

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

use rustc_data_structures::snapshot_vec as sv;
use rustc_data_structures::unify as ut;
Expand All @@ -14,13 +13,13 @@ use std::ops::Range;

fn vars_since_snapshot<'tcx, T>(
table: &mut UnificationTable<'_, 'tcx, T>,
snapshot: usize,
snapshot_var_len: usize,
) -> Range<T>
where
T: UnifyKey,
super::UndoLog<'tcx>: From<sv::UndoLog<ut::Delegate<T>>>,
{
T::from_index(snapshot as u32)..T::from_index(table.len() as u32)
T::from_index(snapshot_var_len as u32)..T::from_index(table.len() as u32)
}

fn const_vars_since_snapshot<'tcx>(
Expand All @@ -36,41 +35,23 @@ 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>,
struct VariableLengths {
type_var_len: usize,
const_var_len: usize,
int_var_len: usize,
float_var_len: usize,
region_constraints_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();
fn variable_lengths(&self) -> VariableLengths {
let mut inner = self.inner.borrow_mut();
FudgeSnapshot {
snapshot,
type_snapshot: inner.type_variables().snapshot(),
VariableLengths {
type_var_len: inner.type_variables().num_vars(),
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(),
region_constraints_len: inner.unwrap_region_constraints().num_region_vars(),
}
}

Expand Down Expand Up @@ -120,7 +101,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
{
debug!("fudge_inference_if_ok()");

let (mut fudger, value) = self.probe_fudge(|snapshot| {
let variable_lengths = self.variable_lengths();
let (mut fudger, value) = self.probe(|_| {
match f() {
Ok(value) => {
let value = self.resolve_vars_if_possible(&value);
Expand All @@ -133,21 +115,21 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

let mut inner = self.inner.borrow_mut();
let type_vars =
inner.type_variables().vars_since_snapshot(&snapshot.type_snapshot);
inner.type_variables().vars_since_snapshot(variable_lengths.type_var_len);
let int_vars = vars_since_snapshot(
&mut inner.int_unification_table(),
snapshot.int_var_len,
variable_lengths.int_var_len,
);
let float_vars = vars_since_snapshot(
&mut inner.float_unification_table(),
snapshot.float_var_len,
variable_lengths.float_var_len,
);
let region_vars = inner
.unwrap_region_constraints()
.vars_since_snapshot(&snapshot.region_constraints_snapshot);
.vars_since_snapshot(variable_lengths.region_constraints_len);
let const_vars = const_vars_since_snapshot(
&mut inner.const_unification_table(),
snapshot.const_var_len,
variable_lengths.const_var_len,
);

let fudger = InferenceFudger {
Expand Down
10 changes: 3 additions & 7 deletions src/librustc_infer/infer/region_constraints/mod.rs
Expand Up @@ -312,7 +312,6 @@ pub struct RegionVariableInfo {
}

pub struct RegionSnapshot {
value_count: usize,
any_unifications: bool,
}

Expand Down Expand Up @@ -454,10 +453,7 @@ impl<'tcx> RegionConstraintCollector<'tcx, '_> {

pub fn start_snapshot(&mut self) -> RegionSnapshot {
debug!("RegionConstraintCollector: start_snapshot");
RegionSnapshot {
value_count: self.unification_table.len(),
any_unifications: self.any_unifications,
}
RegionSnapshot { any_unifications: self.any_unifications }
}

pub fn rollback_to(&mut self, snapshot: RegionSnapshot) {
Expand Down Expand Up @@ -776,9 +772,9 @@ impl<'tcx> RegionConstraintCollector<'tcx, '_> {

pub fn vars_since_snapshot(
&self,
mark: &RegionSnapshot,
value_count: usize,
) -> (Range<RegionVid>, Vec<RegionVariableOrigin>) {
let range = RegionVid::from_index(mark.value_count as u32)
let range = RegionVid::from_index(value_count as u32)
..RegionVid::from_index(self.unification_table.len() as u32);
(
range.clone(),
Expand Down
18 changes: 2 additions & 16 deletions src/librustc_infer/infer/type_variable.rs
Expand Up @@ -150,11 +150,6 @@ impl<'tcx> TypeVariableValue<'tcx> {
}
}

pub struct Snapshot<'tcx> {
value_count: u32,
_marker: PhantomData<&'tcx ()>,
}

pub(crate) struct Instantiate {
vid: ty::TyVid,
}
Expand Down Expand Up @@ -324,14 +319,6 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
}
}

/// Creates a snapshot of the type variable state. This snapshot
/// must later be committed (`commit()`) or rolled back
/// (`rollback_to()`). Nested snapshots are permitted, but must
/// be processed in a stack-like fashion.
pub fn snapshot(&mut self) -> Snapshot<'tcx> {
Snapshot { value_count: self.eq_relations().len() as u32, _marker: PhantomData }
}

fn values(
&mut self,
) -> sv::SnapshotVec<Delegate, &mut Vec<TypeVariableData>, &mut InferCtxtUndoLogs<'tcx>> {
Expand All @@ -349,10 +336,9 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
/// Returns a range of the type variables created during the snapshot.
pub fn vars_since_snapshot(
&mut self,
s: &Snapshot<'tcx>,
value_count: usize,
) -> (Range<TyVid>, Vec<TypeVariableOrigin>) {
let range =
TyVid { index: s.value_count }..TyVid { index: self.eq_relations().len() as u32 };
let range = TyVid { index: value_count as u32 }..TyVid { index: self.num_vars() as u32 };
(
range.start..range.end,
(range.start.index..range.end.index)
Expand Down

0 comments on commit f7f6245

Please sign in to comment.