flux_common/mir_storage.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! This module allows storing mir bodies with borrowck facts in a thread-local
//! storage. Unfortunately, thread local storage requires all data stored in it
//! to have a `'static` lifetime. Therefore, we transmute the lifetime `'tcx`
//! away when storing the data. To ensure that nothing gets meessed up, we
//! require the client to provide a witness: an instance of type `TyCtxt<'tcx>`
//! that is used to show that the lifetime that the client provided is indeed
//! `'tcx`.
use std::{cell::RefCell, collections::HashMap, thread_local};
use rustc_borrowck::consumers::BodyWithBorrowckFacts;
use rustc_hir::def_id::LocalDefId;
use rustc_middle::ty::TyCtxt;
use crate::bug;
thread_local! {
pub static SHARED_STATE:
RefCell<HashMap<LocalDefId, BodyWithBorrowckFacts<'static>>> =
RefCell::new(HashMap::new());
}
/// # Safety
///
/// See the module level comment.
pub unsafe fn store_mir_body<'tcx>(
_tcx: TyCtxt<'tcx>,
def_id: LocalDefId,
body_with_facts: BodyWithBorrowckFacts<'tcx>,
) {
// SAFETY: See the module level comment.
let body_with_facts: BodyWithBorrowckFacts<'static> = std::mem::transmute(body_with_facts);
SHARED_STATE.with(|state| {
let mut map = state.borrow_mut();
assert!(map.insert(def_id, body_with_facts).is_none());
});
}
/// # Safety
///
/// See the module level comment.
#[expect(clippy::needless_lifetimes, reason = "we want to be very explicit about lifetimes here")]
pub unsafe fn retrieve_mir_body<'tcx>(
_tcx: TyCtxt<'tcx>,
def_id: LocalDefId,
) -> BodyWithBorrowckFacts<'tcx> {
let body_with_facts: BodyWithBorrowckFacts<'static> = SHARED_STATE.with(|state| {
match state.borrow_mut().remove(&def_id) {
Some(body) => body,
None => bug!("retrieve_mir_body: panic on {def_id:?}"),
}
});
// SAFETY: See the module level comment.
std::mem::transmute(body_with_facts)
}