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
44#[expect(clippy::needless_lifetimes, reason = "we want to be very explicit about lifetimes here")]
48pub unsafe fn retrieve_mir_body<'tcx>(
49 _tcx: TyCtxt<'tcx>,
50 def_id: LocalDefId,
51) -> BodyWithBorrowckFacts<'tcx> {
52 let body_with_facts: BodyWithBorrowckFacts<'static> = SHARED_STATE.with(|state| {
53 match state.borrow_mut().remove(&def_id) {
54 Some(body) => body,
55 None => bug!("retrieve_mir_body: panic on {def_id:?}"),
56 }
57 });
58 unsafe { std::mem::transmute(body_with_facts) }
60}