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::{AtomicU64, 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,
21 use rustc_query_system::Value;
23 use std::num::NonZeroU64;
24 use thin_vec::ThinVec;
26 #[derive(Copy, Clone)]
27 pub struct QueryCtxt<'tcx> {
28 pub tcx: TyCtxt<'tcx>,
29 pub queries: &'tcx Queries<'tcx>,
32 impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
33 type Target = TyCtxt<'tcx>;
36 fn deref(&self) -> &Self::Target {
41 impl<'tcx> HasDepContext for QueryCtxt<'tcx> {
42 type DepKind = rustc_middle::dep_graph::DepKind;
43 type DepContext = TyCtxt<'tcx>;
46 fn dep_context(&self) -> &Self::DepContext {
51 impl QueryContext for QueryCtxt<'_> {
52 fn next_job_id(&self) -> QueryJobId {
55 self.queries.jobs.fetch_add(1, rustc_data_structures::sync::Ordering::Relaxed),
61 fn current_query_job(&self) -> Option<QueryJobId> {
62 tls::with_related_context(**self, |icx| icx.query)
65 fn try_collect_active_jobs(&self) -> Option<QueryMap> {
66 self.queries.try_collect_active_jobs(**self)
69 // Interactions with on_disk_cache
70 fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects {
74 .map(|c| c.load_side_effects(**self, prev_dep_node_index))
78 fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
79 if let Some(c) = self.queries.on_disk_cache.as_ref() {
80 c.store_side_effects(dep_node_index, side_effects)
84 fn store_side_effects_for_anon_node(
86 dep_node_index: DepNodeIndex,
87 side_effects: QuerySideEffects,
89 if let Some(c) = self.queries.on_disk_cache.as_ref() {
90 c.store_side_effects_for_anon_node(dep_node_index, side_effects)
94 /// Executes a job by changing the `ImplicitCtxt` to point to the
95 /// new query job while it executes. It returns the diagnostics
96 /// captured during execution and the actual result.
102 diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
103 compute: impl FnOnce() -> R,
105 // The `TyCtxt` stored in TLS has the same global interner lifetime
106 // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
107 // when accessing the `ImplicitCtxt`.
108 tls::with_related_context(**self, move |current_icx| {
109 if depth_limit && !self.recursion_limit().value_within_limit(current_icx.query_depth) {
110 self.depth_limit_error();
113 // Update the `ImplicitCtxt` to point to our new query job.
114 let new_icx = ImplicitCtxt {
118 query_depth: current_icx.query_depth + depth_limit as usize,
119 task_deps: current_icx.task_deps,
122 // Use the `ImplicitCtxt` while we execute the query.
123 tls::enter_context(&new_icx, |_| {
124 rustc_data_structures::stack::ensure_sufficient_stack(compute)
130 impl<'tcx> QueryCtxt<'tcx> {
132 pub fn from_tcx(tcx: TyCtxt<'tcx>) -> Self {
133 let queries = tcx.queries.as_any();
134 let queries = unsafe {
135 let queries = std::mem::transmute::<&dyn Any, &dyn Any>(queries);
136 let queries = queries.downcast_ref().unwrap();
137 let queries = std::mem::transmute::<&Queries<'_>, &Queries<'_>>(queries);
140 QueryCtxt { tcx, queries }
143 pub(crate) fn on_disk_cache(self) -> Option<&'tcx on_disk_cache::OnDiskCache<'tcx>> {
144 self.queries.on_disk_cache.as_ref()
147 pub(super) fn encode_query_results(
149 encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx>,
150 query_result_index: &mut on_disk_cache::EncodedDepNodeIndex,
152 macro_rules! encode_queries {
153 ($($query:ident,)*) => {
155 on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>(
164 rustc_cached_queries!(encode_queries!);
167 pub fn try_print_query_stack(
169 query: Option<QueryJobId>,
171 num_frames: Option<usize>,
173 rustc_query_system::query::print_query_stack(self, query, handler, num_frames)
177 macro_rules! handle_cycle_error {
179 rustc_query_system::HandleCycleError::Error
181 ([(fatal_cycle) $($rest:tt)*]) => {{
182 rustc_query_system::HandleCycleError::Fatal
184 ([(cycle_delay_bug) $($rest:tt)*]) => {{
185 rustc_query_system::HandleCycleError::DelayBug
187 ([$other:tt $($modifiers:tt)*]) => {
188 handle_cycle_error!([$($modifiers)*])
192 macro_rules! is_anon {
196 ([(anon) $($rest:tt)*]) => {{
199 ([$other:tt $($modifiers:tt)*]) => {
200 is_anon!([$($modifiers)*])
204 macro_rules! is_eval_always {
208 ([(eval_always) $($rest:tt)*]) => {{
211 ([$other:tt $($modifiers:tt)*]) => {
212 is_eval_always!([$($modifiers)*])
216 macro_rules! depth_limit {
220 ([(depth_limit) $($rest:tt)*]) => {{
223 ([$other:tt $($modifiers:tt)*]) => {
224 depth_limit!([$($modifiers)*])
228 macro_rules! hash_result {
230 Some(dep_graph::hash_result)
232 ([(no_hash) $($rest:tt)*]) => {{
235 ([$other:tt $($modifiers:tt)*]) => {
236 hash_result!([$($modifiers)*])
240 macro_rules! get_provider {
241 ([][$tcx:expr, $name:ident, $key:expr]) => {{
242 $tcx.queries.local_providers.$name
244 ([(separate_provide_extern) $($rest:tt)*][$tcx:expr, $name:ident, $key:expr]) => {{
245 if $key.query_crate_is_local() {
246 $tcx.queries.local_providers.$name
248 $tcx.queries.extern_providers.$name
251 ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
252 get_provider!([$($modifiers)*][$($args)*])
256 pub(crate) fn create_query_frame<
258 K: Copy + Key + for<'a> HashStable<StableHashingContext<'a>>,
260 tcx: QueryCtxt<'tcx>,
261 do_describe: fn(QueryCtxt<'tcx>, K) -> String,
265 ) -> QueryStackFrame {
266 // Disable visible paths printing for performance reasons.
267 // Showing visible path instead of any path is not that important in production.
268 let description = ty::print::with_no_visible_paths!(
269 // Force filename-line mode to avoid invoking `type_of` query.
270 ty::print::with_forced_impl_filename_line!(do_describe(tcx, key))
273 if tcx.sess.verbose() { format!("{} [{}]", description, name) } else { description };
274 let span = if kind == dep_graph::DepKind::def_span {
275 // The `def_span` query is used to calculate `default_span`,
276 // so exit to avoid infinite recursion.
279 Some(key.default_span(*tcx))
281 let def_kind = if kind == dep_graph::DepKind::opt_def_kind {
282 // Try to avoid infinite recursion.
286 .and_then(|def_id| def_id.as_local())
287 .and_then(|def_id| tcx.opt_def_kind(def_id))
290 tcx.with_stable_hashing_context(|mut hcx| {
291 let mut hasher = StableHasher::new();
292 std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
293 key.hash_stable(&mut hcx, &mut hasher);
294 hasher.finish::<u64>()
298 QueryStackFrame::new(name, description, span, def_kind, hash)
301 fn try_load_from_on_disk_cache<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode)
303 Q: QueryDescription<QueryCtxt<'tcx>>,
304 Q::Key: DepNodeParams<TyCtxt<'tcx>>,
306 debug_assert!(tcx.dep_graph.is_green(&dep_node));
308 let key = Q::Key::recover(tcx, &dep_node).unwrap_or_else(|| {
309 panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
311 if Q::cache_on_disk(tcx, &key) {
312 let _ = Q::execute_query(tcx, key);
316 fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
318 Q: QueryDescription<QueryCtxt<'tcx>>,
319 Q::Key: DepNodeParams<TyCtxt<'tcx>>,
320 Q::Value: Value<TyCtxt<'tcx>>,
322 if let Some(key) = Q::Key::recover(tcx, &dep_node) {
323 #[cfg(debug_assertions)]
324 let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
325 let tcx = QueryCtxt::from_tcx(tcx);
326 force_query::<Q, _>(tcx, key, dep_node);
333 pub(crate) fn query_callback<'tcx, Q: QueryConfig>(
335 is_eval_always: bool,
336 ) -> DepKindStruct<'tcx>
338 Q: QueryDescription<QueryCtxt<'tcx>>,
339 Q::Key: DepNodeParams<TyCtxt<'tcx>>,
341 let fingerprint_style = Q::Key::fingerprint_style();
343 if is_anon || !fingerprint_style.reconstructible() {
344 return DepKindStruct {
348 force_from_dep_node: None,
349 try_load_from_on_disk_cache: None,
357 force_from_dep_node: Some(force_from_dep_node::<Q>),
358 try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache::<Q>),
362 // NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
363 // invoked by `rustc_query_append`.
364 macro_rules! define_queries {
367 [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
368 define_queries_struct! {
369 input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
372 #[allow(nonstandard_style)]
374 use std::marker::PhantomData;
376 $(pub struct $name<'tcx> {
377 data: PhantomData<&'tcx ()>
381 $(impl<'tcx> QueryConfig for queries::$name<'tcx> {
382 type Key = query_keys::$name<'tcx>;
383 type Value = query_values::$name<'tcx>;
384 type Stored = query_stored::$name<'tcx>;
385 const NAME: &'static str = stringify!($name);
388 impl<'tcx> QueryDescription<QueryCtxt<'tcx>> for queries::$name<'tcx> {
389 rustc_query_description! { $name }
391 type Cache = query_storage::$name<'tcx>;
394 fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key>
395 where QueryCtxt<'tcx>: 'a
401 fn query_cache<'a>(tcx: QueryCtxt<'tcx>) -> &'a Self::Cache
404 &tcx.query_caches.$name
408 fn make_vtable(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
409 QueryVTable<QueryCtxt<'tcx>, Self::Key, Self::Value>
411 let compute = get_provider!([$($modifiers)*][tcx, $name, key]);
412 let cache_on_disk = Self::cache_on_disk(tcx.tcx, key);
414 anon: is_anon!([$($modifiers)*]),
415 eval_always: is_eval_always!([$($modifiers)*]),
416 depth_limit: depth_limit!([$($modifiers)*]),
417 dep_kind: dep_graph::DepKind::$name,
418 hash_result: hash_result!([$($modifiers)*]),
419 handle_cycle_error: handle_cycle_error!([$($modifiers)*]),
422 try_load_from_disk: Self::TRY_LOAD_FROM_DISK,
426 fn execute_query(tcx: TyCtxt<'tcx>, k: Self::Key) -> Self::Stored {
431 #[allow(nonstandard_style)]
432 mod query_callbacks {
434 use rustc_query_system::dep_graph::FingerprintStyle;
436 // We use this for most things when incr. comp. is turned off.
437 pub fn Null<'tcx>() -> DepKindStruct<'tcx> {
440 is_eval_always: false,
441 fingerprint_style: FingerprintStyle::Unit,
442 force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
443 try_load_from_on_disk_cache: None,
447 // We use this for the forever-red node.
448 pub fn Red<'tcx>() -> DepKindStruct<'tcx> {
451 is_eval_always: false,
452 fingerprint_style: FingerprintStyle::Unit,
453 force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
454 try_load_from_on_disk_cache: None,
458 pub fn TraitSelect<'tcx>() -> DepKindStruct<'tcx> {
461 is_eval_always: false,
462 fingerprint_style: FingerprintStyle::Unit,
463 force_from_dep_node: None,
464 try_load_from_on_disk_cache: None,
468 pub fn CompileCodegenUnit<'tcx>() -> DepKindStruct<'tcx> {
471 is_eval_always: false,
472 fingerprint_style: FingerprintStyle::Opaque,
473 force_from_dep_node: None,
474 try_load_from_on_disk_cache: None,
478 pub fn CompileMonoItem<'tcx>() -> DepKindStruct<'tcx> {
481 is_eval_always: false,
482 fingerprint_style: FingerprintStyle::Opaque,
483 force_from_dep_node: None,
484 try_load_from_on_disk_cache: None,
488 $(pub(crate) fn $name<'tcx>()-> DepKindStruct<'tcx> {
489 $crate::plumbing::query_callback::<queries::$name<'tcx>>(
490 is_anon!([$($modifiers)*]),
491 is_eval_always!([$($modifiers)*]),
496 pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] {
497 arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
502 use crate::{ExternProviders, OnDiskCache, Providers};
504 impl<'tcx> Queries<'tcx> {
506 local_providers: Providers,
507 extern_providers: ExternProviders,
508 on_disk_cache: Option<OnDiskCache<'tcx>>,
511 local_providers: Box::new(local_providers),
512 extern_providers: Box::new(extern_providers),
514 jobs: AtomicU64::new(1),
520 macro_rules! define_queries_struct {
522 input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
524 pub struct Queries<'tcx> {
525 local_providers: Box<Providers>,
526 extern_providers: Box<ExternProviders>,
528 pub on_disk_cache: Option<OnDiskCache<'tcx>>,
532 $($(#[$attr])* $name: QueryState<<queries::$name<'tcx> as QueryConfig>::Key>,)*
535 impl<'tcx> Queries<'tcx> {
536 pub(crate) fn try_collect_active_jobs(
539 ) -> Option<QueryMap> {
540 let tcx = QueryCtxt { tcx, queries: self };
541 let mut jobs = QueryMap::default();
544 let make_query = |tcx, key| {
545 let kind = dep_graph::DepKind::$name;
546 let name = stringify!($name);
547 $crate::plumbing::create_query_frame(tcx, queries::$name::describe, key, kind, name)
549 self.$name.try_collect_active_jobs(
560 impl<'tcx> QueryEngine<'tcx> for Queries<'tcx> {
561 fn as_any(&'tcx self) -> &'tcx dyn std::any::Any {
562 let this = unsafe { std::mem::transmute::<&Queries<'_>, &Queries<'_>>(self) };
566 fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
567 let qcx = QueryCtxt { tcx, queries: self };
568 tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
573 #[tracing::instrument(level = "trace", skip(self, tcx), ret)]
578 key: <queries::$name<'tcx> as QueryConfig>::Key,
580 ) -> Option<query_stored::$name<'tcx>> {
581 let qcx = QueryCtxt { tcx, queries: self };
582 get_query::<queries::$name<'tcx>, _>(qcx, span, key, mode)