Skip to content

Commit bc2fc7f

Browse files
author
Markus Westerlind
committed
Fix review comments
1 parent b61a28b commit bc2fc7f

File tree

4 files changed

+58
-98
lines changed

4 files changed

+58
-98
lines changed

src/librustc_infer/infer/mod.rs

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::traits::{self, ObligationCause, PredicateObligations, TraitEngine};
1313
use rustc_ast::ast;
1414
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1515
use rustc_data_structures::sync::Lrc;
16-
use rustc_data_structures::undo_log::{Rollback, Snapshots};
16+
use rustc_data_structures::undo_log::Rollback;
1717
use rustc_data_structures::unify as ut;
1818
use rustc_errors::DiagnosticBuilder;
1919
use rustc_hir as hir;
@@ -151,16 +151,16 @@ pub struct InferCtxtInner<'tcx> {
151151
/// We instantiate `UnificationTable` with `bounds<Ty>` because the types
152152
/// that might instantiate a general type variable have an order,
153153
/// represented by its upper and lower bounds.
154-
type_variables: type_variable::TypeVariableStorage<'tcx>,
154+
type_variable_storage: type_variable::TypeVariableStorage<'tcx>,
155155

156156
/// Map from const parameter variable to the kind of const it represents.
157-
const_unification_table: ut::UnificationTableStorage<ty::ConstVid<'tcx>>,
157+
const_unification_storage: ut::UnificationTableStorage<ty::ConstVid<'tcx>>,
158158

159159
/// Map from integral variable to the kind of integer it represents.
160-
int_unification_table: ut::UnificationTableStorage<ty::IntVid>,
160+
int_unification_storage: ut::UnificationTableStorage<ty::IntVid>,
161161

162162
/// Map from floating variable to the kind of float it represents.
163-
float_unification_table: ut::UnificationTableStorage<ty::FloatVid>,
163+
float_unification_storage: ut::UnificationTableStorage<ty::FloatVid>,
164164

165165
/// Tracks the set of region variables and the constraints between them.
166166
/// This is initially `Some(_)` but when
@@ -209,11 +209,11 @@ impl<'tcx> InferCtxtInner<'tcx> {
209209
fn new() -> InferCtxtInner<'tcx> {
210210
InferCtxtInner {
211211
projection_cache: Default::default(),
212-
type_variables: type_variable::TypeVariableStorage::new(),
212+
type_variable_storage: type_variable::TypeVariableStorage::new(),
213213
undo_log: InferCtxtUndoLogs::default(),
214-
const_unification_table: ut::UnificationTableStorage::new(),
215-
int_unification_table: ut::UnificationTableStorage::new(),
216-
float_unification_table: ut::UnificationTableStorage::new(),
214+
const_unification_storage: ut::UnificationTableStorage::new(),
215+
int_unification_storage: ut::UnificationTableStorage::new(),
216+
float_unification_storage: ut::UnificationTableStorage::new(),
217217
region_constraints: Some(RegionConstraintStorage::new()),
218218
region_obligations: vec![],
219219
}
@@ -223,12 +223,12 @@ impl<'tcx> InferCtxtInner<'tcx> {
223223
&self.region_obligations
224224
}
225225

226-
pub fn projection_cache(&mut self) -> traits::ProjectionCache<'tcx, '_> {
226+
pub fn projection_cache(&mut self) -> traits::ProjectionCache<'_, 'tcx> {
227227
self.projection_cache.with_log(&mut self.undo_log)
228228
}
229229

230-
fn type_variables(&mut self) -> type_variable::TypeVariableTable<'tcx, '_> {
231-
self.type_variables.with_log(&mut self.undo_log)
230+
fn type_variables(&mut self) -> type_variable::TypeVariableTable<'_, 'tcx> {
231+
self.type_variable_storage.with_log(&mut self.undo_log)
232232
}
233233

234234
fn int_unification_table(
@@ -240,7 +240,7 @@ impl<'tcx> InferCtxtInner<'tcx> {
240240
&mut InferCtxtUndoLogs<'tcx>,
241241
>,
242242
> {
243-
self.int_unification_table.with_log(&mut self.undo_log)
243+
self.int_unification_storage.with_log(&mut self.undo_log)
244244
}
245245

246246
fn float_unification_table(
@@ -252,7 +252,7 @@ impl<'tcx> InferCtxtInner<'tcx> {
252252
&mut InferCtxtUndoLogs<'tcx>,
253253
>,
254254
> {
255-
self.float_unification_table.with_log(&mut self.undo_log)
255+
self.float_unification_storage.with_log(&mut self.undo_log)
256256
}
257257

258258
fn const_unification_table(
@@ -264,7 +264,7 @@ impl<'tcx> InferCtxtInner<'tcx> {
264264
&mut InferCtxtUndoLogs<'tcx>,
265265
>,
266266
> {
267-
self.const_unification_table.with_log(&mut self.undo_log)
267+
self.const_unification_storage.with_log(&mut self.undo_log)
268268
}
269269

270270
pub fn unwrap_region_constraints(&mut self) -> RegionConstraintCollector<'tcx, '_> {
@@ -868,29 +868,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
868868
self.in_snapshot.set(was_in_snapshot);
869869
self.universe.set(universe);
870870

871-
let InferCtxtInner {
872-
type_variables,
873-
const_unification_table,
874-
int_unification_table,
875-
float_unification_table,
876-
region_constraints,
877-
projection_cache,
878-
region_obligations,
879-
undo_log,
880-
..
881-
} = &mut *self.inner.borrow_mut();
882-
undo_log.rollback_to(
883-
|| undo_log::RollbackView {
884-
type_variables,
885-
const_unification_table,
886-
int_unification_table,
887-
float_unification_table,
888-
region_constraints: region_constraints.as_mut().unwrap(),
889-
projection_cache,
890-
region_obligations,
891-
},
892-
undo_snapshot,
893-
);
871+
self.inner.borrow_mut().rollback_to(undo_snapshot);
894872
}
895873

896874
fn commit_from(&self, snapshot: CombinedSnapshot<'a, 'tcx>) {
@@ -900,8 +878,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
900878

901879
self.in_snapshot.set(was_in_snapshot);
902880

903-
let mut inner = self.inner.borrow_mut();
904-
inner.undo_log.commit(undo_snapshot);
881+
self.inner.borrow_mut().commit(undo_snapshot);
905882
}
906883

907884
/// Executes `f` and commit the bindings.

src/librustc_infer/infer/type_variable.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::cmp;
1111
use std::marker::PhantomData;
1212
use std::ops::Range;
1313

14-
use rustc_data_structures::undo_log::{Rollback, Snapshots, UndoLogs};
14+
use rustc_data_structures::undo_log::{Rollback, UndoLogs};
1515

1616
pub(crate) enum UndoLog<'tcx> {
1717
EqRelation(sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>),
@@ -81,7 +81,7 @@ pub struct TypeVariableStorage<'tcx> {
8181
sub_relations: ut::UnificationTableStorage<ty::TyVid>,
8282
}
8383

84-
pub struct TypeVariableTable<'tcx, 'a> {
84+
pub struct TypeVariableTable<'a, 'tcx> {
8585
values: &'a mut sv::SnapshotVecStorage<Delegate>,
8686

8787
eq_relations: &'a mut ut::UnificationTableStorage<TyVidEqKey<'tcx>>,
@@ -168,13 +168,13 @@ impl<'tcx> TypeVariableStorage<'tcx> {
168168
pub(crate) fn with_log<'a>(
169169
&'a mut self,
170170
undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
171-
) -> TypeVariableTable<'tcx, 'a> {
171+
) -> TypeVariableTable<'a, 'tcx> {
172172
let TypeVariableStorage { values, eq_relations, sub_relations } = self;
173173
TypeVariableTable { values, eq_relations, sub_relations, undo_log }
174174
}
175175
}
176176

177-
impl<'tcx> TypeVariableTable<'tcx, '_> {
177+
impl<'tcx> TypeVariableTable<'_, 'tcx> {
178178
/// Returns the diverges flag given when `vid` was created.
179179
///
180180
/// Note that this function does not return care whether

src/librustc_infer/infer/undo_log.rs

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
use std::marker::PhantomData;
22

33
use rustc_data_structures::snapshot_vec as sv;
4-
use rustc_data_structures::undo_log::{Rollback, Snapshots, UndoLogs};
4+
use rustc_data_structures::undo_log::{Rollback, UndoLogs};
55
use rustc_data_structures::unify as ut;
6-
use rustc_hir as hir;
76
use rustc_middle::ty;
87

98
use crate::{
10-
infer::{
11-
region_constraints::{self, RegionConstraintStorage},
12-
type_variable, RegionObligation,
13-
},
9+
infer::{region_constraints, type_variable, InferCtxtInner},
1410
traits,
1511
};
1612

@@ -19,6 +15,7 @@ pub struct Snapshot<'tcx> {
1915
_marker: PhantomData<&'tcx ()>,
2016
}
2117

18+
/// Records the 'undo' data fora single operation that affects some form of inference variable.
2219
pub(crate) enum UndoLog<'tcx> {
2320
TypeVariables(type_variable::UndoLog<'tcx>),
2421
ConstUnificationTable(sv::UndoLog<ut::Delegate<ty::ConstVid<'tcx>>>),
@@ -96,26 +93,18 @@ impl<'tcx> From<traits::UndoLog<'tcx>> for UndoLog<'tcx> {
9693
}
9794
}
9895

99-
pub(super) struct RollbackView<'tcx, 'a> {
100-
pub(super) type_variables: &'a mut type_variable::TypeVariableStorage<'tcx>,
101-
pub(super) const_unification_table: &'a mut ut::UnificationTableStorage<ty::ConstVid<'tcx>>,
102-
pub(super) int_unification_table: &'a mut ut::UnificationTableStorage<ty::IntVid>,
103-
pub(super) float_unification_table: &'a mut ut::UnificationTableStorage<ty::FloatVid>,
104-
pub(super) region_constraints: &'a mut RegionConstraintStorage<'tcx>,
105-
pub(super) projection_cache: &'a mut traits::ProjectionCacheStorage<'tcx>,
106-
pub(super) region_obligations: &'a mut Vec<(hir::HirId, RegionObligation<'tcx>)>,
107-
}
108-
109-
impl<'tcx> Rollback<UndoLog<'tcx>> for RollbackView<'tcx, '_> {
96+
impl<'tcx> Rollback<UndoLog<'tcx>> for InferCtxtInner<'tcx> {
11097
fn reverse(&mut self, undo: UndoLog<'tcx>) {
11198
match undo {
112-
UndoLog::TypeVariables(undo) => self.type_variables.reverse(undo),
113-
UndoLog::ConstUnificationTable(undo) => self.const_unification_table.reverse(undo),
114-
UndoLog::IntUnificationTable(undo) => self.int_unification_table.reverse(undo),
115-
UndoLog::FloatUnificationTable(undo) => self.float_unification_table.reverse(undo),
116-
UndoLog::RegionConstraintCollector(undo) => self.region_constraints.reverse(undo),
99+
UndoLog::TypeVariables(undo) => self.type_variable_storage.reverse(undo),
100+
UndoLog::ConstUnificationTable(undo) => self.const_unification_storage.reverse(undo),
101+
UndoLog::IntUnificationTable(undo) => self.int_unification_storage.reverse(undo),
102+
UndoLog::FloatUnificationTable(undo) => self.float_unification_storage.reverse(undo),
103+
UndoLog::RegionConstraintCollector(undo) => {
104+
self.region_constraints.as_mut().unwrap().reverse(undo)
105+
}
117106
UndoLog::RegionUnificationTable(undo) => {
118-
self.region_constraints.unification_table.reverse(undo)
107+
self.region_constraints.as_mut().unwrap().unification_table.reverse(undo)
119108
}
120109
UndoLog::ProjectionCache(undo) => self.projection_cache.reverse(undo),
121110
UndoLog::PushRegionObligation => {
@@ -163,58 +152,52 @@ where
163152
}
164153
}
165154

166-
impl<'tcx> Snapshots<UndoLog<'tcx>> for InferCtxtUndoLogs<'tcx> {
167-
type Snapshot = Snapshot<'tcx>;
168-
fn actions_since_snapshot(&self, snapshot: &Self::Snapshot) -> &[UndoLog<'tcx>] {
169-
&self.logs[snapshot.undo_len..]
170-
}
171-
172-
fn start_snapshot(&mut self) -> Self::Snapshot {
173-
self.num_open_snapshots += 1;
174-
Snapshot { undo_len: self.logs.len(), _marker: PhantomData }
175-
}
176-
177-
fn rollback_to<R>(&mut self, values: impl FnOnce() -> R, snapshot: Self::Snapshot)
178-
where
179-
R: Rollback<UndoLog<'tcx>>,
180-
{
155+
impl<'tcx> InferCtxtInner<'tcx> {
156+
pub fn rollback_to(&mut self, snapshot: Snapshot<'tcx>) {
181157
debug!("rollback_to({})", snapshot.undo_len);
182-
self.assert_open_snapshot(&snapshot);
158+
self.undo_log.assert_open_snapshot(&snapshot);
183159

184-
if self.logs.len() > snapshot.undo_len {
185-
let mut values = values();
186-
while self.logs.len() > snapshot.undo_len {
187-
values.reverse(self.logs.pop().unwrap());
188-
}
160+
while self.undo_log.logs.len() > snapshot.undo_len {
161+
let undo = self.undo_log.logs.pop().unwrap();
162+
self.reverse(undo);
189163
}
190164

191-
if self.num_open_snapshots == 1 {
165+
if self.undo_log.num_open_snapshots == 1 {
192166
// The root snapshot. It's safe to clear the undo log because
193167
// there's no snapshot further out that we might need to roll back
194168
// to.
195169
assert!(snapshot.undo_len == 0);
196-
self.logs.clear();
170+
self.undo_log.logs.clear();
197171
}
198172

199-
self.num_open_snapshots -= 1;
173+
self.undo_log.num_open_snapshots -= 1;
200174
}
201175

202-
fn commit(&mut self, snapshot: Self::Snapshot) {
176+
pub fn commit(&mut self, snapshot: Snapshot<'tcx>) {
203177
debug!("commit({})", snapshot.undo_len);
204178

205-
if self.num_open_snapshots == 1 {
179+
if self.undo_log.num_open_snapshots == 1 {
206180
// The root snapshot. It's safe to clear the undo log because
207181
// there's no snapshot further out that we might need to roll back
208182
// to.
209183
assert!(snapshot.undo_len == 0);
210-
self.logs.clear();
184+
self.undo_log.logs.clear();
211185
}
212186

213-
self.num_open_snapshots -= 1;
187+
self.undo_log.num_open_snapshots -= 1;
214188
}
215189
}
216190

217191
impl<'tcx> InferCtxtUndoLogs<'tcx> {
192+
pub fn actions_since_snapshot(&self, snapshot: &Snapshot<'tcx>) -> &[UndoLog<'tcx>] {
193+
&self.logs[snapshot.undo_len..]
194+
}
195+
196+
pub fn start_snapshot(&mut self) -> Snapshot<'tcx> {
197+
self.num_open_snapshots += 1;
198+
Snapshot { undo_len: self.logs.len(), _marker: PhantomData }
199+
}
200+
218201
pub(crate) fn region_constraints_in_snapshot(
219202
&self,
220203
s: &Snapshot<'tcx>,

src/librustc_infer/traits/project.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'tcx, T> Normalized<'tcx, T> {
6565
//
6666
// FIXME: we probably also want some sort of cross-infcx cache here to
6767
// reduce the amount of duplication. Let's see what we get with the Chalk reforms.
68-
pub struct ProjectionCache<'tcx, 'a> {
68+
pub struct ProjectionCache<'a, 'tcx> {
6969
map: &'a mut SnapshotMapStorage<ProjectionCacheKey<'tcx>, ProjectionCacheEntry<'tcx>>,
7070
undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
7171
}
@@ -98,12 +98,12 @@ impl<'tcx> ProjectionCacheStorage<'tcx> {
9898
pub(crate) fn with_log<'a>(
9999
&'a mut self,
100100
undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
101-
) -> ProjectionCache<'tcx, 'a> {
101+
) -> ProjectionCache<'a, 'tcx> {
102102
ProjectionCache { map: &mut self.map, undo_log }
103103
}
104104
}
105105

106-
impl<'tcx> ProjectionCache<'tcx, '_> {
106+
impl<'tcx> ProjectionCache<'_, 'tcx> {
107107
fn map(
108108
&mut self,
109109
) -> SnapshotMapRef<

0 commit comments

Comments
 (0)