diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index 5665bb866d46c..eb8f6a1175a90 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -102,14 +102,28 @@ impl<'tcx> TyCtxt<'tcx> { } } -/// Helper for `TyCtxtEnsure` to avoid a closure. -#[inline(always)] -fn noop(_: &T) {} - -/// Helper to ensure that queries only return `Copy` types. -#[inline(always)] -fn copy(x: &T) -> T { - *x +fn evaluate_query<'tcx, Cache>( + tcx: TyCtxt<'tcx>, + execute_query: fn( + &'tcx dyn QueryEngine<'tcx>, + TyCtxt<'tcx>, + Span, + Cache::Key, + QueryMode, + ) -> Option, + query_cache: &Cache, + span: Span, + key: Cache::Key, + mode: QueryMode, +) -> Option +where + Cache::Stored: Copy, + Cache: QueryCache, +{ + match try_get_cached(tcx, query_cache, &key, mode) { + Ok(value) => value, + Err(()) => execute_query(tcx.queries, tcx, span, key, mode), + } } macro_rules! query_helper_param_ty { @@ -220,14 +234,7 @@ macro_rules! define_callbacks { let key = key.into_query_param(); opt_remap_env_constness!([$($modifiers)*][key]); - let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, noop); - - match cached { - Ok(()) => return, - Err(()) => (), - } - - self.tcx.queries.$name(self.tcx, DUMMY_SP, key, QueryMode::Ensure); + let _ = evaluate_query(self.tcx, QueryEngine::$name, &self.tcx.query_caches.$name, DUMMY_SP, key, QueryMode::Ensure); })* } @@ -249,14 +256,7 @@ macro_rules! define_callbacks { let key = key.into_query_param(); opt_remap_env_constness!([$($modifiers)*][key]); - let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, copy); - - match cached { - Ok(value) => return value, - Err(()) => (), - } - - self.tcx.queries.$name(self.tcx, self.span, key, QueryMode::Get).unwrap() + evaluate_query(self.tcx, QueryEngine::$name, &self.tcx.query_caches.$name, self.span, key, QueryMode::Get).unwrap() })* } diff --git a/compiler/rustc_query_system/src/query/plumbing.rs b/compiler/rustc_query_system/src/query/plumbing.rs index 7bbc22e8293a5..891558e314b87 100644 --- a/compiler/rustc_query_system/src/query/plumbing.rs +++ b/compiler/rustc_query_system/src/query/plumbing.rs @@ -303,24 +303,23 @@ where /// which will be used if the query is not in the cache and we need /// to compute it. #[inline] -pub fn try_get_cached<'a, CTX, C, R, OnHit>( +pub fn try_get_cached<'a, CTX, C>( tcx: CTX, cache: &'a C, key: &C::Key, - // `on_hit` can be called while holding a lock to the query cache - on_hit: OnHit, -) -> Result + mode: QueryMode, +) -> Result, ()> where C: QueryCache, + C::Stored: Copy, CTX: DepContext, - OnHit: FnOnce(&C::Stored) -> R, { cache.lookup(&key, |value, index| { if std::intrinsics::unlikely(tcx.profiler().enabled()) { tcx.profiler().query_cache_hit(index.into()); } tcx.dep_graph().read_index(index); - on_hit(value) + if matches!(mode, QueryMode::Ensure) { None } else { Some(*value) } }) } @@ -676,7 +675,7 @@ where } } -#[derive(Debug)] +#[derive(Copy, Clone, Debug)] pub enum QueryMode { Get, Ensure,