Skip to content

Commit f7f6245

Browse files
author
Markus Westerlind
committed
refactor: Replace probe_fudge by an explict call for the lengths
1 parent 4a2a6bc commit f7f6245

File tree

3 files changed

+23
-59
lines changed

3 files changed

+23
-59
lines changed

src/librustc_infer/infer/fudge.rs

+18-36
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
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::region_constraints::RegionSnapshot;
5-
use super::type_variable::{self, TypeVariableOrigin};
4+
use super::type_variable::TypeVariableOrigin;
65
use super::InferCtxt;
7-
use super::{CombinedSnapshot, ConstVariableOrigin, RegionVariableOrigin, UnificationTable};
6+
use super::{ConstVariableOrigin, RegionVariableOrigin, UnificationTable};
87

98
use rustc_data_structures::snapshot_vec as sv;
109
use rustc_data_structures::unify as ut;
@@ -14,13 +13,13 @@ use std::ops::Range;
1413

1514
fn vars_since_snapshot<'tcx, T>(
1615
table: &mut UnificationTable<'_, 'tcx, T>,
17-
snapshot: usize,
16+
snapshot_var_len: usize,
1817
) -> Range<T>
1918
where
2019
T: UnifyKey,
2120
super::UndoLog<'tcx>: From<sv::UndoLog<ut::Delegate<T>>>,
2221
{
23-
T::from_index(snapshot as u32)..T::from_index(table.len() as u32)
22+
T::from_index(snapshot_var_len as u32)..T::from_index(table.len() as u32)
2423
}
2524

2625
fn const_vars_since_snapshot<'tcx>(
@@ -36,41 +35,23 @@ fn const_vars_since_snapshot<'tcx>(
3635
)
3736
}
3837

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>,
38+
struct VariableLengths {
39+
type_var_len: usize,
4540
const_var_len: usize,
4641
int_var_len: usize,
4742
float_var_len: usize,
43+
region_constraints_len: usize,
4844
}
4945

5046
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();
47+
fn variable_lengths(&self) -> VariableLengths {
6648
let mut inner = self.inner.borrow_mut();
67-
FudgeSnapshot {
68-
snapshot,
69-
type_snapshot: inner.type_variables().snapshot(),
49+
VariableLengths {
50+
type_var_len: inner.type_variables().num_vars(),
7051
const_var_len: inner.const_unification_table().len(),
7152
int_var_len: inner.int_unification_table().len(),
7253
float_var_len: inner.float_unification_table().len(),
73-
region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(),
54+
region_constraints_len: inner.unwrap_region_constraints().num_region_vars(),
7455
}
7556
}
7657

@@ -120,7 +101,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
120101
{
121102
debug!("fudge_inference_if_ok()");
122103

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

134116
let mut inner = self.inner.borrow_mut();
135117
let type_vars =
136-
inner.type_variables().vars_since_snapshot(&snapshot.type_snapshot);
118+
inner.type_variables().vars_since_snapshot(variable_lengths.type_var_len);
137119
let int_vars = vars_since_snapshot(
138120
&mut inner.int_unification_table(),
139-
snapshot.int_var_len,
121+
variable_lengths.int_var_len,
140122
);
141123
let float_vars = vars_since_snapshot(
142124
&mut inner.float_unification_table(),
143-
snapshot.float_var_len,
125+
variable_lengths.float_var_len,
144126
);
145127
let region_vars = inner
146128
.unwrap_region_constraints()
147-
.vars_since_snapshot(&snapshot.region_constraints_snapshot);
129+
.vars_since_snapshot(variable_lengths.region_constraints_len);
148130
let const_vars = const_vars_since_snapshot(
149131
&mut inner.const_unification_table(),
150-
snapshot.const_var_len,
132+
variable_lengths.const_var_len,
151133
);
152134

153135
let fudger = InferenceFudger {

src/librustc_infer/infer/region_constraints/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ pub struct RegionVariableInfo {
312312
}
313313

314314
pub struct RegionSnapshot {
315-
value_count: usize,
316315
any_unifications: bool,
317316
}
318317

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

455454
pub fn start_snapshot(&mut self) -> RegionSnapshot {
456455
debug!("RegionConstraintCollector: start_snapshot");
457-
RegionSnapshot {
458-
value_count: self.unification_table.len(),
459-
any_unifications: self.any_unifications,
460-
}
456+
RegionSnapshot { any_unifications: self.any_unifications }
461457
}
462458

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

777773
pub fn vars_since_snapshot(
778774
&self,
779-
mark: &RegionSnapshot,
775+
value_count: usize,
780776
) -> (Range<RegionVid>, Vec<RegionVariableOrigin>) {
781-
let range = RegionVid::from_index(mark.value_count as u32)
777+
let range = RegionVid::from_index(value_count as u32)
782778
..RegionVid::from_index(self.unification_table.len() as u32);
783779
(
784780
range.clone(),

src/librustc_infer/infer/type_variable.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ impl<'tcx> TypeVariableValue<'tcx> {
150150
}
151151
}
152152

153-
pub struct Snapshot<'tcx> {
154-
value_count: u32,
155-
_marker: PhantomData<&'tcx ()>,
156-
}
157-
158153
pub(crate) struct Instantiate {
159154
vid: ty::TyVid,
160155
}
@@ -324,14 +319,6 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
324319
}
325320
}
326321

327-
/// Creates a snapshot of the type variable state. This snapshot
328-
/// must later be committed (`commit()`) or rolled back
329-
/// (`rollback_to()`). Nested snapshots are permitted, but must
330-
/// be processed in a stack-like fashion.
331-
pub fn snapshot(&mut self) -> Snapshot<'tcx> {
332-
Snapshot { value_count: self.eq_relations().len() as u32, _marker: PhantomData }
333-
}
334-
335322
fn values(
336323
&mut self,
337324
) -> sv::SnapshotVec<Delegate, &mut Vec<TypeVariableData>, &mut InferCtxtUndoLogs<'tcx>> {
@@ -349,10 +336,9 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
349336
/// Returns a range of the type variables created during the snapshot.
350337
pub fn vars_since_snapshot(
351338
&mut self,
352-
s: &Snapshot<'tcx>,
339+
value_count: usize,
353340
) -> (Range<TyVid>, Vec<TypeVariableOrigin>) {
354-
let range =
355-
TyVid { index: s.value_count }..TyVid { index: self.eq_relations().len() as u32 };
341+
let range = TyVid { index: value_count as u32 }..TyVid { index: self.num_vars() as u32 };
356342
(
357343
range.start..range.end,
358344
(range.start.index..range.end.index)

0 commit comments

Comments
 (0)