Skip to content

Commit cd86ce1

Browse files
author
Markus Westerlind
committed
Rebase on top of undo_log unification
1 parent 31fb673 commit cd86ce1

File tree

18 files changed

+421
-207
lines changed

18 files changed

+421
-207
lines changed

compiler/rustc_data_structures/src/logged_unification_table.rs

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use crate::unify_log as ul;
99

1010
use ena::undo_log::{Rollback, Snapshots, UndoLogs};
1111

12-
enum UndoLog<K: ut::UnifyKey, I> {
12+
pub enum UndoLog<K: ut::UnifyKey, I = K> {
1313
Relation(sv::UndoLog<ut::Delegate<K>>),
1414
UnifyLog(ul::Undo<I>),
15-
ModifiedSet(ms::Undo),
15+
ModifiedSet(ms::Undo<I>),
1616
}
1717

1818
impl<K: ut::UnifyKey, I> From<sv::UndoLog<ut::Delegate<K>>> for UndoLog<K, I> {
@@ -27,8 +27,8 @@ impl<K: ut::UnifyKey, I> From<ul::Undo<I>> for UndoLog<K, I> {
2727
}
2828
}
2929

30-
impl<K: ut::UnifyKey, I> From<ms::Undo> for UndoLog<K, I> {
31-
fn from(l: ms::Undo) -> Self {
30+
impl<K: ut::UnifyKey, I> From<ms::Undo<I>> for UndoLog<K, I> {
31+
fn from(l: ms::Undo<I>) -> Self {
3232
UndoLog::ModifiedSet(l)
3333
}
3434
}
@@ -56,6 +56,10 @@ where
5656
self.logs.push(undo.into())
5757
}
5858
}
59+
fn clear(&mut self) {
60+
self.logs.clear();
61+
self.num_open_snapshots = 0;
62+
}
5963
fn extend<J>(&mut self, undos: J)
6064
where
6165
Self: Sized,
@@ -67,13 +71,7 @@ where
6771
}
6872
}
6973

70-
struct RollbackView<'a, K: ut::UnifyKey, I: Idx> {
71-
relations: &'a mut ut::UnificationStorage<K>,
72-
unify_log: &'a mut ul::UnifyLog<I>,
73-
modified_set: &'a mut ms::ModifiedSet<I>,
74-
}
75-
76-
impl<K: ut::UnifyKey, I: Idx> Rollback<UndoLog<K, I>> for RollbackView<'_, K, I> {
74+
impl<K: ut::UnifyKey, I: Idx> Rollback<UndoLog<K, I>> for LoggedUnificationTableStorage<K, I> {
7775
fn reverse(&mut self, undo: UndoLog<K, I>) {
7876
match undo {
7977
UndoLog::Relation(undo) => self.relations.reverse(undo),
@@ -93,12 +91,18 @@ impl<K: ut::UnifyKey, I: Idx> Snapshots<UndoLog<K, I>> for Logs<K, I> {
9391
unreachable!()
9492
}
9593

96-
fn rollback_to(&mut self, values: &mut impl Rollback<UndoLog<K, I>>, snapshot: Self::Snapshot) {
94+
fn rollback_to<R>(&mut self, values: impl FnOnce() -> R, snapshot: Self::Snapshot)
95+
where
96+
R: Rollback<UndoLog<K, I>>,
97+
{
9798
debug!("rollback_to({})", snapshot.undo_len);
9899
self.assert_open_snapshot(&snapshot);
99100

100-
while self.logs.len() > snapshot.undo_len {
101-
values.reverse(self.logs.pop().unwrap());
101+
if self.logs.len() > snapshot.undo_len {
102+
let mut values = values();
103+
while self.logs.len() > snapshot.undo_len {
104+
values.reverse(self.logs.pop().unwrap());
105+
}
102106
}
103107

104108
if self.num_open_snapshots == 1 {
@@ -135,14 +139,18 @@ impl<K: ut::UnifyKey, I: Idx> Logs<K, I> {
135139
}
136140
}
137141

138-
pub struct LoggedUnificationTable<K: ut::UnifyKey, I: Idx = K> {
139-
relations: ut::UnificationStorage<K>,
142+
pub struct LoggedUnificationTableStorage<K: ut::UnifyKey, I: Idx = K> {
143+
relations: ut::UnificationTableStorage<K>,
140144
unify_log: ul::UnifyLog<I>,
141145
modified_set: ms::ModifiedSet<I>,
142-
undo_log: Logs<K, I>,
143146
}
144147

145-
impl<K, I> LoggedUnificationTable<K, I>
148+
pub struct LoggedUnificationTable<'a, K: ut::UnifyKey, I: Idx, L> {
149+
storage: &'a mut LoggedUnificationTableStorage<K, I>,
150+
undo_log: L,
151+
}
152+
153+
impl<K, I> LoggedUnificationTableStorage<K, I>
146154
where
147155
K: ut::UnifyKey + From<I>,
148156
I: Idx + From<K>,
@@ -152,14 +160,34 @@ where
152160
relations: Default::default(),
153161
unify_log: ul::UnifyLog::new(),
154162
modified_set: ms::ModifiedSet::new(),
155-
undo_log: Logs::default(),
156163
}
157164
}
158165

166+
pub fn with_log<L>(&mut self, undo_log: L) -> LoggedUnificationTable<'_, K, I, L> {
167+
LoggedUnificationTable { storage: self, undo_log }
168+
}
169+
}
170+
171+
impl<K, I, L> LoggedUnificationTable<'_, K, I, L>
172+
where
173+
K: ut::UnifyKey,
174+
I: Idx,
175+
{
176+
pub fn len(&self) -> usize {
177+
self.storage.relations.len()
178+
}
179+
}
180+
181+
impl<K, I, L> LoggedUnificationTable<'_, K, I, L>
182+
where
183+
K: ut::UnifyKey + From<I>,
184+
I: Idx + From<K>,
185+
L: UndoLogs<ms::Undo<I>> + UndoLogs<ul::Undo<I>> + UndoLogs<sv::UndoLog<ut::Delegate<K>>>,
186+
{
159187
fn relations(
160188
&mut self,
161-
) -> ut::UnificationTable<ut::InPlace<K, &mut ut::UnificationStorage<K>, &mut Logs<K, I>>> {
162-
ut::UnificationTable::with_log(&mut self.relations, &mut self.undo_log)
189+
) -> ut::UnificationTable<ut::InPlace<K, &mut ut::UnificationStorage<K>, &mut L>> {
190+
ut::UnificationTable::with_log(&mut self.storage.relations, &mut self.undo_log)
163191
}
164192

165193
pub fn unify(&mut self, a: I, b: I)
@@ -173,9 +201,9 @@ where
173201
where
174202
K::Value: ut::UnifyValue<Error = ut::NoError>,
175203
{
176-
if self.unify_log.needs_log(vid) {
204+
if self.storage.unify_log.needs_log(vid) {
177205
warn!("ModifiedSet {:?} => {:?}", vid, ty);
178-
self.modified_set.set(&mut self.undo_log, vid);
206+
self.storage.modified_set.set(&mut self.undo_log, vid);
179207
}
180208
let vid = vid.into();
181209
let mut relations = self.relations();
@@ -195,8 +223,8 @@ where
195223
value: K::Value,
196224
) -> Result<(), <K::Value as ut::UnifyValue>::Error> {
197225
let vid = self.find(vid).into();
198-
if self.unify_log.needs_log(vid) {
199-
self.modified_set.set(&mut self.undo_log, vid);
226+
if self.storage.unify_log.needs_log(vid) {
227+
self.storage.modified_set.set(&mut self.undo_log, vid);
200228
}
201229
self.relations().unify_var_value(vid, value)
202230
}
@@ -212,9 +240,9 @@ where
212240
relations.unify_var_var(a, b)?;
213241

214242
if a == relations.find(a) {
215-
self.unify_log.unify(&mut self.undo_log, a.into(), b.into());
243+
self.storage.unify_log.unify(&mut self.undo_log, a.into(), b.into());
216244
} else {
217-
self.unify_log.unify(&mut self.undo_log, b.into(), a.into());
245+
self.storage.unify_log.unify(&mut self.undo_log, b.into(), a.into());
218246
}
219247
Ok(())
220248
}
@@ -240,14 +268,11 @@ where
240268
self.relations().new_key(value)
241269
}
242270

243-
pub fn len(&self) -> usize {
244-
self.relations.len()
245-
}
246-
247271
pub fn vars_since_snapshot(&mut self, s: &Snapshot<K, I>) -> Range<K> {
248272
K::from(I::new(s.value_count))..K::from(I::new(self.relations().len()))
249273
}
250274

275+
/* FIXME
251276
pub fn snapshot(&mut self) -> Snapshot<K, I> {
252277
self.undo_log.num_open_snapshots += 1;
253278
Snapshot {
@@ -258,44 +283,44 @@ where
258283
}
259284
260285
pub fn rollback_to(&mut self, snapshot: Snapshot<K, I>) {
261-
let Self { relations, unify_log, modified_set, .. } = self;
286+
let UnificationTableStorage { relations, unify_log, modified_set, .. } = self.storage;
262287
263-
self.undo_log
264-
.rollback_to(&mut RollbackView { relations, unify_log, modified_set }, snapshot);
288+
self.undo_log.rollback_to(|| RollbackView { relations, unify_log, modified_set }, snapshot);
265289
266290
if self.undo_log.num_open_snapshots == 0 {
267-
self.modified_set.clear();
291+
self.storage.modified_set.clear();
268292
}
269293
}
270294
271295
pub fn commit(&mut self, snapshot: Snapshot<K, I>) {
272296
self.undo_log.commit(snapshot);
273297
274-
if self.undo_log.num_open_snapshots == 0 {
275-
self.modified_set.clear();
298+
if self.undo_log.num_open_snapshots() == 0 {
299+
self.storage.modified_set.clear();
276300
}
277301
}
302+
*/
278303

279304
pub fn register(&mut self) -> ms::Offset<I> {
280-
self.modified_set.register()
305+
self.storage.modified_set.register()
281306
}
282307

283308
pub fn deregister(&mut self, offset: ms::Offset<I>) {
284-
self.modified_set.deregister(offset);
309+
self.storage.modified_set.deregister(offset);
285310
}
286311

287312
pub fn watch_variable(&mut self, index: I) {
288313
debug_assert!(index == self.relations().find(index).into());
289-
self.unify_log.watch_variable(index)
314+
self.storage.unify_log.watch_variable(index)
290315
}
291316

292317
pub fn unwatch_variable(&mut self, index: I) {
293-
self.unify_log.unwatch_variable(index)
318+
self.storage.unify_log.unwatch_variable(index)
294319
}
295320

296321
pub fn drain_modified_set(&mut self, offset: &ms::Offset<I>, mut f: impl FnMut(I) -> bool) {
297-
let unify_log = &self.unify_log;
298-
self.modified_set.drain(&mut self.undo_log, offset, |vid| {
322+
let unify_log = &self.storage.unify_log;
323+
self.storage.modified_set.drain(&mut self.undo_log, offset, |vid| {
299324
for &unified_vid in unify_log.get(vid) {
300325
f(unified_vid);
301326
}

compiler/rustc_data_structures/src/modified_set.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ use rustc_index::vec::Idx;
55
use ena::undo_log::{Rollback, UndoLogs};
66

77
#[derive(Copy, Clone, Debug)]
8-
pub enum Undo {
8+
enum UndoInner {
99
Add,
1010
Drain { index: usize, offset: usize },
1111
}
1212

13+
#[derive(Copy, Clone, Debug)]
14+
pub struct Undo<I>(UndoInner, PhantomData<I>);
15+
1316
#[derive(Clone, Debug)]
1417
pub struct ModifiedSet<T: Idx> {
1518
modified: Vec<T>,
@@ -27,14 +30,14 @@ impl<T: Idx> ModifiedSet<T> {
2730
Self::default()
2831
}
2932

30-
pub fn set(&mut self, undo_log: &mut impl UndoLogs<Undo>, index: T) {
33+
pub fn set(&mut self, undo_log: &mut impl UndoLogs<Undo<T>>, index: T) {
3134
self.modified.push(index);
32-
undo_log.push(Undo::Add);
35+
undo_log.push(Undo(UndoInner::Add, PhantomData));
3336
}
3437

3538
pub fn drain(
3639
&mut self,
37-
undo_log: &mut impl UndoLogs<Undo>,
40+
undo_log: &mut impl UndoLogs<Undo<T>>,
3841
index: &Offset<T>,
3942
mut f: impl FnMut(T) -> bool,
4043
) {
@@ -43,7 +46,8 @@ impl<T: Idx> ModifiedSet<T> {
4346
for &index in &self.modified[*offset..] {
4447
f(index);
4548
}
46-
undo_log.push(Undo::Drain { index: index.index, offset: *offset });
49+
undo_log
50+
.push(Undo(UndoInner::Drain { index: index.index, offset: *offset }, PhantomData));
4751
*offset = self.modified.len();
4852
}
4953
}
@@ -69,13 +73,13 @@ impl<T: Idx> ModifiedSet<T> {
6973
}
7074
}
7175

72-
impl<I: Idx> Rollback<Undo> for ModifiedSet<I> {
73-
fn reverse(&mut self, undo: Undo) {
74-
match undo {
75-
Undo::Add => {
76+
impl<I: Idx> Rollback<Undo<I>> for ModifiedSet<I> {
77+
fn reverse(&mut self, undo: Undo<I>) {
78+
match undo.0 {
79+
UndoInner::Add => {
7680
self.modified.pop();
7781
}
78-
Undo::Drain { index, offset } => {
82+
UndoInner::Drain { index, offset } => {
7983
if let Some(o) = self.offsets.get_mut(index) {
8084
*o = offset;
8185
}

compiler/rustc_infer/src/infer/combine.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,11 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
226226
fn unify_const_variable(
227227
&self,
228228
param_env: ty::ParamEnv<'tcx>,
229-
target_vid: ty::ConstVid<'tcx>,
229+
target_vid: ty::ConstVid,
230230
ct: &'tcx ty::Const<'tcx>,
231231
vid_is_expected: bool,
232+
vid: ty::ConstVid,
233+
value: &'tcx ty::Const<'tcx>,
232234
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
233235
let (for_universe, span) = {
234236
let mut inner = self.inner.borrow_mut();
@@ -722,10 +724,14 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
722724
if self.for_universe.can_name(universe) {
723725
Ok(c)
724726
} else {
725-
let new_var_id = variable_table.new_key(ConstVarValue {
726-
origin: var_value.origin,
727-
val: ConstVariableValue::Unknown { universe: self.for_universe },
728-
});
727+
let new_var_id = variable_table
728+
.new_key(ConstVarValue {
729+
origin: var_value.origin,
730+
val: ConstVariableValue::Unknown {
731+
universe: self.for_universe,
732+
},
733+
})
734+
.vid;
729735
Ok(self.tcx().mk_const_var(new_var_id, c.ty))
730736
}
731737
}

compiler/rustc_infer/src/infer/freshen.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct TypeFreshener<'a, 'tcx> {
4646
ty_freshen_count: u32,
4747
const_freshen_count: u32,
4848
ty_freshen_map: FxHashMap<ty::InferTy, Ty<'tcx>>,
49-
const_freshen_map: FxHashMap<ty::InferConst<'tcx>, &'tcx ty::Const<'tcx>>,
49+
const_freshen_map: FxHashMap<ty::InferConst, &'tcx ty::Const<'tcx>>,
5050
}
5151

5252
impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
@@ -88,12 +88,12 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
8888
fn freshen_const<F>(
8989
&mut self,
9090
opt_ct: Option<&'tcx ty::Const<'tcx>>,
91-
key: ty::InferConst<'tcx>,
91+
key: ty::InferConst,
9292
freshener: F,
9393
ty: Ty<'tcx>,
9494
) -> &'tcx ty::Const<'tcx>
9595
where
96-
F: FnOnce(u32) -> ty::InferConst<'tcx>,
96+
F: FnOnce(u32) -> ty::InferConst,
9797
{
9898
if let Some(ct) = opt_ct {
9999
return ct.fold_with(self);

0 commit comments

Comments
 (0)