Remove unsafe and Rc

This commit is contained in:
Celina G. Val 2023-10-23 14:21:24 -07:00
parent f613b26cfb
commit 421631a3a1
3 changed files with 15 additions and 22 deletions

View File

@ -18,15 +18,14 @@ use std::cell::RefCell;
use std::fmt::Debug; use std::fmt::Debug;
use std::hash::Hash; use std::hash::Hash;
use std::ops::Index; use std::ops::Index;
use std::rc::Rc;
mod internal; mod internal;
pub unsafe fn stable<'tcx, S: Stable<'tcx>>(item: &S) -> S::T { pub fn stable<'tcx, S: Stable<'tcx>>(item: &S) -> S::T {
with_tables(|tables| item.stable(tables)) with_tables(|tables| item.stable(tables))
} }
pub unsafe fn internal<'tcx, S: RustcInternal<'tcx>>(item: &S) -> S::T { pub fn internal<'tcx, S: RustcInternal<'tcx>>(item: &S) -> S::T {
with_tables(|tables| item.internal(tables)) with_tables(|tables| item.internal(tables))
} }
@ -141,15 +140,12 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
// datastructures and stable MIR datastructures // datastructures and stable MIR datastructures
scoped_thread_local! (static TLV: Cell<*const ()>); scoped_thread_local! (static TLV: Cell<*const ()>);
pub(crate) fn init<'tcx>(tables: TablesWrapper<'tcx>, f: impl FnOnce()) { pub(crate) fn init<'tcx>(tables: &TablesWrapper<'tcx>, f: impl FnOnce()) {
assert!(!TLV.is_set()); assert!(!TLV.is_set());
fn g<'a, 'tcx>(context: &'a TablesWrapper<'tcx>, f: impl FnOnce()) { let ptr: *const () = &tables as *const &_ as _;
let ptr: *const () = &context as *const &_ as _; TLV.set(&Cell::new(ptr), || {
TLV.set(&Cell::new(ptr), || { f();
f(); });
});
}
g(&tables, f);
} }
/// Loads the current context and calls a function with it. /// Loads the current context and calls a function with it.
@ -166,7 +162,7 @@ pub(crate) fn with_tables<'tcx, R>(f: impl FnOnce(&mut Tables<'tcx>) -> R) -> R
} }
pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) { pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
let tables = Rc::new(RefCell::new(Tables { let tables = TablesWrapper(RefCell::new(Tables {
tcx, tcx,
def_ids: IndexMap::default(), def_ids: IndexMap::default(),
alloc_ids: IndexMap::default(), alloc_ids: IndexMap::default(),
@ -174,7 +170,7 @@ pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
types: vec![], types: vec![],
instances: IndexMap::default(), instances: IndexMap::default(),
})); }));
stable_mir::run(TablesWrapper(Rc::clone(&tables)), || init(TablesWrapper(tables), f)); stable_mir::run(&tables, || init(&tables, f));
} }
#[macro_export] #[macro_export]

View File

@ -279,7 +279,7 @@ impl<S, R: PartialEq> PartialEq<R> for MaybeStable<S, R> {
} }
} }
pub(crate) struct TablesWrapper<'tcx>(pub(crate) std::rc::Rc<RefCell<Tables<'tcx>>>); pub(crate) struct TablesWrapper<'tcx>(pub(crate) RefCell<Tables<'tcx>>);
pub struct Tables<'tcx> { pub struct Tables<'tcx> {
pub(crate) tcx: TyCtxt<'tcx>, pub(crate) tcx: TyCtxt<'tcx>,

View File

@ -243,15 +243,12 @@ pub trait Context {
// datastructures and stable MIR datastructures // datastructures and stable MIR datastructures
scoped_thread_local! (static TLV: Cell<*const ()>); scoped_thread_local! (static TLV: Cell<*const ()>);
pub fn run(context: impl Context, f: impl FnOnce()) { pub fn run(context: &dyn Context, f: impl FnOnce()) {
assert!(!TLV.is_set()); assert!(!TLV.is_set());
fn g<'a>(context: &(dyn Context + 'a), f: impl FnOnce()) { let ptr: *const () = &context as *const &_ as _;
let ptr: *const () = &context as *const &_ as _; TLV.set(&Cell::new(ptr), || {
TLV.set(&Cell::new(ptr), || { f();
f(); });
});
}
g(&context, f);
} }
/// Loads the current context and calls a function with it. /// Loads the current context and calls a function with it.