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.
5 use crate::{on_disk_cache, Queries};
6 use rustc_middle::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
7 use rustc_middle::ty::tls::{self, ImplicitCtxt};
8 use rustc_middle::ty::TyCtxt;
9 use rustc_query_system::dep_graph::HasDepContext;
10 use rustc_query_system::query::{QueryContext, QueryJobId, QueryMap, QuerySideEffects};
12 use rustc_data_structures::sync::Lock;
13 use rustc_data_structures::thin_vec::ThinVec;
14 use rustc_errors::{Diagnostic, Handler};
17 use std::num::NonZeroU64;
19 #[derive(Copy, Clone)]
20 pub struct QueryCtxt<'tcx> {
21 pub tcx: TyCtxt<'tcx>,
22 pub queries: &'tcx Queries<'tcx>,
25 impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
26 type Target = TyCtxt<'tcx>;
29 fn deref(&self) -> &Self::Target {
34 impl<'tcx> HasDepContext for QueryCtxt<'tcx> {
35 type DepKind = rustc_middle::dep_graph::DepKind;
36 type DepContext = TyCtxt<'tcx>;
39 fn dep_context(&self) -> &Self::DepContext {
44 impl QueryContext for QueryCtxt<'_> {
45 fn next_job_id(&self) -> QueryJobId {
48 self.queries.jobs.fetch_add(1, rustc_data_structures::sync::Ordering::Relaxed),
54 fn current_query_job(&self) -> Option<QueryJobId> {
55 tls::with_related_context(**self, |icx| icx.query)
58 fn try_collect_active_jobs(&self) -> Option<QueryMap> {
59 self.queries.try_collect_active_jobs(**self)
62 // Interactions with on_disk_cache
63 fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects {
67 .map(|c| c.load_side_effects(**self, prev_dep_node_index))
71 fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
72 if let Some(c) = self.queries.on_disk_cache.as_ref() {
73 c.store_side_effects(dep_node_index, side_effects)
77 fn store_side_effects_for_anon_node(
79 dep_node_index: DepNodeIndex,
80 side_effects: QuerySideEffects,
82 if let Some(c) = self.queries.on_disk_cache.as_ref() {
83 c.store_side_effects_for_anon_node(dep_node_index, side_effects)
87 /// Executes a job by changing the `ImplicitCtxt` to point to the
88 /// new query job while it executes. It returns the diagnostics
89 /// captured during execution and the actual result.
94 diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
95 compute: impl FnOnce() -> R,
97 // The `TyCtxt` stored in TLS has the same global interner lifetime
98 // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
99 // when accessing the `ImplicitCtxt`.
100 tls::with_related_context(**self, move |current_icx| {
101 // Update the `ImplicitCtxt` to point to our new query job.
102 let new_icx = ImplicitCtxt {
106 layout_depth: current_icx.layout_depth,
107 task_deps: current_icx.task_deps,
110 // Use the `ImplicitCtxt` while we execute the query.
111 tls::enter_context(&new_icx, |_| {
112 rustc_data_structures::stack::ensure_sufficient_stack(compute)
118 impl<'tcx> QueryCtxt<'tcx> {
120 pub fn from_tcx(tcx: TyCtxt<'tcx>) -> Self {
121 let queries = tcx.queries.as_any();
122 let queries = unsafe {
123 let queries = std::mem::transmute::<&dyn Any, &dyn Any>(queries);
124 let queries = queries.downcast_ref().unwrap();
125 let queries = std::mem::transmute::<&Queries<'_>, &Queries<'_>>(queries);
128 QueryCtxt { tcx, queries }
131 pub(crate) fn on_disk_cache(self) -> Option<&'tcx on_disk_cache::OnDiskCache<'tcx>> {
132 self.queries.on_disk_cache.as_ref()
135 pub(super) fn encode_query_results(
137 encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx>,
138 query_result_index: &mut on_disk_cache::EncodedDepNodeIndex,
140 macro_rules! encode_queries {
141 ($($query:ident,)*) => {
143 on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>(
152 rustc_cached_queries!(encode_queries!);
155 pub fn try_print_query_stack(
157 query: Option<QueryJobId>,
159 num_frames: Option<usize>,
161 rustc_query_system::query::print_query_stack(self, query, handler, num_frames)
165 macro_rules! handle_cycle_error {
166 ([][$tcx: expr, $error:expr]) => {{
168 Value::from_cycle_error($tcx)
170 ([(fatal_cycle) $($rest:tt)*][$tcx:expr, $error:expr]) => {{
172 $tcx.sess.abort_if_errors();
175 ([(cycle_delay_bug) $($rest:tt)*][$tcx:expr, $error:expr]) => {{
176 $error.delay_as_bug();
177 Value::from_cycle_error($tcx)
179 ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
180 handle_cycle_error!([$($modifiers)*][$($args)*])
184 macro_rules! is_anon {
188 ([(anon) $($rest:tt)*]) => {{
191 ([$other:tt $($modifiers:tt)*]) => {
192 is_anon!([$($modifiers)*])
196 macro_rules! is_eval_always {
200 ([(eval_always) $($rest:tt)*]) => {{
203 ([$other:tt $($modifiers:tt)*]) => {
204 is_eval_always!([$($modifiers)*])
208 macro_rules! hash_result {
210 Some(dep_graph::hash_result)
212 ([(no_hash) $($rest:tt)*]) => {{
215 ([$other:tt $($modifiers:tt)*]) => {
216 hash_result!([$($modifiers)*])
220 macro_rules! get_provider {
221 ([][$tcx:expr, $name:ident, $key:expr]) => {{
222 $tcx.queries.local_providers.$name
224 ([(separate_provide_extern) $($rest:tt)*][$tcx:expr, $name:ident, $key:expr]) => {{
225 if $key.query_crate_is_local() {
226 $tcx.queries.local_providers.$name
228 $tcx.queries.extern_providers.$name
231 ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
232 get_provider!([$($modifiers)*][$($args)*])
236 macro_rules! define_queries {
239 [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
240 define_queries_struct! {
242 input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
248 // Create an eponymous constructor for each query.
249 $(#[allow(nonstandard_style)] $(#[$attr])*
250 pub fn $name<$tcx>(tcx: QueryCtxt<$tcx>, key: query_keys::$name<$tcx>) -> QueryStackFrame {
251 let kind = dep_graph::DepKind::$name;
252 let name = stringify!($name);
253 // Disable visible paths printing for performance reasons.
254 // Showing visible path instead of any path is not that important in production.
255 let description = ty::print::with_no_visible_paths!(
256 // Force filename-line mode to avoid invoking `type_of` query.
257 ty::print::with_forced_impl_filename_line!(
258 queries::$name::describe(tcx, key)
261 let description = if tcx.sess.verbose() {
262 format!("{} [{}]", description, name)
266 let span = if kind == dep_graph::DepKind::def_span {
267 // The `def_span` query is used to calculate `default_span`,
268 // so exit to avoid infinite recursion.
271 Some(key.default_span(*tcx))
273 let def_kind = if kind == dep_graph::DepKind::opt_def_kind {
274 // Try to avoid infinite recursion.
278 .and_then(|def_id| def_id.as_local())
279 .and_then(|def_id| tcx.opt_def_kind(def_id))
282 tcx.with_stable_hashing_context(|mut hcx|{
283 let mut hasher = StableHasher::new();
284 std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
285 key.hash_stable(&mut hcx, &mut hasher);
286 hasher.finish::<u64>()
290 QueryStackFrame::new(name, description, span, def_kind, hash)
294 #[allow(nonstandard_style)]
296 use std::marker::PhantomData;
298 $(pub struct $name<$tcx> {
299 data: PhantomData<&$tcx ()>
303 $(impl<$tcx> QueryConfig for queries::$name<$tcx> {
304 type Key = query_keys::$name<$tcx>;
305 type Value = query_values::$name<$tcx>;
306 type Stored = query_stored::$name<$tcx>;
307 const NAME: &'static str = stringify!($name);
310 impl<$tcx> QueryDescription<QueryCtxt<$tcx>> for queries::$name<$tcx> {
311 rustc_query_description! { $name<$tcx> }
313 type Cache = query_storage::$name<$tcx>;
316 fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<Self::Key>
317 where QueryCtxt<$tcx>: 'a
323 fn query_cache<'a>(tcx: QueryCtxt<$tcx>) -> &'a Self::Cache
326 &tcx.query_caches.$name
330 fn make_vtable(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
331 QueryVTable<QueryCtxt<$tcx>, Self::Key, Self::Value>
333 let compute = get_provider!([$($modifiers)*][tcx, $name, key]);
334 let cache_on_disk = Self::cache_on_disk(tcx.tcx, key);
336 anon: is_anon!([$($modifiers)*]),
337 eval_always: is_eval_always!([$($modifiers)*]),
338 dep_kind: dep_graph::DepKind::$name,
339 hash_result: hash_result!([$($modifiers)*]),
340 handle_cycle_error: |tcx, mut error| handle_cycle_error!([$($modifiers)*][tcx, error]),
343 try_load_from_disk: Self::TRY_LOAD_FROM_DISK,
348 #[allow(nonstandard_style)]
349 mod query_callbacks {
351 use rustc_middle::dep_graph::DepNode;
352 use rustc_middle::ty::query::query_keys;
353 use rustc_query_system::dep_graph::DepNodeParams;
354 use rustc_query_system::query::{force_query, QueryDescription};
355 use rustc_query_system::dep_graph::FingerprintStyle;
357 // We use this for most things when incr. comp. is turned off.
358 pub fn Null() -> DepKindStruct {
361 is_eval_always: false,
362 fingerprint_style: FingerprintStyle::Unit,
363 force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
364 try_load_from_on_disk_cache: None,
368 // We use this for the forever-red node.
369 pub fn Red() -> DepKindStruct {
372 is_eval_always: false,
373 fingerprint_style: FingerprintStyle::Unit,
374 force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
375 try_load_from_on_disk_cache: None,
379 pub fn TraitSelect() -> DepKindStruct {
382 is_eval_always: false,
383 fingerprint_style: FingerprintStyle::Unit,
384 force_from_dep_node: None,
385 try_load_from_on_disk_cache: None,
389 pub fn CompileCodegenUnit() -> DepKindStruct {
392 is_eval_always: false,
393 fingerprint_style: FingerprintStyle::Opaque,
394 force_from_dep_node: None,
395 try_load_from_on_disk_cache: None,
399 pub fn CompileMonoItem() -> DepKindStruct {
402 is_eval_always: false,
403 fingerprint_style: FingerprintStyle::Opaque,
404 force_from_dep_node: None,
405 try_load_from_on_disk_cache: None,
409 $(pub(crate) fn $name()-> DepKindStruct {
410 let is_anon = is_anon!([$($modifiers)*]);
411 let is_eval_always = is_eval_always!([$($modifiers)*]);
413 let fingerprint_style =
414 <query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>::fingerprint_style();
416 if is_anon || !fingerprint_style.reconstructible() {
417 return DepKindStruct {
421 force_from_dep_node: None,
422 try_load_from_on_disk_cache: None,
427 fn recover<'tcx>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> Option<query_keys::$name<'tcx>> {
428 <query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>::recover(tcx, &dep_node)
431 fn force_from_dep_node(tcx: TyCtxt<'_>, dep_node: DepNode) -> bool {
432 if let Some(key) = recover(tcx, dep_node) {
433 #[cfg(debug_assertions)]
434 let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
435 let tcx = QueryCtxt::from_tcx(tcx);
436 force_query::<queries::$name<'_>, _>(tcx, key, dep_node);
443 fn try_load_from_on_disk_cache(tcx: TyCtxt<'_>, dep_node: DepNode) {
444 debug_assert!(tcx.dep_graph.is_green(&dep_node));
446 let key = recover(tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
447 if queries::$name::cache_on_disk(tcx, &key) {
448 let _ = tcx.$name(key);
456 force_from_dep_node: Some(force_from_dep_node),
457 try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache),
462 pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct] {
463 arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
468 // FIXME(eddyb) this macro (and others?) use `$tcx` and `'tcx` interchangeably.
469 // We should either not take `$tcx` at all and use `'tcx` everywhere, or use
470 // `$tcx` everywhere (even if that isn't necessary due to lack of hygiene).
471 macro_rules! define_queries_struct {
473 input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
474 pub struct Queries<$tcx> {
475 local_providers: Box<Providers>,
476 extern_providers: Box<ExternProviders>,
478 pub on_disk_cache: Option<OnDiskCache<$tcx>>,
482 $($(#[$attr])* $name: QueryState<query_keys::$name<$tcx>>,)*
485 impl<$tcx> Queries<$tcx> {
487 local_providers: Providers,
488 extern_providers: ExternProviders,
489 on_disk_cache: Option<OnDiskCache<$tcx>>,
492 local_providers: Box::new(local_providers),
493 extern_providers: Box::new(extern_providers),
495 jobs: AtomicU64::new(1),
496 $($name: Default::default()),*
500 pub(crate) fn try_collect_active_jobs(
503 ) -> Option<QueryMap> {
504 let tcx = QueryCtxt { tcx, queries: self };
505 let mut jobs = QueryMap::default();
508 self.$name.try_collect_active_jobs(
519 impl<'tcx> QueryEngine<'tcx> for Queries<'tcx> {
520 fn as_any(&'tcx self) -> &'tcx dyn std::any::Any {
521 let this = unsafe { std::mem::transmute::<&Queries<'_>, &Queries<'_>>(self) };
525 fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
526 let qcx = QueryCtxt { tcx, queries: self };
527 tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
532 #[tracing::instrument(level = "trace", skip(self, tcx))]
537 key: query_keys::$name<$tcx>,
539 ) -> Option<query_stored::$name<$tcx>> {
540 let qcx = QueryCtxt { tcx, queries: self };
541 get_query::<queries::$name<$tcx>, _>(qcx, span, key, mode)