From afd530793454a42951dcc0fe50b1cd66f397c438 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 31 Dec 2022 11:00:41 +0100 Subject: [PATCH 1/3] Move `ty::tls` to seperate file --- compiler/rustc_middle/src/ty/context.rs | 174 +------------------- compiler/rustc_middle/src/ty/context/tls.rs | 169 +++++++++++++++++++ 2 files changed, 171 insertions(+), 172 deletions(-) create mode 100644 compiler/rustc_middle/src/ty/context/tls.rs diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index ce04d8d21f4cd..ed23297dba78c 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2,6 +2,8 @@ #![allow(rustc::usage_of_ty_tykind)] +pub mod tls; + use crate::arena::Arena; use crate::dep_graph::{DepGraph, DepKindStruct}; use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos}; @@ -1188,178 +1190,6 @@ CloneLiftImpls! { for<'tcx> { Constness, traits::WellFormedLoc, ImplPolarity, crate::mir::ReturnConstraint, } } -pub mod tls { - use super::{ptr_eq, GlobalCtxt, TyCtxt}; - - use crate::dep_graph::TaskDepsRef; - use crate::ty::query; - use rustc_data_structures::sync::{self, Lock}; - use rustc_errors::Diagnostic; - use std::mem; - use thin_vec::ThinVec; - - #[cfg(not(parallel_compiler))] - use std::cell::Cell; - - #[cfg(parallel_compiler)] - use rustc_rayon_core as rayon_core; - - /// This is the implicit state of rustc. It contains the current - /// `TyCtxt` and query. It is updated when creating a local interner or - /// executing a new query. Whenever there's a `TyCtxt` value available - /// you should also have access to an `ImplicitCtxt` through the functions - /// in this module. - #[derive(Clone)] - pub struct ImplicitCtxt<'a, 'tcx> { - /// The current `TyCtxt`. - pub tcx: TyCtxt<'tcx>, - - /// The current query job, if any. This is updated by `JobOwner::start` in - /// `ty::query::plumbing` when executing a query. - pub query: Option, - - /// Where to store diagnostics for the current query job, if any. - /// This is updated by `JobOwner::start` in `ty::query::plumbing` when executing a query. - pub diagnostics: Option<&'a Lock>>, - - /// Used to prevent queries from calling too deeply. - pub query_depth: usize, - - /// The current dep graph task. This is used to add dependencies to queries - /// when executing them. - pub task_deps: TaskDepsRef<'a>, - } - - impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> { - pub fn new(gcx: &'tcx GlobalCtxt<'tcx>) -> Self { - let tcx = TyCtxt { gcx }; - ImplicitCtxt { - tcx, - query: None, - diagnostics: None, - query_depth: 0, - task_deps: TaskDepsRef::Ignore, - } - } - } - - /// Sets Rayon's thread-local variable, which is preserved for Rayon jobs - /// to `value` during the call to `f`. It is restored to its previous value after. - /// This is used to set the pointer to the new `ImplicitCtxt`. - #[cfg(parallel_compiler)] - #[inline] - fn set_tlv R, R>(value: usize, f: F) -> R { - rayon_core::tlv::with(value, f) - } - - /// Gets Rayon's thread-local variable, which is preserved for Rayon jobs. - /// This is used to get the pointer to the current `ImplicitCtxt`. - #[cfg(parallel_compiler)] - #[inline] - pub fn get_tlv() -> usize { - rayon_core::tlv::get() - } - - #[cfg(not(parallel_compiler))] - thread_local! { - /// A thread local variable that stores a pointer to the current `ImplicitCtxt`. - static TLV: Cell = const { Cell::new(0) }; - } - - /// Sets TLV to `value` during the call to `f`. - /// It is restored to its previous value after. - /// This is used to set the pointer to the new `ImplicitCtxt`. - #[cfg(not(parallel_compiler))] - #[inline] - fn set_tlv R, R>(value: usize, f: F) -> R { - let old = get_tlv(); - let _reset = rustc_data_structures::OnDrop(move || TLV.with(|tlv| tlv.set(old))); - TLV.with(|tlv| tlv.set(value)); - f() - } - - /// Gets the pointer to the current `ImplicitCtxt`. - #[cfg(not(parallel_compiler))] - #[inline] - fn get_tlv() -> usize { - TLV.with(|tlv| tlv.get()) - } - - /// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`. - #[inline] - pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> R - where - F: FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R, - { - set_tlv(context as *const _ as usize, || f(&context)) - } - - /// Allows access to the current `ImplicitCtxt` in a closure if one is available. - #[inline] - pub fn with_context_opt(f: F) -> R - where - F: for<'a, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'tcx>>) -> R, - { - let context = get_tlv(); - if context == 0 { - f(None) - } else { - // We could get an `ImplicitCtxt` pointer from another thread. - // Ensure that `ImplicitCtxt` is `Sync`. - sync::assert_sync::>(); - - unsafe { f(Some(&*(context as *const ImplicitCtxt<'_, '_>))) } - } - } - - /// Allows access to the current `ImplicitCtxt`. - /// Panics if there is no `ImplicitCtxt` available. - #[inline] - pub fn with_context(f: F) -> R - where - F: for<'a, 'tcx> FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R, - { - with_context_opt(|opt_context| f(opt_context.expect("no ImplicitCtxt stored in tls"))) - } - - /// Allows access to the current `ImplicitCtxt` whose tcx field is the same as the tcx argument - /// passed in. This means the closure is given an `ImplicitCtxt` with the same `'tcx` lifetime - /// as the `TyCtxt` passed in. - /// This will panic if you pass it a `TyCtxt` which is different from the current - /// `ImplicitCtxt`'s `tcx` field. - #[inline] - pub fn with_related_context<'tcx, F, R>(tcx: TyCtxt<'tcx>, f: F) -> R - where - F: FnOnce(&ImplicitCtxt<'_, 'tcx>) -> R, - { - with_context(|context| unsafe { - assert!(ptr_eq(context.tcx.gcx, tcx.gcx)); - let context: &ImplicitCtxt<'_, '_> = mem::transmute(context); - f(context) - }) - } - - /// Allows access to the `TyCtxt` in the current `ImplicitCtxt`. - /// Panics if there is no `ImplicitCtxt` available. - #[inline] - pub fn with(f: F) -> R - where - F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> R, - { - with_context(|context| f(context.tcx)) - } - - /// Allows access to the `TyCtxt` in the current `ImplicitCtxt`. - /// The closure is passed None if there is no `ImplicitCtxt` available. - #[inline] - pub fn with_opt(f: F) -> R - where - F: for<'tcx> FnOnce(Option>) -> R, - { - with_context_opt(|opt_context| f(opt_context.map(|context| context.tcx))) - } -} - macro_rules! sty_debug_print { ($fmt: expr, $ctxt: expr, $($variant: ident),*) => {{ // Curious inner module to allow variant names to be used as diff --git a/compiler/rustc_middle/src/ty/context/tls.rs b/compiler/rustc_middle/src/ty/context/tls.rs new file mode 100644 index 0000000000000..0737131f1794e --- /dev/null +++ b/compiler/rustc_middle/src/ty/context/tls.rs @@ -0,0 +1,169 @@ +use super::{ptr_eq, GlobalCtxt, TyCtxt}; + +use crate::dep_graph::TaskDepsRef; +use crate::ty::query; +use rustc_data_structures::sync::{self, Lock}; +use rustc_errors::Diagnostic; +use std::mem; +use thin_vec::ThinVec; + +#[cfg(not(parallel_compiler))] +use std::cell::Cell; + +#[cfg(parallel_compiler)] +use rustc_rayon_core as rayon_core; + +/// This is the implicit state of rustc. It contains the current +/// `TyCtxt` and query. It is updated when creating a local interner or +/// executing a new query. Whenever there's a `TyCtxt` value available +/// you should also have access to an `ImplicitCtxt` through the functions +/// in this module. +#[derive(Clone)] +pub struct ImplicitCtxt<'a, 'tcx> { + /// The current `TyCtxt`. + pub tcx: TyCtxt<'tcx>, + + /// The current query job, if any. This is updated by `JobOwner::start` in + /// `ty::query::plumbing` when executing a query. + pub query: Option, + + /// Where to store diagnostics for the current query job, if any. + /// This is updated by `JobOwner::start` in `ty::query::plumbing` when executing a query. + pub diagnostics: Option<&'a Lock>>, + + /// Used to prevent queries from calling too deeply. + pub query_depth: usize, + + /// The current dep graph task. This is used to add dependencies to queries + /// when executing them. + pub task_deps: TaskDepsRef<'a>, +} + +impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> { + pub fn new(gcx: &'tcx GlobalCtxt<'tcx>) -> Self { + let tcx = TyCtxt { gcx }; + ImplicitCtxt { + tcx, + query: None, + diagnostics: None, + query_depth: 0, + task_deps: TaskDepsRef::Ignore, + } + } +} + +/// Sets Rayon's thread-local variable, which is preserved for Rayon jobs +/// to `value` during the call to `f`. It is restored to its previous value after. +/// This is used to set the pointer to the new `ImplicitCtxt`. +#[cfg(parallel_compiler)] +#[inline] +fn set_tlv R, R>(value: usize, f: F) -> R { + rayon_core::tlv::with(value, f) +} + +/// Gets Rayon's thread-local variable, which is preserved for Rayon jobs. +/// This is used to get the pointer to the current `ImplicitCtxt`. +#[cfg(parallel_compiler)] +#[inline] +pub fn get_tlv() -> usize { + rayon_core::tlv::get() +} + +#[cfg(not(parallel_compiler))] +thread_local! { + /// A thread local variable that stores a pointer to the current `ImplicitCtxt`. + static TLV: Cell = const { Cell::new(0) }; +} + +/// Sets TLV to `value` during the call to `f`. +/// It is restored to its previous value after. +/// This is used to set the pointer to the new `ImplicitCtxt`. +#[cfg(not(parallel_compiler))] +#[inline] +fn set_tlv R, R>(value: usize, f: F) -> R { + let old = get_tlv(); + let _reset = rustc_data_structures::OnDrop(move || TLV.with(|tlv| tlv.set(old))); + TLV.with(|tlv| tlv.set(value)); + f() +} + +/// Gets the pointer to the current `ImplicitCtxt`. +#[cfg(not(parallel_compiler))] +#[inline] +fn get_tlv() -> usize { + TLV.with(|tlv| tlv.get()) +} + +/// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`. +#[inline] +pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> R +where + F: FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R, +{ + set_tlv(context as *const _ as usize, || f(&context)) +} + +/// Allows access to the current `ImplicitCtxt` in a closure if one is available. +#[inline] +pub fn with_context_opt(f: F) -> R +where + F: for<'a, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'tcx>>) -> R, +{ + let context = get_tlv(); + if context == 0 { + f(None) + } else { + // We could get an `ImplicitCtxt` pointer from another thread. + // Ensure that `ImplicitCtxt` is `Sync`. + sync::assert_sync::>(); + + unsafe { f(Some(&*(context as *const ImplicitCtxt<'_, '_>))) } + } +} + +/// Allows access to the current `ImplicitCtxt`. +/// Panics if there is no `ImplicitCtxt` available. +#[inline] +pub fn with_context(f: F) -> R +where + F: for<'a, 'tcx> FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R, +{ + with_context_opt(|opt_context| f(opt_context.expect("no ImplicitCtxt stored in tls"))) +} + +/// Allows access to the current `ImplicitCtxt` whose tcx field is the same as the tcx argument +/// passed in. This means the closure is given an `ImplicitCtxt` with the same `'tcx` lifetime +/// as the `TyCtxt` passed in. +/// This will panic if you pass it a `TyCtxt` which is different from the current +/// `ImplicitCtxt`'s `tcx` field. +#[inline] +pub fn with_related_context<'tcx, F, R>(tcx: TyCtxt<'tcx>, f: F) -> R +where + F: FnOnce(&ImplicitCtxt<'_, 'tcx>) -> R, +{ + with_context(|context| unsafe { + assert!(ptr_eq(context.tcx.gcx, tcx.gcx)); + let context: &ImplicitCtxt<'_, '_> = mem::transmute(context); + f(context) + }) +} + +/// Allows access to the `TyCtxt` in the current `ImplicitCtxt`. +/// Panics if there is no `ImplicitCtxt` available. +#[inline] +pub fn with(f: F) -> R +where + F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> R, +{ + with_context(|context| f(context.tcx)) +} + +/// Allows access to the `TyCtxt` in the current `ImplicitCtxt`. +/// The closure is passed None if there is no `ImplicitCtxt` available. +#[inline] +pub fn with_opt(f: F) -> R +where + F: for<'tcx> FnOnce(Option>) -> R, +{ + with_context_opt(|opt_context| f(opt_context.map(|context| context.tcx))) +} From 0d11b77005c9304e45db7b1321d8d4b324366e2f Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 31 Dec 2022 11:14:29 +0100 Subject: [PATCH 2/3] Some ty::tls cleanups Putting the cfged functions into a seperate module and giving them better names. --- compiler/rustc_middle/src/ty/context.rs | 6 -- compiler/rustc_middle/src/ty/context/tls.rs | 98 +++++++++++---------- 2 files changed, 53 insertions(+), 51 deletions(-) diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index ed23297dba78c..bae2c863a813d 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2222,12 +2222,6 @@ pub struct DeducedParamAttrs { pub read_only: bool, } -// We are comparing types with different invariant lifetimes, so `ptr::eq` -// won't work for us. -fn ptr_eq(t: *const T, u: *const U) -> bool { - t as *const () == u as *const () -} - pub fn provide(providers: &mut ty::query::Providers) { providers.module_reexports = |tcx, id| tcx.resolutions(()).reexport_map.get(&id).map(|v| &v[..]); diff --git a/compiler/rustc_middle/src/ty/context/tls.rs b/compiler/rustc_middle/src/ty/context/tls.rs index 0737131f1794e..f1fe47f6ba6ed 100644 --- a/compiler/rustc_middle/src/ty/context/tls.rs +++ b/compiler/rustc_middle/src/ty/context/tls.rs @@ -1,18 +1,13 @@ -use super::{ptr_eq, GlobalCtxt, TyCtxt}; +use super::{GlobalCtxt, TyCtxt}; use crate::dep_graph::TaskDepsRef; use crate::ty::query; use rustc_data_structures::sync::{self, Lock}; use rustc_errors::Diagnostic; use std::mem; +use std::ptr; use thin_vec::ThinVec; -#[cfg(not(parallel_compiler))] -use std::cell::Cell; - -#[cfg(parallel_compiler)] -use rustc_rayon_core as rayon_core; - /// This is the implicit state of rustc. It contains the current /// `TyCtxt` and query. It is updated when creating a local interner or /// executing a new query. Whenever there's a `TyCtxt` value available @@ -52,46 +47,53 @@ impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> { } } -/// Sets Rayon's thread-local variable, which is preserved for Rayon jobs -/// to `value` during the call to `f`. It is restored to its previous value after. -/// This is used to set the pointer to the new `ImplicitCtxt`. #[cfg(parallel_compiler)] -#[inline] -fn set_tlv R, R>(value: usize, f: F) -> R { - rayon_core::tlv::with(value, f) -} +mod tlv { + use rustc_rayon_core as rayon_core; + use std::ptr; + + /// Gets Rayon's thread-local variable, which is preserved for Rayon jobs. + /// This is used to get the pointer to the current `ImplicitCtxt`. + #[inline] + pub(super) fn get_tlv() -> usize { + rayon_core::tlv::get() + } -/// Gets Rayon's thread-local variable, which is preserved for Rayon jobs. -/// This is used to get the pointer to the current `ImplicitCtxt`. -#[cfg(parallel_compiler)] -#[inline] -pub fn get_tlv() -> usize { - rayon_core::tlv::get() + /// Sets Rayon's thread-local variable, which is preserved for Rayon jobs + /// to `value` during the call to `f`. It is restored to its previous value after. + /// This is used to set the pointer to the new `ImplicitCtxt`. + #[inline] + pub(super) fn with_tlv R, R>(value: usize, f: F) -> R { + rayon_core::tlv::with(value, f) + } } #[cfg(not(parallel_compiler))] -thread_local! { - /// A thread local variable that stores a pointer to the current `ImplicitCtxt`. - static TLV: Cell = const { Cell::new(0) }; -} +mod tlv { + use std::cell::Cell; + use std::ptr; -/// Sets TLV to `value` during the call to `f`. -/// It is restored to its previous value after. -/// This is used to set the pointer to the new `ImplicitCtxt`. -#[cfg(not(parallel_compiler))] -#[inline] -fn set_tlv R, R>(value: usize, f: F) -> R { - let old = get_tlv(); - let _reset = rustc_data_structures::OnDrop(move || TLV.with(|tlv| tlv.set(old))); - TLV.with(|tlv| tlv.set(value)); - f() -} + thread_local! { + /// A thread local variable that stores a pointer to the current `ImplicitCtxt`. + static TLV: Cell = const { Cell::new(0) }; + } -/// Gets the pointer to the current `ImplicitCtxt`. -#[cfg(not(parallel_compiler))] -#[inline] -fn get_tlv() -> usize { - TLV.with(|tlv| tlv.get()) + /// Gets the pointer to the current `ImplicitCtxt`. + #[inline] + pub(super) fn get_tlv() -> usize { + TLV.with(|tlv| tlv.get()) + } + + /// Sets TLV to `value` during the call to `f`. + /// It is restored to its previous value after. + /// This is used to set the pointer to the new `ImplicitCtxt`. + #[inline] + pub(super) fn with_tlv R, R>(value: usize, f: F) -> R { + let old = get_tlv(); + let _reset = rustc_data_structures::OnDrop(move || TLV.with(|tlv| tlv.set(old))); + TLV.with(|tlv| tlv.set(value)); + f() + } } /// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`. @@ -100,7 +102,7 @@ pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> where F: FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R, { - set_tlv(context as *const _ as usize, || f(&context)) + tlv::with_tlv(context as *const _ as usize, || f(&context)) } /// Allows access to the current `ImplicitCtxt` in a closure if one is available. @@ -109,7 +111,7 @@ pub fn with_context_opt(f: F) -> R where F: for<'a, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'tcx>>) -> R, { - let context = get_tlv(); + let context = tlv::get_tlv(); if context == 0 { f(None) } else { @@ -141,9 +143,15 @@ pub fn with_related_context<'tcx, F, R>(tcx: TyCtxt<'tcx>, f: F) -> R where F: FnOnce(&ImplicitCtxt<'_, 'tcx>) -> R, { - with_context(|context| unsafe { - assert!(ptr_eq(context.tcx.gcx, tcx.gcx)); - let context: &ImplicitCtxt<'_, '_> = mem::transmute(context); + with_context(|context| { + // The two gcx have different invariant lifetimes, so we need to erase them for the comparison. + assert!(ptr::eq( + context.tcx.gcx as *const _ as *const (), + tcx.gcx as *const _ as *const () + )); + + let context: &ImplicitCtxt<'_, '_> = unsafe { mem::transmute(context) }; + f(context) }) } From db305d0ca8b03492877a467d061f0c65eb194b2a Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 31 Dec 2022 11:43:40 +0100 Subject: [PATCH 3/3] Use strict provenance APIs in ty::tls --- compiler/rustc_middle/src/lib.rs | 1 + compiler/rustc_middle/src/ty/context/tls.rs | 30 ++++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_middle/src/lib.rs b/compiler/rustc_middle/src/lib.rs index 7e4063c2ffd78..95148de251824 100644 --- a/compiler/rustc_middle/src/lib.rs +++ b/compiler/rustc_middle/src/lib.rs @@ -43,6 +43,7 @@ #![feature(min_specialization)] #![feature(trusted_len)] #![feature(type_alias_impl_trait)] +#![feature(strict_provenance)] #![feature(associated_type_bounds)] #![feature(rustc_attrs)] #![feature(control_flow_enum)] diff --git a/compiler/rustc_middle/src/ty/context/tls.rs b/compiler/rustc_middle/src/ty/context/tls.rs index f1fe47f6ba6ed..71b025dc1be4b 100644 --- a/compiler/rustc_middle/src/ty/context/tls.rs +++ b/compiler/rustc_middle/src/ty/context/tls.rs @@ -55,16 +55,16 @@ mod tlv { /// Gets Rayon's thread-local variable, which is preserved for Rayon jobs. /// This is used to get the pointer to the current `ImplicitCtxt`. #[inline] - pub(super) fn get_tlv() -> usize { - rayon_core::tlv::get() + pub(super) fn get_tlv() -> *const () { + ptr::from_exposed_addr(rayon_core::tlv::get()) } /// Sets Rayon's thread-local variable, which is preserved for Rayon jobs /// to `value` during the call to `f`. It is restored to its previous value after. /// This is used to set the pointer to the new `ImplicitCtxt`. #[inline] - pub(super) fn with_tlv R, R>(value: usize, f: F) -> R { - rayon_core::tlv::with(value, f) + pub(super) fn with_tlv R, R>(value: *const (), f: F) -> R { + rayon_core::tlv::with(value.expose_addr(), f) } } @@ -75,12 +75,12 @@ mod tlv { thread_local! { /// A thread local variable that stores a pointer to the current `ImplicitCtxt`. - static TLV: Cell = const { Cell::new(0) }; + static TLV: Cell<*const ()> = const { Cell::new(ptr::null()) }; } /// Gets the pointer to the current `ImplicitCtxt`. #[inline] - pub(super) fn get_tlv() -> usize { + pub(super) fn get_tlv() -> *const () { TLV.with(|tlv| tlv.get()) } @@ -88,7 +88,7 @@ mod tlv { /// It is restored to its previous value after. /// This is used to set the pointer to the new `ImplicitCtxt`. #[inline] - pub(super) fn with_tlv R, R>(value: usize, f: F) -> R { + pub(super) fn with_tlv R, R>(value: *const (), f: F) -> R { let old = get_tlv(); let _reset = rustc_data_structures::OnDrop(move || TLV.with(|tlv| tlv.set(old))); TLV.with(|tlv| tlv.set(value)); @@ -96,13 +96,23 @@ mod tlv { } } +#[inline] +fn erase(context: &ImplicitCtxt<'_, '_>) -> *const () { + context as *const _ as *const () +} + +#[inline] +unsafe fn downcast<'a, 'tcx>(context: *const ()) -> &'a ImplicitCtxt<'a, 'tcx> { + &*(context as *const ImplicitCtxt<'a, 'tcx>) +} + /// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`. #[inline] pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> R where F: FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R, { - tlv::with_tlv(context as *const _ as usize, || f(&context)) + tlv::with_tlv(erase(context), || f(&context)) } /// Allows access to the current `ImplicitCtxt` in a closure if one is available. @@ -112,14 +122,14 @@ where F: for<'a, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'tcx>>) -> R, { let context = tlv::get_tlv(); - if context == 0 { + if context.is_null() { f(None) } else { // We could get an `ImplicitCtxt` pointer from another thread. // Ensure that `ImplicitCtxt` is `Sync`. sync::assert_sync::>(); - unsafe { f(Some(&*(context as *const ImplicitCtxt<'_, '_>))) } + unsafe { f(Some(downcast(context))) } } }