Skip to content

Commit 421631a

Browse files
committed
Remove unsafe and Rc
1 parent f613b26 commit 421631a

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ use std::cell::RefCell;
1818
use std::fmt::Debug;
1919
use std::hash::Hash;
2020
use std::ops::Index;
21-
use std::rc::Rc;
2221

2322
mod internal;
2423

25-
pub unsafe fn stable<'tcx, S: Stable<'tcx>>(item: &S) -> S::T {
24+
pub fn stable<'tcx, S: Stable<'tcx>>(item: &S) -> S::T {
2625
with_tables(|tables| item.stable(tables))
2726
}
2827

29-
pub unsafe fn internal<'tcx, S: RustcInternal<'tcx>>(item: &S) -> S::T {
28+
pub fn internal<'tcx, S: RustcInternal<'tcx>>(item: &S) -> S::T {
3029
with_tables(|tables| item.internal(tables))
3130
}
3231

@@ -141,15 +140,12 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
141140
// datastructures and stable MIR datastructures
142141
scoped_thread_local! (static TLV: Cell<*const ()>);
143142

144-
pub(crate) fn init<'tcx>(tables: TablesWrapper<'tcx>, f: impl FnOnce()) {
143+
pub(crate) fn init<'tcx>(tables: &TablesWrapper<'tcx>, f: impl FnOnce()) {
145144
assert!(!TLV.is_set());
146-
fn g<'a, 'tcx>(context: &'a TablesWrapper<'tcx>, f: impl FnOnce()) {
147-
let ptr: *const () = &context as *const &_ as _;
148-
TLV.set(&Cell::new(ptr), || {
149-
f();
150-
});
151-
}
152-
g(&tables, f);
145+
let ptr: *const () = &tables as *const &_ as _;
146+
TLV.set(&Cell::new(ptr), || {
147+
f();
148+
});
153149
}
154150

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

168164
pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
169-
let tables = Rc::new(RefCell::new(Tables {
165+
let tables = TablesWrapper(RefCell::new(Tables {
170166
tcx,
171167
def_ids: IndexMap::default(),
172168
alloc_ids: IndexMap::default(),
173169
spans: IndexMap::default(),
174170
types: vec![],
175171
instances: IndexMap::default(),
176172
}));
177-
stable_mir::run(TablesWrapper(Rc::clone(&tables)), || init(TablesWrapper(tables), f));
173+
stable_mir::run(&tables, || init(&tables, f));
178174
}
179175

180176
#[macro_export]

compiler/rustc_smir/src/rustc_smir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ impl<S, R: PartialEq> PartialEq<R> for MaybeStable<S, R> {
279279
}
280280
}
281281

282-
pub(crate) struct TablesWrapper<'tcx>(pub(crate) std::rc::Rc<RefCell<Tables<'tcx>>>);
282+
pub(crate) struct TablesWrapper<'tcx>(pub(crate) RefCell<Tables<'tcx>>);
283283

284284
pub struct Tables<'tcx> {
285285
pub(crate) tcx: TyCtxt<'tcx>,

compiler/stable_mir/src/lib.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,12 @@ pub trait Context {
243243
// datastructures and stable MIR datastructures
244244
scoped_thread_local! (static TLV: Cell<*const ()>);
245245

246-
pub fn run(context: impl Context, f: impl FnOnce()) {
246+
pub fn run(context: &dyn Context, f: impl FnOnce()) {
247247
assert!(!TLV.is_set());
248-
fn g<'a>(context: &(dyn Context + 'a), f: impl FnOnce()) {
249-
let ptr: *const () = &context as *const &_ as _;
250-
TLV.set(&Cell::new(ptr), || {
251-
f();
252-
});
253-
}
254-
g(&context, f);
248+
let ptr: *const () = &context as *const &_ as _;
249+
TLV.set(&Cell::new(ptr), || {
250+
f();
251+
});
255252
}
256253

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

0 commit comments

Comments
 (0)