Skip to content

Commit 5d128cd

Browse files
committed
Make Steal thread-safe
1 parent 962a53d commit 5d128cd

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/librustc/ty/steal.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::cell::{Ref, RefCell};
11+
use rustc_data_structures::sync::{RwLock, ReadGuard};
1212
use std::mem;
1313

1414
/// The `Steal` struct is intended to used as the value for a query.
@@ -32,25 +32,25 @@ use std::mem;
3232
///
3333
/// FIXME(#41710) -- what is the best way to model linear queries?
3434
pub struct Steal<T> {
35-
value: RefCell<Option<T>>
35+
value: RwLock<Option<T>>
3636
}
3737

3838
impl<T> Steal<T> {
3939
pub fn new(value: T) -> Self {
4040
Steal {
41-
value: RefCell::new(Some(value))
41+
value: RwLock::new(Some(value))
4242
}
4343
}
4444

45-
pub fn borrow(&self) -> Ref<T> {
46-
Ref::map(self.value.borrow(), |opt| match *opt {
45+
pub fn borrow(&self) -> ReadGuard<T> {
46+
ReadGuard::map(self.value.borrow(), |opt| match *opt {
4747
None => bug!("attempted to read from stolen value"),
4848
Some(ref v) => v
4949
})
5050
}
5151

5252
pub fn steal(&self) -> T {
53-
let value_ref = &mut *self.value.borrow_mut();
53+
let value_ref = &mut *self.value.try_write().expect("stealing value which is locked");
5454
let value = mem::replace(value_ref, None);
5555
value.expect("attempt to read from stolen value")
5656
}

src/librustc_borrowck/borrowck/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, owner_def_id: DefId)
128128
// Note that `mir_validated` is a "stealable" result; the
129129
// thief, `optimized_mir()`, forces borrowck, so we know that
130130
// is not yet stolen.
131-
tcx.mir_validated(owner_def_id).borrow();
131+
ty::maps::queries::mir_validated::ensure(tcx, owner_def_id);
132132

133133
// option dance because you can't capture an uninitialized variable
134134
// by mut-ref.

0 commit comments

Comments
 (0)