1 //! The implementation of the query system itself. This defines the macros that
2 //! generate the actual methods on tcx which find and execute the provider,
3 //! manage the caches, and so forth.
6 use crate::{on_disk_cache, Queries};
7 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
8 use rustc_data_structures::sync::Lock;
9 use rustc_errors::{Diagnostic, Handler};
10 use rustc_middle::dep_graph::{
11 self, DepKind, DepKindStruct, DepNode, DepNodeIndex, SerializedDepNodeIndex,
13 use rustc_middle::ty::tls::{self, ImplicitCtxt};
14 use rustc_middle::ty::{self, TyCtxt};
15 use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext};
16 use rustc_query_system::ich::StableHashingContext;
17 use rustc_query_system::query::{
18 force_query, QueryConfig, QueryContext, QueryDescription, QueryJobId, QueryMap,
19 QuerySideEffects, QueryStackFrame,
22 use std::num::NonZeroU64;
23 use thin_vec::ThinVec;
25 #[derive(Copy, Clone)]
26 pub struct QueryCtxt<'tcx> {
27 pub tcx: TyCtxt<'tcx>,
28 pub queries: &'tcx Queries<'tcx>,
31 impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
32 type Target = TyCtxt<'tcx>;
35 fn deref(&self) -> &Self::Target {
40 impl<'tcx> HasDepContext for QueryCtxt<'tcx> {
41 type DepKind = rustc_middle::dep_graph::DepKind;
42 type DepContext = TyCtxt<'tcx>;
45 fn dep_context(&self) -> &Self::DepContext {
50 impl QueryContext for QueryCtxt<'_> {
51 fn next_job_id(&self) -> QueryJobId {
54 self.queries.jobs.fetch_add(1, rustc_data_structures::sync::Ordering::Relaxed),
60 fn current_query_job(&self) -> Option<QueryJobId> {
61 tls::with_related_context(**self, |icx| icx.query)
64 fn try_collect_active_jobs(&self) -> Option<QueryMap> {
65 self.queries.try_collect_active_jobs(**self)
68 // Interactions with on_disk_cache
69 fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects {
73 .map(|c| c.load_side_effects(**self, prev_dep_node_index))
77 fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
78 if let Some(c) = self.queries.on_disk_cache.as_ref() {
79 c.store_side_effects(dep_node_index, side_effects)
83 fn store_side_effects_for_anon_node(
85 dep_node_index: DepNodeIndex,
86 side_effects: QuerySideEffects,
88 if let Some(c) = self.queries.on_disk_cache.as_ref() {
89 c.store_side_effects_for_anon_node(dep_node_index, side_effects)
93 /// Executes a job by changing the `ImplicitCtxt` to point to the
94 /// new query job while it executes. It returns the diagnostics
95 /// captured during execution and the actual result.
101 diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
102 compute: impl FnOnce() -> R,
104 // The `TyCtxt` stored in TLS has the same global interner lifetime
105 // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
106 // when accessing the `ImplicitCtxt`.
107 tls::with_related_context(**self, move |current_icx| {
108 if depth_limit && !self.recursion_limit().value_within_limit(current_icx.query_depth) {
109 self.depth_limit_error();
112 // Update the `ImplicitCtxt` to point to our new query job.
113 let new_icx = ImplicitCtxt {
117 query_depth: current_icx.query_depth + depth_limit as usize,
118 task_deps: current_icx.task_deps,
121 // Use the `ImplicitCtxt` while we execute the query.
122 tls::enter_context(&new_icx, |_| {
123 rustc_data_structures::stack::ensure_sufficient_stack(compute)
129 impl<'tcx> QueryCtxt<'tcx> {
131 pub fn from_tcx(tcx: TyCtxt<'tcx>) -> Self {
132 let queries = tcx.queries.as_any();
133 let queries = unsafe {
134 let queries = std::mem::transmute::<&dyn Any, &dyn Any>(queries);
135 let queries = queries.downcast_ref().unwrap();
136 let queries = std::mem::transmute::<&Queries<'_>, &Queries<'_>>(queries);
139 QueryCtxt { tcx, queries }
142 pub(crate) fn on_disk_cache(self) -> Option<&'tcx on_disk_cache::OnDiskCache<'tcx>> {
143 self.queries.on_disk_cache.as_ref()
146 pub(super) fn encode_query_results(
148 encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx>,
149 query_result_index: &mut on_disk_cache::EncodedDepNodeIndex,
151 macro_rules! encode_queries {
152 ($($query:ident,)*) => {
154 on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>(
163 rustc_cached_queries!(encode_queries!);
166 pub fn try_print_query_stack(
168 query: Option<QueryJobId>,
170 num_frames: Option<usize>,
172 rustc_query_system::query::print_query_stack(self, query, handler, num_frames)
176 macro_rules! handle_cycle_error {
177 ([][$tcx: expr, $error:expr]) => {{
179 Value::from_cycle_error($tcx)
181 ([(fatal_cycle) $($rest:tt)*][$tcx:expr, $error:expr]) => {{
183 $tcx.sess.abort_if_errors();
186 ([(cycle_delay_bug) $($rest:tt)*][$tcx:expr, $error:expr]) => {{
187 $error.delay_as_bug();
188 Value::from_cycle_error($tcx)
190 ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
191 handle_cycle_error!([$($modifiers)*][$($args)*])
195 macro_rules! is_anon {
199 ([(anon) $($rest:tt)*]) => {{
202 ([$other:tt $($modifiers:tt)*]) => {
203 is_anon!([$($modifiers)*])
207 macro_rules! is_eval_always {
211 ([(eval_always) $($rest:tt)*]) => {{
214 ([$other:tt $($modifiers:tt)*]) => {
215 is_eval_always!([$($modifiers)*])
219 macro_rules! depth_limit {
223 ([(depth_limit) $($rest:tt)*]) => {{
226 ([$other:tt $($modifiers:tt)*]) => {
227 depth_limit!([$($modifiers)*])
231 macro_rules! hash_result {
233 Some(dep_graph::hash_result)
235 ([(no_hash) $($rest:tt)*]) => {{
238 ([$other:tt $($modifiers:tt)*]) => {
239 hash_result!([$($modifiers)*])
243 macro_rules! get_provider {
244 ([][$tcx:expr, $name:ident, $key:expr]) => {{
245 $tcx.queries.local_providers.$name
247 ([(separate_provide_extern) $($rest:tt)*][$tcx:expr, $name:ident, $key:expr]) => {{
248 if $key.query_crate_is_local() {
249 $tcx.queries.local_providers.$name
251 $tcx.queries.extern_providers.$name
254 ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
255 get_provider!([$($modifiers)*][$($args)*])
259 pub(crate) fn create_query_frame<
261 K: Copy + Key + for<'a> HashStable<StableHashingContext<'a>>,
263 tcx: QueryCtxt<'tcx>,
264 do_describe: fn(QueryCtxt<'tcx>, K) -> String,
268 ) -> QueryStackFrame {
269 // Disable visible paths printing for performance reasons.
270 // Showing visible path instead of any path is not that important in production.
271 let description = ty::print::with_no_visible_paths!(
272 // Force filename-line mode to avoid invoking `type_of` query.
273 ty::print::with_forced_impl_filename_line!(do_describe(tcx, key))
276 if tcx.sess.verbose() { format!("{} [{}]", description, name) } else { description };
277 let span = if kind == dep_graph::DepKind::def_span {
278 // The `def_span` query is used to calculate `default_span`,
279 // so exit to avoid infinite recursion.
282 Some(key.default_span(*tcx))
284 let def_kind = if kind == dep_graph::DepKind::opt_def_kind {
285 // Try to avoid infinite recursion.
289 .and_then(|def_id| def_id.as_local())
290 .and_then(|def_id| tcx.opt_def_kind(def_id))
293 tcx.with_stable_hashing_context(|mut hcx| {
294 let mut hasher = StableHasher::new();
295 std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
296 key.hash_stable(&mut hcx, &mut hasher);
297 hasher.finish::<u64>()
301 QueryStackFrame::new(name, description, span, def_kind, hash)
304 pub(crate) fn try_load_from_on_disk_cache<'tcx, K: DepNodeParams<TyCtxt<'tcx>>, V>(
307 cache_on_disk: fn(TyCtxt<'tcx>, &K) -> bool,
308 cache_query_deps: fn(TyCtxt<'tcx>, K) -> V,
310 debug_assert!(tcx.dep_graph.is_green(&dep_node));
312 let key = K::recover(tcx, &dep_node).unwrap_or_else(|| {
313 panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
315 if cache_on_disk(tcx, &key) {
316 let _ = cache_query_deps(tcx, key);
320 pub(crate) fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
322 Q: QueryDescription<QueryCtxt<'tcx>>,
323 Q::Key: DepNodeParams<TyCtxt<'tcx>>,
325 if let Some(key) = Q::Key::recover(tcx, &dep_node) {
326 #[cfg(debug_assertions)]
327 let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
328 let tcx = QueryCtxt::from_tcx(tcx);
329 force_query::<Q, _>(tcx, key, dep_node);
336 pub(crate) fn query_callback<'tcx, Q: QueryConfig>(
337 // NOTE: we can't remove these function pointers, because `recover` is invariant -> `try_load_from_on_disk_cache` takes a concrete lifetime, not a universal lifetime.
338 // Instead, we infer the correct lifetime at the callsite, so we can pass in a HRTB function pointer to the DepKindStruct.
339 try_load_from_on_disk_cache: fn(TyCtxt<'_>, DepNode),
340 force_from_dep_node: fn(TyCtxt<'_>, DepNode) -> bool,
342 is_eval_always: bool,
345 Q: QueryDescription<QueryCtxt<'tcx>>,
346 Q::Key: DepNodeParams<TyCtxt<'tcx>>,
348 let fingerprint_style = Q::Key::fingerprint_style();
350 if is_anon || !fingerprint_style.reconstructible() {
351 return DepKindStruct {
355 force_from_dep_node: None,
356 try_load_from_on_disk_cache: None,
364 force_from_dep_node: Some(force_from_dep_node),
365 try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache),
369 // NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
370 // invoked by `rustc_query_append`.
371 macro_rules! define_queries {
374 [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
375 define_queries_struct! {
376 input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
379 #[allow(nonstandard_style)]
381 use std::marker::PhantomData;
383 $(pub struct $name<'tcx> {
384 data: PhantomData<&'tcx ()>
388 $(impl<'tcx> QueryConfig for queries::$name<'tcx> {
389 type Key = query_keys::$name<'tcx>;
390 type Value = query_values::$name<'tcx>;
391 type Stored = query_stored::$name<'tcx>;
392 const NAME: &'static str = stringify!($name);
395 impl<'tcx> QueryDescription<QueryCtxt<'tcx>> for queries::$name<'tcx> {
396 rustc_query_description! { $name }
398 type Cache = query_storage::$name<'tcx>;
401 fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key>
402 where QueryCtxt<'tcx>: 'a
408 fn query_cache<'a>(tcx: QueryCtxt<'tcx>) -> &'a Self::Cache
411 &tcx.query_caches.$name
415 fn make_vtable(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
416 QueryVTable<QueryCtxt<'tcx>, Self::Key, Self::Value>
418 let compute = get_provider!([$($modifiers)*][tcx, $name, key]);
419 let cache_on_disk = Self::cache_on_disk(tcx.tcx, key);
421 anon: is_anon!([$($modifiers)*]),
422 eval_always: is_eval_always!([$($modifiers)*]),
423 depth_limit: depth_limit!([$($modifiers)*]),
424 dep_kind: dep_graph::DepKind::$name,
425 hash_result: hash_result!([$($modifiers)*]),
426 handle_cycle_error: |tcx, mut error| handle_cycle_error!([$($modifiers)*][tcx, error]),
429 try_load_from_disk: Self::TRY_LOAD_FROM_DISK,
434 #[allow(nonstandard_style)]
435 mod query_callbacks {
437 use rustc_query_system::query::QueryDescription;
438 use rustc_query_system::dep_graph::FingerprintStyle;
440 // We use this for most things when incr. comp. is turned off.
441 pub fn Null() -> DepKindStruct {
444 is_eval_always: false,
445 fingerprint_style: FingerprintStyle::Unit,
446 force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
447 try_load_from_on_disk_cache: None,
451 // We use this for the forever-red node.
452 pub fn Red() -> DepKindStruct {
455 is_eval_always: false,
456 fingerprint_style: FingerprintStyle::Unit,
457 force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
458 try_load_from_on_disk_cache: None,
462 pub fn TraitSelect() -> DepKindStruct {
465 is_eval_always: false,
466 fingerprint_style: FingerprintStyle::Unit,
467 force_from_dep_node: None,
468 try_load_from_on_disk_cache: None,
472 pub fn CompileCodegenUnit() -> DepKindStruct {
475 is_eval_always: false,
476 fingerprint_style: FingerprintStyle::Opaque,
477 force_from_dep_node: None,
478 try_load_from_on_disk_cache: None,
482 pub fn CompileMonoItem() -> DepKindStruct {
485 is_eval_always: false,
486 fingerprint_style: FingerprintStyle::Opaque,
487 force_from_dep_node: None,
488 try_load_from_on_disk_cache: None,
492 $(pub(crate) fn $name()-> DepKindStruct {
493 let is_anon = is_anon!([$($modifiers)*]);
494 let is_eval_always = is_eval_always!([$($modifiers)*]);
495 type Q<'tcx> = queries::$name<'tcx>;
497 $crate::plumbing::query_callback::<Q<'_>>(
498 |tcx, key| $crate::plumbing::try_load_from_on_disk_cache::<<Q<'_> as QueryConfig>::Key, _>(tcx, key, <Q<'_>>::cache_on_disk, TyCtxt::$name),
499 |tcx, key| $crate::plumbing::force_from_dep_node::<Q<'_>>(tcx, key),
506 pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct] {
507 arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
512 macro_rules! define_queries_struct {
514 input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
515 pub struct Queries<'tcx> {
516 local_providers: Box<Providers>,
517 extern_providers: Box<ExternProviders>,
519 pub on_disk_cache: Option<OnDiskCache<'tcx>>,
523 $($(#[$attr])* $name: QueryState<<queries::$name<'tcx> as QueryConfig>::Key>,)*
526 impl<'tcx> Queries<'tcx> {
528 local_providers: Providers,
529 extern_providers: ExternProviders,
530 on_disk_cache: Option<OnDiskCache<'tcx>>,
533 local_providers: Box::new(local_providers),
534 extern_providers: Box::new(extern_providers),
536 jobs: AtomicU64::new(1),
537 $($name: Default::default()),*
541 pub(crate) fn try_collect_active_jobs(
544 ) -> Option<QueryMap> {
545 let tcx = QueryCtxt { tcx, queries: self };
546 let mut jobs = QueryMap::default();
549 let make_query = |tcx, key| {
550 let kind = dep_graph::DepKind::$name;
551 let name = stringify!($name);
552 $crate::plumbing::create_query_frame(tcx, queries::$name::describe, key, kind, name)
554 self.$name.try_collect_active_jobs(
565 impl<'tcx> QueryEngine<'tcx> for Queries<'tcx> {
566 fn as_any(&'tcx self) -> &'tcx dyn std::any::Any {
567 let this = unsafe { std::mem::transmute::<&Queries<'_>, &Queries<'_>>(self) };
571 fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
572 let qcx = QueryCtxt { tcx, queries: self };
573 tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
578 #[tracing::instrument(level = "trace", skip(self, tcx), ret)]
583 key: <queries::$name<'tcx> as QueryConfig>::Key,
585 ) -> Option<query_stored::$name<'tcx>> {
586 let qcx = QueryCtxt { tcx, queries: self };
587 get_query::<queries::$name<'tcx>, _>(qcx, span, key, mode)