Skip to content

Commit d3fae79

Browse files
authored
Rollup merge of #113251 - chenyukang:yukang-fix-112940-smir, r=oli-obk
Use scoped-tls for SMIR to map between TyCtxt and SMIR datastructures Fixes #112940 r? `@oli-obk`
2 parents 6fb790a + 361df86 commit d3fae79

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3937,6 +3937,7 @@ dependencies = [
39373937
"rustc_hir",
39383938
"rustc_middle",
39393939
"rustc_span",
3940+
"scoped-tls",
39403941
"tracing",
39413942
]
39423943

compiler/rustc_smir/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ rustc_hir = { path = "../rustc_hir" }
88
rustc_middle = { path = "../rustc_middle", optional = true }
99
rustc_span = { path = "../rustc_span", optional = true }
1010
tracing = "0.1"
11+
scoped-tls = "1.0"
1112

1213
[features]
1314
default = [

compiler/rustc_smir/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ pub mod stable_mir;
1919

2020
// Make this module private for now since external users should not call these directly.
2121
mod rustc_smir;
22+
23+
#[macro_use]
24+
extern crate scoped_tls;

compiler/rustc_smir/src/stable_mir/mod.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,28 @@ pub trait Context {
100100
fn rustc_tables(&mut self, f: &mut dyn FnMut(&mut Tables<'_>));
101101
}
102102

103-
thread_local! {
104-
/// A thread local variable that stores a pointer to the tables mapping between TyCtxt
105-
/// datastructures and stable MIR datastructures.
106-
static TLV: Cell<*mut ()> = const { Cell::new(std::ptr::null_mut()) };
107-
}
103+
// A thread local variable that stores a pointer to the tables mapping between TyCtxt
104+
// datastructures and stable MIR datastructures
105+
scoped_thread_local! (static TLV: Cell<*mut ()>);
108106

109107
pub fn run(mut context: impl Context, f: impl FnOnce()) {
110-
assert!(TLV.get().is_null());
108+
assert!(!TLV.is_set());
111109
fn g<'a>(mut context: &mut (dyn Context + 'a), f: impl FnOnce()) {
112-
TLV.set(&mut context as *mut &mut _ as _);
113-
f();
114-
TLV.replace(std::ptr::null_mut());
110+
let ptr: *mut () = &mut context as *mut &mut _ as _;
111+
TLV.set(&Cell::new(ptr), || {
112+
f();
113+
});
115114
}
116115
g(&mut context, f);
117116
}
118117

119118
/// Loads the current context and calls a function with it.
120119
/// Do not nest these, as that will ICE.
121120
pub(crate) fn with<R>(f: impl FnOnce(&mut dyn Context) -> R) -> R {
122-
let ptr = TLV.replace(std::ptr::null_mut()) as *mut &mut dyn Context;
123-
assert!(!ptr.is_null());
124-
let ret = f(unsafe { *ptr });
125-
TLV.set(ptr as _);
126-
ret
121+
assert!(TLV.is_set());
122+
TLV.with(|tlv| {
123+
let ptr = tlv.get();
124+
assert!(!ptr.is_null());
125+
f(unsafe { *(ptr as *mut &mut dyn Context) })
126+
})
127127
}

0 commit comments

Comments
 (0)