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 fn try_load_from_on_disk_cache<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode)
306 Q: QueryDescription<QueryCtxt<'tcx>>,
307 Q::Key: DepNodeParams<TyCtxt<'tcx>>,
309 debug_assert!(tcx.dep_graph.is_green(&dep_node));
311 let key = Q::Key::recover(tcx, &dep_node).unwrap_or_else(|| {
312 panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
314 if Q::cache_on_disk(tcx, &key) {
315 let _ = Q::execute_query(tcx, key);
319 fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
321 Q: QueryDescription<QueryCtxt<'tcx>>,
322 Q::Key: DepNodeParams<TyCtxt<'tcx>>,
324 if let Some(key) = Q::Key::recover(tcx, &dep_node) {
325 #[cfg(debug_assertions)]
326 let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
327 let tcx = QueryCtxt::from_tcx(tcx);
328 force_query::<Q, _>(tcx, key, dep_node);
335 pub(crate) fn query_callback<'tcx, Q: QueryConfig>(
337 is_eval_always: bool,
338 ) -> DepKindStruct<'tcx>
340 Q: QueryDescription<QueryCtxt<'tcx>>,
341 Q::Key: DepNodeParams<TyCtxt<'tcx>>,
343 let fingerprint_style = Q::Key::fingerprint_style();
345 if is_anon || !fingerprint_style.reconstructible() {
346 return DepKindStruct {
350 force_from_dep_node: None,
351 try_load_from_on_disk_cache: None,
359 force_from_dep_node: Some(force_from_dep_node::<Q>),
360 try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache::<Q>),
364 // NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
365 // invoked by `rustc_query_append`.
366 macro_rules! define_queries {
369 [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
370 define_queries_struct! {
371 input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
374 #[allow(nonstandard_style)]
376 use std::marker::PhantomData;
378 $(pub struct $name<'tcx> {
379 data: PhantomData<&'tcx ()>
383 $(impl<'tcx> QueryConfig for queries::$name<'tcx> {
384 type Key = query_keys::$name<'tcx>;
385 type Value = query_values::$name<'tcx>;
386 type Stored = query_stored::$name<'tcx>;
387 const NAME: &'static str = stringify!($name);
390 impl<'tcx> QueryDescription<QueryCtxt<'tcx>> for queries::$name<'tcx> {
391 rustc_query_description! { $name }
393 type Cache = query_storage::$name<'tcx>;
396 fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key>
397 where QueryCtxt<'tcx>: 'a
403 fn query_cache<'a>(tcx: QueryCtxt<'tcx>) -> &'a Self::Cache
406 &tcx.query_caches.$name
410 fn make_vtable(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
411 QueryVTable<QueryCtxt<'tcx>, Self::Key, Self::Value>
413 let compute = get_provider!([$($modifiers)*][tcx, $name, key]);
414 let cache_on_disk = Self::cache_on_disk(tcx.tcx, key);
416 anon: is_anon!([$($modifiers)*]),
417 eval_always: is_eval_always!([$($modifiers)*]),
418 depth_limit: depth_limit!([$($modifiers)*]),
419 dep_kind: dep_graph::DepKind::$name,
420 hash_result: hash_result!([$($modifiers)*]),
421 handle_cycle_error: |tcx, mut error| handle_cycle_error!([$($modifiers)*][tcx, error]),
424 try_load_from_disk: Self::TRY_LOAD_FROM_DISK,
428 fn execute_query(tcx: TyCtxt<'tcx>, k: Self::Key) -> Self::Stored {
433 #[allow(nonstandard_style)]
434 mod query_callbacks {
436 use rustc_query_system::dep_graph::FingerprintStyle;
438 // We use this for most things when incr. comp. is turned off.
439 pub fn Null<'tcx>() -> DepKindStruct<'tcx> {
442 is_eval_always: false,
443 fingerprint_style: FingerprintStyle::Unit,
444 force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
445 try_load_from_on_disk_cache: None,
449 // We use this for the forever-red node.
450 pub fn Red<'tcx>() -> DepKindStruct<'tcx> {
453 is_eval_always: false,
454 fingerprint_style: FingerprintStyle::Unit,
455 force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
456 try_load_from_on_disk_cache: None,
460 pub fn TraitSelect<'tcx>() -> DepKindStruct<'tcx> {
463 is_eval_always: false,
464 fingerprint_style: FingerprintStyle::Unit,
465 force_from_dep_node: None,
466 try_load_from_on_disk_cache: None,
470 pub fn CompileCodegenUnit<'tcx>() -> DepKindStruct<'tcx> {
473 is_eval_always: false,
474 fingerprint_style: FingerprintStyle::Opaque,
475 force_from_dep_node: None,
476 try_load_from_on_disk_cache: None,
480 pub fn CompileMonoItem<'tcx>() -> DepKindStruct<'tcx> {
483 is_eval_always: false,
484 fingerprint_style: FingerprintStyle::Opaque,
485 force_from_dep_node: None,
486 try_load_from_on_disk_cache: None,
490 $(pub(crate) fn $name<'tcx>()-> DepKindStruct<'tcx> {
491 $crate::plumbing::query_callback::<queries::$name<'tcx>>(
492 is_anon!([$($modifiers)*]),
493 is_eval_always!([$($modifiers)*]),
498 pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] {
499 arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
504 macro_rules! define_queries_struct {
506 input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
507 pub struct Queries<'tcx> {
508 local_providers: Box<Providers>,
509 extern_providers: Box<ExternProviders>,
511 pub on_disk_cache: Option<OnDiskCache<'tcx>>,
515 $($(#[$attr])* $name: QueryState<<queries::$name<'tcx> as QueryConfig>::Key>,)*
518 impl<'tcx> Queries<'tcx> {
520 local_providers: Providers,
521 extern_providers: ExternProviders,
522 on_disk_cache: Option<OnDiskCache<'tcx>>,
525 local_providers: Box::new(local_providers),
526 extern_providers: Box::new(extern_providers),
528 jobs: AtomicU64::new(1),
529 $($name: Default::default()),*
533 pub(crate) fn try_collect_active_jobs(
536 ) -> Option<QueryMap> {
537 let tcx = QueryCtxt { tcx, queries: self };
538 let mut jobs = QueryMap::default();
541 let make_query = |tcx, key| {
542 let kind = dep_graph::DepKind::$name;
543 let name = stringify!($name);
544 $crate::plumbing::create_query_frame(tcx, queries::$name::describe, key, kind, name)
546 self.$name.try_collect_active_jobs(
557 impl<'tcx> QueryEngine<'tcx> for Queries<'tcx> {
558 fn as_any(&'tcx self) -> &'tcx dyn std::any::Any {
559 let this = unsafe { std::mem::transmute::<&Queries<'_>, &Queries<'_>>(self) };
563 fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
564 let qcx = QueryCtxt { tcx, queries: self };
565 tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
570 #[tracing::instrument(level = "trace", skip(self, tcx), ret)]
575 key: <queries::$name<'tcx> as QueryConfig>::Key,
577 ) -> Option<query_stored::$name<'tcx>> {
578 let qcx = QueryCtxt { tcx, queries: self };
579 get_query::<queries::$name<'tcx>, _>(qcx, span, key, mode)