Skip to content

Commit 4a2a6bc

Browse files
author
Markus Westerlind
committed
refactor: Move probe_fudge into fudge.rs
1 parent 6f495f3 commit 4a2a6bc

File tree

2 files changed

+41
-42
lines changed

2 files changed

+41
-42
lines changed

src/librustc_infer/infer/fudge.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
22
use rustc_middle::ty::{self, ConstVid, FloatVid, IntVid, RegionVid, Ty, TyCtxt, TyVid};
33

4-
use super::type_variable::TypeVariableOrigin;
4+
use super::region_constraints::RegionSnapshot;
5+
use super::type_variable::{self, TypeVariableOrigin};
56
use super::InferCtxt;
6-
use super::{ConstVariableOrigin, RegionVariableOrigin, UnificationTable};
7+
use super::{CombinedSnapshot, ConstVariableOrigin, RegionVariableOrigin, UnificationTable};
78

89
use rustc_data_structures::snapshot_vec as sv;
910
use rustc_data_structures::unify as ut;
@@ -35,7 +36,44 @@ fn const_vars_since_snapshot<'tcx>(
3536
)
3637
}
3738

39+
/// Extends `CombinedSnapshot` by tracking which variables were added in the snapshot
40+
#[must_use = "once you start a snapshot, you should always consume it"]
41+
struct FudgeSnapshot<'a, 'tcx> {
42+
snapshot: CombinedSnapshot<'a, 'tcx>,
43+
region_constraints_snapshot: RegionSnapshot,
44+
type_snapshot: type_variable::Snapshot<'tcx>,
45+
const_var_len: usize,
46+
int_var_len: usize,
47+
float_var_len: usize,
48+
}
49+
3850
impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
51+
/// Like `probe` but provides information about which variables were created in the snapshot,
52+
/// allowing for inference fudging
53+
fn probe_fudge<R, F>(&self, f: F) -> R
54+
where
55+
F: FnOnce(&FudgeSnapshot<'a, 'tcx>) -> R,
56+
{
57+
debug!("probe()");
58+
let snapshot = self.start_fudge_snapshot();
59+
let r = f(&snapshot);
60+
self.rollback_to("probe", snapshot.snapshot);
61+
r
62+
}
63+
64+
fn start_fudge_snapshot(&self) -> FudgeSnapshot<'a, 'tcx> {
65+
let snapshot = self.start_snapshot();
66+
let mut inner = self.inner.borrow_mut();
67+
FudgeSnapshot {
68+
snapshot,
69+
type_snapshot: inner.type_variables().snapshot(),
70+
const_var_len: inner.const_unification_table().len(),
71+
int_var_len: inner.int_unification_table().len(),
72+
float_var_len: inner.float_unification_table().len(),
73+
region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(),
74+
}
75+
}
76+
3977
/// This rather funky routine is used while processing expected
4078
/// types. What happens here is that we want to propagate a
4179
/// coercion through the return type of a fn to its

src/librustc_infer/infer/mod.rs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ use self::free_regions::RegionRelations;
4545
use self::lexical_region_resolve::LexicalRegionResolutions;
4646
use self::outlives::env::OutlivesEnvironment;
4747
use self::region_constraints::{GenericKind, RegionConstraintData, VarInfos, VerifyBound};
48-
use self::region_constraints::{
49-
RegionConstraintCollector, RegionConstraintStorage, RegionSnapshot,
50-
};
48+
use self::region_constraints::{RegionConstraintCollector, RegionConstraintStorage};
5149
use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
5250

5351
pub mod at;
@@ -705,17 +703,6 @@ impl<'tcx> InferOk<'tcx, ()> {
705703
}
706704
}
707705

708-
/// Extends `CombinedSnapshot` by tracking which variables were added in the snapshot
709-
#[must_use = "once you start a snapshot, you should always consume it"]
710-
pub struct FudgeSnapshot<'a, 'tcx> {
711-
snapshot: CombinedSnapshot<'a, 'tcx>,
712-
region_constraints_snapshot: RegionSnapshot,
713-
type_snapshot: type_variable::Snapshot<'tcx>,
714-
const_var_len: usize,
715-
int_var_len: usize,
716-
float_var_len: usize,
717-
}
718-
719706
#[must_use = "once you start a snapshot, you should always consume it"]
720707
pub struct CombinedSnapshot<'a, 'tcx> {
721708
undo_snapshot: Snapshot<'tcx>,
@@ -831,19 +818,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
831818
result
832819
}
833820

834-
fn start_fudge_snapshot(&self) -> FudgeSnapshot<'a, 'tcx> {
835-
let snapshot = self.start_snapshot();
836-
let mut inner = self.inner.borrow_mut();
837-
FudgeSnapshot {
838-
snapshot,
839-
type_snapshot: inner.type_variables().snapshot(),
840-
const_var_len: inner.const_unification_table().len(),
841-
int_var_len: inner.int_unification_table().len(),
842-
float_var_len: inner.float_unification_table().len(),
843-
region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(),
844-
}
845-
}
846-
847821
fn start_snapshot(&self) -> CombinedSnapshot<'a, 'tcx> {
848822
debug!("start_snapshot()");
849823

@@ -926,19 +900,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
926900
r
927901
}
928902

929-
/// Like `probe` but provides information about which variables were created in the snapshot,
930-
/// allowing for inference fudging
931-
pub fn probe_fudge<R, F>(&self, f: F) -> R
932-
where
933-
F: FnOnce(&FudgeSnapshot<'a, 'tcx>) -> R,
934-
{
935-
debug!("probe()");
936-
let snapshot = self.start_fudge_snapshot();
937-
let r = f(&snapshot);
938-
self.rollback_to("probe", snapshot.snapshot);
939-
r
940-
}
941-
942903
/// If `should_skip` is true, then execute `f` then unroll any bindings it creates.
943904
pub fn probe_maybe_skip_leak_check<R, F>(&self, should_skip: bool, f: F) -> R
944905
where

0 commit comments

Comments
 (0)