flux_common/
mir_storage.rs1use std::{cell::RefCell, collections::HashMap, rc::Rc, 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, Rc<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, Rc::new(body_with_facts)).is_none());
41 });
42}
43
44pub unsafe fn retrieve_mir_body<'tcx>(
48 tcx: TyCtxt<'tcx>,
49 def_id: LocalDefId,
50) -> Rc<BodyWithBorrowckFacts<'tcx>> {
51 match unsafe { try_retrieve_mir_body(tcx, def_id) } {
53 Some(body) => body,
54 None => bug!("retrieve_mir_body: panic on {def_id:?}"),
55 }
56}
57
58pub unsafe fn try_retrieve_mir_body<'tcx>(
66 _tcx: TyCtxt<'tcx>,
67 def_id: LocalDefId,
68) -> Option<Rc<BodyWithBorrowckFacts<'tcx>>> {
69 let body_with_facts: Rc<BodyWithBorrowckFacts<'static>> =
70 SHARED_STATE.with(|state| state.borrow().get(&def_id).map(Rc::clone))?;
71 Some(unsafe {
73 std::mem::transmute::<Rc<BodyWithBorrowckFacts<'static>>, Rc<BodyWithBorrowckFacts<'tcx>>>(
74 body_with_facts,
75 )
76 })
77}