Skip to content

Commit 38d5072

Browse files
committed
librustc: De-@mut the undo log in RegionVarBindings
1 parent b84f294 commit 38d5072

File tree

1 file changed

+57
-32
lines changed
  • src/librustc/middle/typeck/infer/region_inference

1 file changed

+57
-32
lines changed

src/librustc/middle/typeck/infer/region_inference/mod.rs

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub struct RegionVarBindings {
103103
// actively snapshotting. The reason for this is that otherwise
104104
// we end up adding entries for things like the lower bound on
105105
// a variable and so forth, which can never be rolled back.
106-
undo_log: ~[UndoLogEntry],
106+
undo_log: RefCell<~[UndoLogEntry]>,
107107

108108
// This contains the results of inference. It begins as an empty
109109
// option and only acquires a value after inference is complete.
@@ -120,36 +120,45 @@ pub fn RegionVarBindings(tcx: ty::ctxt) -> RegionVarBindings {
120120
glbs: RefCell::new(HashMap::new()),
121121
skolemization_count: Cell::new(0),
122122
bound_count: Cell::new(0),
123-
undo_log: ~[]
123+
undo_log: RefCell::new(~[])
124124
}
125125
}
126126

127127
impl RegionVarBindings {
128128
pub fn in_snapshot(&self) -> bool {
129-
self.undo_log.len() > 0
129+
let undo_log = self.undo_log.borrow();
130+
undo_log.get().len() > 0
130131
}
131132

132133
pub fn start_snapshot(&mut self) -> uint {
133-
debug!("RegionVarBindings: snapshot()={}", self.undo_log.len());
134+
debug!("RegionVarBindings: start_snapshot()");
134135
if self.in_snapshot() {
135-
self.undo_log.len()
136+
{
137+
let undo_log = self.undo_log.borrow();
138+
undo_log.get().len()
139+
}
136140
} else {
137-
self.undo_log.push(Snapshot);
138-
0
141+
{
142+
let mut undo_log = self.undo_log.borrow_mut();
143+
undo_log.get().push(Snapshot);
144+
0
145+
}
139146
}
140147
}
141148

142149
pub fn commit(&mut self) {
143150
debug!("RegionVarBindings: commit()");
144-
while self.undo_log.len() > 0 {
145-
self.undo_log.pop();
151+
let mut undo_log = self.undo_log.borrow_mut();
152+
while undo_log.get().len() > 0 {
153+
undo_log.get().pop();
146154
}
147155
}
148156

149157
pub fn rollback_to(&mut self, snapshot: uint) {
150158
debug!("RegionVarBindings: rollback_to({})", snapshot);
151-
while self.undo_log.len() > snapshot {
152-
let undo_item = self.undo_log.pop();
159+
let mut undo_log = self.undo_log.borrow_mut();
160+
while undo_log.get().len() > snapshot {
161+
let undo_item = undo_log.get().pop();
153162
debug!("undo_item={:?}", undo_item);
154163
match undo_item {
155164
Snapshot => {}
@@ -182,7 +191,10 @@ impl RegionVarBindings {
182191
self.var_origins.push(origin);
183192
let vid = RegionVid { id: id };
184193
if self.in_snapshot() {
185-
self.undo_log.push(AddVar(vid));
194+
{
195+
let mut undo_log = self.undo_log.borrow_mut();
196+
undo_log.get().push(AddVar(vid));
197+
}
186198
}
187199
debug!("created new region variable {:?} with origin {:?}",
188200
vid, origin.repr(self.tcx));
@@ -235,7 +247,10 @@ impl RegionVarBindings {
235247
let mut constraints = self.constraints.borrow_mut();
236248
if constraints.get().insert(constraint, origin) {
237249
if self.in_snapshot() {
238-
self.undo_log.push(AddConstraint(constraint));
250+
{
251+
let mut undo_log = self.undo_log.borrow_mut();
252+
undo_log.get().push(AddConstraint(constraint));
253+
}
239254
}
240255
}
241256
}
@@ -380,7 +395,10 @@ impl RegionVarBindings {
380395
map.get().insert(vars, c);
381396
}
382397
if self.in_snapshot() {
383-
self.undo_log.push(AddCombination(t, vars));
398+
{
399+
let mut undo_log = self.undo_log.borrow_mut();
400+
undo_log.get().push(AddCombination(t, vars));
401+
}
384402
}
385403
relate(self, a, ReInfer(ReVar(c)));
386404
relate(self, b, ReInfer(ReVar(c)));
@@ -390,7 +408,8 @@ impl RegionVarBindings {
390408

391409
pub fn vars_created_since_snapshot(&mut self, snapshot: uint)
392410
-> ~[RegionVid] {
393-
self.undo_log.slice_from(snapshot).iter()
411+
let undo_log = self.undo_log.borrow();
412+
undo_log.get().slice_from(snapshot).iter()
394413
.filter_map(|&elt| match elt {
395414
AddVar(vid) => Some(vid),
396415
_ => None
@@ -410,7 +429,10 @@ impl RegionVarBindings {
410429
debug!("tainted(snapshot={}, r0={:?})", snapshot, r0);
411430
let _indenter = indenter();
412431

413-
let undo_len = self.undo_log.len();
432+
let undo_len = {
433+
let undo_log = self.undo_log.borrow();
434+
undo_log.get().len()
435+
};
414436

415437
// `result_set` acts as a worklist: we explore all outgoing
416438
// edges and add any new regions we find to result_set. This
@@ -426,22 +448,25 @@ impl RegionVarBindings {
426448
let mut undo_index = snapshot;
427449
while undo_index < undo_len {
428450
// nb: can't use uint::range() here as we move result_set
429-
let regs = match self.undo_log[undo_index] {
430-
AddConstraint(ConstrainVarSubVar(ref a, ref b)) => {
431-
Some((ReInfer(ReVar(*a)),
432-
ReInfer(ReVar(*b))))
433-
}
434-
AddConstraint(ConstrainRegSubVar(ref a, ref b)) => {
435-
Some((*a, ReInfer(ReVar(*b))))
436-
}
437-
AddConstraint(ConstrainVarSubReg(ref a, ref b)) => {
438-
Some((ReInfer(ReVar(*a)), *b))
439-
}
440-
AddConstraint(ConstrainRegSubReg(a, b)) => {
441-
Some((a, b))
442-
}
443-
_ => {
444-
None
451+
let regs = {
452+
let undo_log = self.undo_log.borrow();
453+
match undo_log.get()[undo_index] {
454+
AddConstraint(ConstrainVarSubVar(ref a, ref b)) => {
455+
Some((ReInfer(ReVar(*a)),
456+
ReInfer(ReVar(*b))))
457+
}
458+
AddConstraint(ConstrainRegSubVar(ref a, ref b)) => {
459+
Some((*a, ReInfer(ReVar(*b))))
460+
}
461+
AddConstraint(ConstrainVarSubReg(ref a, ref b)) => {
462+
Some((ReInfer(ReVar(*a)), *b))
463+
}
464+
AddConstraint(ConstrainRegSubReg(a, b)) => {
465+
Some((a, b))
466+
}
467+
_ => {
468+
None
469+
}
445470
}
446471
};
447472

0 commit comments

Comments
 (0)