Skip to content

Commit d2ddc6b

Browse files
committed
Add Steal::risky_hack_borrow_mut
and remove unused Steal::get_mut
1 parent 90b6588 commit d2ddc6b

File tree

1 file changed

+10
-3
lines changed
  • compiler/rustc_data_structures/src

1 file changed

+10
-3
lines changed

compiler/rustc_data_structures/src/steal.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::stable_hasher::{HashStable, StableHasher};
2-
use crate::sync::{MappedReadGuard, ReadGuard, RwLock};
2+
use crate::sync::{MappedReadGuard, MappedWriteGuard, ReadGuard, RwLock, WriteGuard};
33

44
/// The `Steal` struct is intended to used as the value for a query.
55
/// Specifically, we sometimes have queries (*cough* MIR *cough*)
@@ -40,9 +40,16 @@ impl<T> Steal<T> {
4040
ReadGuard::map(borrow, |opt| opt.as_ref().unwrap())
4141
}
4242

43+
/// An escape hatch for rustc drivers to mutate `Steal` caches.
44+
/// Use at your own risk. This can badly break incremental compilation
45+
/// and anything else that relies on the immutability of query caches.
4346
#[track_caller]
44-
pub fn get_mut(&mut self) -> &mut T {
45-
self.value.get_mut().as_mut().expect("attempt to read from stolen value")
47+
pub fn risky_hack_borrow_mut(&self) -> MappedWriteGuard<'_, T> {
48+
let borrow = self.value.borrow_mut();
49+
if borrow.is_none() {
50+
panic!("attempted to read from stolen value: {}", std::any::type_name::<T>());
51+
}
52+
WriteGuard::map(borrow, |opt| opt.as_mut().unwrap())
4653
}
4754

4855
#[track_caller]

0 commit comments

Comments
 (0)