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::CacheDecoder;
7 use crate::{on_disk_cache, Queries};
8 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
9 use rustc_data_structures::sync::{AtomicU64, Lock};
10 use rustc_errors::{Diagnostic, Handler};
11 use rustc_middle::dep_graph::{
12 self, DepKind, DepKindStruct, DepNode, DepNodeIndex, SerializedDepNodeIndex,
14 use rustc_middle::ty::tls::{self, ImplicitCtxt};
15 use rustc_middle::ty::{self, TyCtxt};
16 use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext};
17 use rustc_query_system::ich::StableHashingContext;
18 use rustc_query_system::query::{
19 force_query, QueryConfig, QueryContext, QueryDescription, QueryJobId, QueryMap,
20 QuerySideEffects, QueryStackFrame,
22 use rustc_query_system::Value;
23 use rustc_serialize::Decodable;
25 use std::num::NonZeroU64;
26 use thin_vec::ThinVec;
28 #[derive(Copy, Clone)]
29 pub struct QueryCtxt<'tcx> {
30 pub tcx: TyCtxt<'tcx>,
31 pub queries: &'tcx Queries<'tcx>,
34 impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
35 type Target = TyCtxt<'tcx>;
38 fn deref(&self) -> &Self::Target {
43 impl<'tcx> HasDepContext for QueryCtxt<'tcx> {
44 type DepKind = rustc_middle::dep_graph::DepKind;
45 type DepContext = TyCtxt<'tcx>;
48 fn dep_context(&self) -> &Self::DepContext {
53 impl QueryContext for QueryCtxt<'_> {
54 fn next_job_id(&self) -> QueryJobId {
57 self.queries.jobs.fetch_add(1, rustc_data_structures::sync::Ordering::Relaxed),
63 fn current_query_job(&self) -> Option<QueryJobId> {
64 tls::with_related_context(**self, |icx| icx.query)
67 fn try_collect_active_jobs(&self) -> Option<QueryMap> {
68 self.queries.try_collect_active_jobs(**self)
71 // Interactions with on_disk_cache
72 fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects {
76 .map(|c| c.load_side_effects(**self, prev_dep_node_index))
80 fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
81 if let Some(c) = self.queries.on_disk_cache.as_ref() {
82 c.store_side_effects(dep_node_index, side_effects)
86 fn store_side_effects_for_anon_node(
88 dep_node_index: DepNodeIndex,
89 side_effects: QuerySideEffects,
91 if let Some(c) = self.queries.on_disk_cache.as_ref() {
92 c.store_side_effects_for_anon_node(dep_node_index, side_effects)
96 /// Executes a job by changing the `ImplicitCtxt` to point to the
97 /// new query job while it executes. It returns the diagnostics
98 /// captured during execution and the actual result.
104 diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
105 compute: impl FnOnce() -> R,
107 // The `TyCtxt` stored in TLS has the same global interner lifetime
108 // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
109 // when accessing the `ImplicitCtxt`.
110 tls::with_related_context(**self, move |current_icx| {
111 if depth_limit && !self.recursion_limit().value_within_limit(current_icx.query_depth) {
112 self.depth_limit_error();
115 // Update the `ImplicitCtxt` to point to our new query job.
116 let new_icx = ImplicitCtxt {
120 query_depth: current_icx.query_depth + depth_limit as usize,
121 task_deps: current_icx.task_deps,
124 // Use the `ImplicitCtxt` while we execute the query.
125 tls::enter_context(&new_icx, |_| {
126 rustc_data_structures::stack::ensure_sufficient_stack(compute)
132 impl<'tcx> QueryCtxt<'tcx> {
134 pub fn from_tcx(tcx: TyCtxt<'tcx>) -> Self {
135 let queries = tcx.queries.as_any();
136 let queries = unsafe {
137 let queries = std::mem::transmute::<&dyn Any, &dyn Any>(queries);
138 let queries = queries.downcast_ref().unwrap();
139 let queries = std::mem::transmute::<&Queries<'_>, &Queries<'_>>(queries);
142 QueryCtxt { tcx, queries }
145 pub(crate) fn on_disk_cache(self) -> Option<&'tcx on_disk_cache::OnDiskCache<'tcx>> {
146 self.queries.on_disk_cache.as_ref()
149 pub(super) fn encode_query_results(
151 encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx>,
152 query_result_index: &mut on_disk_cache::EncodedDepNodeIndex,
154 macro_rules! expand_if_cached {
155 ([] $encode:expr) => {};
156 ([(cache) $($rest:tt)*] $encode:expr) => {
159 ([$other:tt $($modifiers:tt)*] $encode:expr) => {
160 expand_if_cached!([$($modifiers)*] $encode)
164 macro_rules! encode_queries {
167 [$($modifiers:tt)*] fn $query:ident($($K:tt)*) -> $V:ty,)*) => {
169 expand_if_cached!([$($modifiers)*] on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>(
178 rustc_query_append!(encode_queries!);
181 pub fn try_print_query_stack(
183 query: Option<QueryJobId>,
185 num_frames: Option<usize>,
187 rustc_query_system::query::print_query_stack(self, query, handler, num_frames)
191 macro_rules! handle_cycle_error {
193 rustc_query_system::HandleCycleError::Error
195 ([(fatal_cycle) $($rest:tt)*]) => {{
196 rustc_query_system::HandleCycleError::Fatal
198 ([(cycle_delay_bug) $($rest:tt)*]) => {{
199 rustc_query_system::HandleCycleError::DelayBug
201 ([$other:tt $($modifiers:tt)*]) => {
202 handle_cycle_error!([$($modifiers)*])
206 macro_rules! is_anon {
210 ([(anon) $($rest:tt)*]) => {{
213 ([$other:tt $($modifiers:tt)*]) => {
214 is_anon!([$($modifiers)*])
218 macro_rules! is_eval_always {
222 ([(eval_always) $($rest:tt)*]) => {{
225 ([$other:tt $($modifiers:tt)*]) => {
226 is_eval_always!([$($modifiers)*])
230 macro_rules! depth_limit {
234 ([(depth_limit) $($rest:tt)*]) => {{
237 ([$other:tt $($modifiers:tt)*]) => {
238 depth_limit!([$($modifiers)*])
242 macro_rules! hash_result {
244 Some(dep_graph::hash_result)
246 ([(no_hash) $($rest:tt)*]) => {{
249 ([$other:tt $($modifiers:tt)*]) => {
250 hash_result!([$($modifiers)*])
254 macro_rules! get_provider {
255 ([][$tcx:expr, $name:ident, $key:expr]) => {{
256 $tcx.queries.local_providers.$name
258 ([(separate_provide_extern) $($rest:tt)*][$tcx:expr, $name:ident, $key:expr]) => {{
259 if $key.query_crate_is_local() {
260 $tcx.queries.local_providers.$name
262 $tcx.queries.extern_providers.$name
265 ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
266 get_provider!([$($modifiers)*][$($args)*])
270 macro_rules! should_ever_cache_on_disk {
274 ([(cache) $($rest:tt)*]) => {{
275 Some($crate::plumbing::try_load_from_disk::<Self::Value>)
277 ([$other:tt $($modifiers:tt)*]) => {
278 should_ever_cache_on_disk!([$($modifiers)*])
282 pub(crate) fn create_query_frame<
284 K: Copy + Key + for<'a> HashStable<StableHashingContext<'a>>,
286 tcx: QueryCtxt<'tcx>,
287 do_describe: fn(QueryCtxt<'tcx>, K) -> String,
291 ) -> QueryStackFrame {
292 // Disable visible paths printing for performance reasons.
293 // Showing visible path instead of any path is not that important in production.
294 let description = ty::print::with_no_visible_paths!(
295 // Force filename-line mode to avoid invoking `type_of` query.
296 ty::print::with_forced_impl_filename_line!(do_describe(tcx, key))
299 if tcx.sess.verbose() { format!("{} [{}]", description, name) } else { description };
300 let span = if kind == dep_graph::DepKind::def_span {
301 // The `def_span` query is used to calculate `default_span`,
302 // so exit to avoid infinite recursion.
305 Some(key.default_span(*tcx))
307 let def_kind = if kind == dep_graph::DepKind::opt_def_kind {
308 // Try to avoid infinite recursion.
312 .and_then(|def_id| def_id.as_local())
313 .and_then(|def_id| tcx.opt_def_kind(def_id))
316 tcx.with_stable_hashing_context(|mut hcx| {
317 let mut hasher = StableHasher::new();
318 std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
319 key.hash_stable(&mut hcx, &mut hasher);
320 hasher.finish::<u64>()
324 QueryStackFrame::new(name, description, span, def_kind, hash)
327 fn try_load_from_on_disk_cache<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode)
329 Q: QueryDescription<QueryCtxt<'tcx>>,
330 Q::Key: DepNodeParams<TyCtxt<'tcx>>,
332 debug_assert!(tcx.dep_graph.is_green(&dep_node));
334 let key = Q::Key::recover(tcx, &dep_node).unwrap_or_else(|| {
335 panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
337 if Q::cache_on_disk(tcx, &key) {
338 let _ = Q::execute_query(tcx, key);
342 pub(crate) fn try_load_from_disk<'tcx, V>(
343 tcx: QueryCtxt<'tcx>,
344 id: SerializedDepNodeIndex,
347 V: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
349 tcx.on_disk_cache().as_ref()?.try_load_query_result(*tcx, id)
352 fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
354 Q: QueryDescription<QueryCtxt<'tcx>>,
355 Q::Key: DepNodeParams<TyCtxt<'tcx>>,
356 Q::Value: Value<TyCtxt<'tcx>>,
358 if let Some(key) = Q::Key::recover(tcx, &dep_node) {
359 #[cfg(debug_assertions)]
360 let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
361 let tcx = QueryCtxt::from_tcx(tcx);
362 force_query::<Q, _>(tcx, key, dep_node);
369 pub(crate) fn query_callback<'tcx, Q: QueryConfig>(
371 is_eval_always: bool,
372 ) -> DepKindStruct<'tcx>
374 Q: QueryDescription<QueryCtxt<'tcx>>,
375 Q::Key: DepNodeParams<TyCtxt<'tcx>>,
377 let fingerprint_style = Q::Key::fingerprint_style();
379 if is_anon || !fingerprint_style.reconstructible() {
380 return DepKindStruct {
384 force_from_dep_node: None,
385 try_load_from_on_disk_cache: None,
393 force_from_dep_node: Some(force_from_dep_node::<Q>),
394 try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache::<Q>),
398 // NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
399 // invoked by `rustc_query_append`.
400 macro_rules! define_queries {
403 [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
404 define_queries_struct! {
405 input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
408 #[allow(nonstandard_style)]
410 use std::marker::PhantomData;
412 $(pub struct $name<'tcx> {
413 data: PhantomData<&'tcx ()>
417 $(impl<'tcx> QueryConfig for queries::$name<'tcx> {
418 type Key = query_keys::$name<'tcx>;
419 type Value = query_values::$name<'tcx>;
420 type Stored = query_stored::$name<'tcx>;
421 const NAME: &'static str = stringify!($name);
424 impl<'tcx> QueryDescription<QueryCtxt<'tcx>> for queries::$name<'tcx> {
425 rustc_query_description! { $name }
427 type Cache = query_storage::$name<'tcx>;
430 fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key>
431 where QueryCtxt<'tcx>: 'a
437 fn query_cache<'a>(tcx: QueryCtxt<'tcx>) -> &'a Self::Cache
440 &tcx.query_caches.$name
444 fn make_vtable(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
445 QueryVTable<QueryCtxt<'tcx>, Self::Key, Self::Value>
447 let compute = get_provider!([$($modifiers)*][tcx, $name, key]);
448 let cache_on_disk = Self::cache_on_disk(tcx.tcx, key);
450 anon: is_anon!([$($modifiers)*]),
451 eval_always: is_eval_always!([$($modifiers)*]),
452 depth_limit: depth_limit!([$($modifiers)*]),
453 dep_kind: dep_graph::DepKind::$name,
454 hash_result: hash_result!([$($modifiers)*]),
455 handle_cycle_error: handle_cycle_error!([$($modifiers)*]),
457 try_load_from_disk: if cache_on_disk { should_ever_cache_on_disk!([$($modifiers)*]) } else { None },
461 fn execute_query(tcx: TyCtxt<'tcx>, k: Self::Key) -> Self::Stored {
466 #[allow(nonstandard_style)]
467 mod query_callbacks {
469 use rustc_query_system::dep_graph::FingerprintStyle;
471 // We use this for most things when incr. comp. is turned off.
472 pub fn Null<'tcx>() -> DepKindStruct<'tcx> {
475 is_eval_always: false,
476 fingerprint_style: FingerprintStyle::Unit,
477 force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
478 try_load_from_on_disk_cache: None,
482 // We use this for the forever-red node.
483 pub fn Red<'tcx>() -> DepKindStruct<'tcx> {
486 is_eval_always: false,
487 fingerprint_style: FingerprintStyle::Unit,
488 force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
489 try_load_from_on_disk_cache: None,
493 pub fn TraitSelect<'tcx>() -> DepKindStruct<'tcx> {
496 is_eval_always: false,
497 fingerprint_style: FingerprintStyle::Unit,
498 force_from_dep_node: None,
499 try_load_from_on_disk_cache: None,
503 pub fn CompileCodegenUnit<'tcx>() -> DepKindStruct<'tcx> {
506 is_eval_always: false,
507 fingerprint_style: FingerprintStyle::Opaque,
508 force_from_dep_node: None,
509 try_load_from_on_disk_cache: None,
513 pub fn CompileMonoItem<'tcx>() -> DepKindStruct<'tcx> {
516 is_eval_always: false,
517 fingerprint_style: FingerprintStyle::Opaque,
518 force_from_dep_node: None,
519 try_load_from_on_disk_cache: None,
523 $(pub(crate) fn $name<'tcx>()-> DepKindStruct<'tcx> {
524 $crate::plumbing::query_callback::<queries::$name<'tcx>>(
525 is_anon!([$($modifiers)*]),
526 is_eval_always!([$($modifiers)*]),
531 pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] {
532 arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
537 use crate::{ExternProviders, OnDiskCache, Providers};
539 impl<'tcx> Queries<'tcx> {
541 local_providers: Providers,
542 extern_providers: ExternProviders,
543 on_disk_cache: Option<OnDiskCache<'tcx>>,
546 local_providers: Box::new(local_providers),
547 extern_providers: Box::new(extern_providers),
549 jobs: AtomicU64::new(1),
555 macro_rules! define_queries_struct {
557 input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
559 pub struct Queries<'tcx> {
560 local_providers: Box<Providers>,
561 extern_providers: Box<ExternProviders>,
563 pub on_disk_cache: Option<OnDiskCache<'tcx>>,
567 $($(#[$attr])* $name: QueryState<<queries::$name<'tcx> as QueryConfig>::Key>,)*
570 impl<'tcx> Queries<'tcx> {
571 pub(crate) fn try_collect_active_jobs(
574 ) -> Option<QueryMap> {
575 let tcx = QueryCtxt { tcx, queries: self };
576 let mut jobs = QueryMap::default();
579 let make_query = |tcx, key| {
580 let kind = dep_graph::DepKind::$name;
581 let name = stringify!($name);
582 $crate::plumbing::create_query_frame(tcx, queries::$name::describe, key, kind, name)
584 self.$name.try_collect_active_jobs(
595 impl<'tcx> QueryEngine<'tcx> for Queries<'tcx> {
596 fn as_any(&'tcx self) -> &'tcx dyn std::any::Any {
597 let this = unsafe { std::mem::transmute::<&Queries<'_>, &Queries<'_>>(self) };
601 fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
602 let qcx = QueryCtxt { tcx, queries: self };
603 tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
608 #[tracing::instrument(level = "trace", skip(self, tcx), ret)]
613 key: <queries::$name<'tcx> as QueryConfig>::Key,
615 ) -> Option<query_stored::$name<'tcx>> {
616 let qcx = QueryCtxt { tcx, queries: self };
617 get_query::<queries::$name<'tcx>, _>(qcx, span, key, mode)