Skip to content

Commit db305d0

Browse files
committed
Use strict provenance APIs in ty::tls
1 parent 0d11b77 commit db305d0

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#![feature(min_specialization)]
4444
#![feature(trusted_len)]
4545
#![feature(type_alias_impl_trait)]
46+
#![feature(strict_provenance)]
4647
#![feature(associated_type_bounds)]
4748
#![feature(rustc_attrs)]
4849
#![feature(control_flow_enum)]

compiler/rustc_middle/src/ty/context/tls.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ mod tlv {
5555
/// Gets Rayon's thread-local variable, which is preserved for Rayon jobs.
5656
/// This is used to get the pointer to the current `ImplicitCtxt`.
5757
#[inline]
58-
pub(super) fn get_tlv() -> usize {
59-
rayon_core::tlv::get()
58+
pub(super) fn get_tlv() -> *const () {
59+
ptr::from_exposed_addr(rayon_core::tlv::get())
6060
}
6161

6262
/// Sets Rayon's thread-local variable, which is preserved for Rayon jobs
6363
/// to `value` during the call to `f`. It is restored to its previous value after.
6464
/// This is used to set the pointer to the new `ImplicitCtxt`.
6565
#[inline]
66-
pub(super) fn with_tlv<F: FnOnce() -> R, R>(value: usize, f: F) -> R {
67-
rayon_core::tlv::with(value, f)
66+
pub(super) fn with_tlv<F: FnOnce() -> R, R>(value: *const (), f: F) -> R {
67+
rayon_core::tlv::with(value.expose_addr(), f)
6868
}
6969
}
7070

@@ -75,34 +75,44 @@ mod tlv {
7575

7676
thread_local! {
7777
/// A thread local variable that stores a pointer to the current `ImplicitCtxt`.
78-
static TLV: Cell<usize> = const { Cell::new(0) };
78+
static TLV: Cell<*const ()> = const { Cell::new(ptr::null()) };
7979
}
8080

8181
/// Gets the pointer to the current `ImplicitCtxt`.
8282
#[inline]
83-
pub(super) fn get_tlv() -> usize {
83+
pub(super) fn get_tlv() -> *const () {
8484
TLV.with(|tlv| tlv.get())
8585
}
8686

8787
/// Sets TLV to `value` during the call to `f`.
8888
/// It is restored to its previous value after.
8989
/// This is used to set the pointer to the new `ImplicitCtxt`.
9090
#[inline]
91-
pub(super) fn with_tlv<F: FnOnce() -> R, R>(value: usize, f: F) -> R {
91+
pub(super) fn with_tlv<F: FnOnce() -> R, R>(value: *const (), f: F) -> R {
9292
let old = get_tlv();
9393
let _reset = rustc_data_structures::OnDrop(move || TLV.with(|tlv| tlv.set(old)));
9494
TLV.with(|tlv| tlv.set(value));
9595
f()
9696
}
9797
}
9898

99+
#[inline]
100+
fn erase(context: &ImplicitCtxt<'_, '_>) -> *const () {
101+
context as *const _ as *const ()
102+
}
103+
104+
#[inline]
105+
unsafe fn downcast<'a, 'tcx>(context: *const ()) -> &'a ImplicitCtxt<'a, 'tcx> {
106+
&*(context as *const ImplicitCtxt<'a, 'tcx>)
107+
}
108+
99109
/// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`.
100110
#[inline]
101111
pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> R
102112
where
103113
F: FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R,
104114
{
105-
tlv::with_tlv(context as *const _ as usize, || f(&context))
115+
tlv::with_tlv(erase(context), || f(&context))
106116
}
107117

108118
/// Allows access to the current `ImplicitCtxt` in a closure if one is available.
@@ -112,14 +122,14 @@ where
112122
F: for<'a, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'tcx>>) -> R,
113123
{
114124
let context = tlv::get_tlv();
115-
if context == 0 {
125+
if context.is_null() {
116126
f(None)
117127
} else {
118128
// We could get an `ImplicitCtxt` pointer from another thread.
119129
// Ensure that `ImplicitCtxt` is `Sync`.
120130
sync::assert_sync::<ImplicitCtxt<'_, '_>>();
121131

122-
unsafe { f(Some(&*(context as *const ImplicitCtxt<'_, '_>))) }
132+
unsafe { f(Some(downcast(context))) }
123133
}
124134
}
125135

0 commit comments

Comments
 (0)