Skip to content

Commit

Permalink
Remove unsafe and Rc
Browse files Browse the repository at this point in the history
  • Loading branch information
celinval committed Oct 23, 2023
1 parent f613b26 commit 421631a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
22 changes: 9 additions & 13 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ use std::cell::RefCell;
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::Index;
use std::rc::Rc;

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))
}

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))
}

Expand Down Expand Up @@ -141,15 +140,12 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
// datastructures and stable MIR datastructures
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());
fn g<'a, 'tcx>(context: &'a TablesWrapper<'tcx>, f: impl FnOnce()) {
let ptr: *const () = &context as *const &_ as _;
TLV.set(&Cell::new(ptr), || {
f();
});
}
g(&tables, f);
let ptr: *const () = &tables as *const &_ as _;
TLV.set(&Cell::new(ptr), || {
f();
});
}

/// Loads the current context and calls a function with it.
Expand All @@ -166,15 +162,15 @@ pub(crate) fn with_tables<'tcx, R>(f: impl FnOnce(&mut Tables<'tcx>) -> R) -> R
}

pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
let tables = Rc::new(RefCell::new(Tables {
let tables = TablesWrapper(RefCell::new(Tables {
tcx,
def_ids: IndexMap::default(),
alloc_ids: IndexMap::default(),
spans: IndexMap::default(),
types: vec![],
instances: IndexMap::default(),
}));
stable_mir::run(TablesWrapper(Rc::clone(&tables)), || init(TablesWrapper(tables), f));
stable_mir::run(&tables, || init(&tables, f));
}

#[macro_export]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(crate) tcx: TyCtxt<'tcx>,
Expand Down
13 changes: 5 additions & 8 deletions compiler/stable_mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,12 @@ pub trait Context {
// datastructures and stable MIR datastructures
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());
fn g<'a>(context: &(dyn Context + 'a), f: impl FnOnce()) {
let ptr: *const () = &context as *const &_ as _;
TLV.set(&Cell::new(ptr), || {
f();
});
}
g(&context, f);
let ptr: *const () = &context as *const &_ as _;
TLV.set(&Cell::new(ptr), || {
f();
});
}

/// Loads the current context and calls a function with it.
Expand Down

0 comments on commit 421631a

Please sign in to comment.