flux_common/
mir_storage.rs1use std::{cell::RefCell, collections::HashMap, thread_local};
14
15use rustc_borrowck::consumers::BodyWithBorrowckFacts;
16use rustc_hir::def_id::LocalDefId;
17use rustc_middle::ty::TyCtxt;
18
19use crate::bug;
20
21thread_local! {
22 pub static SHARED_STATE:
23 RefCell<HashMap<LocalDefId, BodyWithBorrowckFacts<'static>>> =
24 RefCell::new(HashMap::new());
25}
26
27pub unsafe fn store_mir_body<'tcx>(
31 _tcx: TyCtxt<'tcx>,
32 def_id: LocalDefId,
33 body_with_facts: BodyWithBorrowckFacts<'tcx>,
34) {
35 let body_with_facts: BodyWithBorrowckFacts<'static> =
37 unsafe { std::mem::transmute(body_with_facts) };
38 SHARED_STATE.with(|state| {
39 let mut map = state.borrow_mut();
40 assert!(map.insert(def_id, body_with_facts).is_none());
41 });
42}
43
44pub unsafe fn retrieve_mir_body<'tcx>(
48 _tcx: TyCtxt<'tcx>,
49 def_id: LocalDefId,
50) -> BodyWithBorrowckFacts<'tcx> {
51 let body_with_facts: BodyWithBorrowckFacts<'static> = SHARED_STATE.with(|state| {
52 let mut map = state.borrow_mut();
53 match map.remove(&def_id) {
54 Some(body) => body,
55 None => {
56 bug!("retrieve_mir_body: panic on {def_id:?}")
57 }
58 }
59 });
60 unsafe { std::mem::transmute(body_with_facts) }
62}