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 rustc_middle::dep_graph::{DepKind, DepNode, DepNodeExt, DepNodeIndex, SerializedDepNodeIndex};
7 use rustc_middle::ty::query::on_disk_cache;
8 use rustc_middle::ty::tls::{self, ImplicitCtxt};
9 use rustc_middle::ty::{self, TyCtxt};
10 use rustc_query_system::dep_graph::HasDepContext;
11 use rustc_query_system::query::{QueryContext, QueryDescription, QueryJobId, QueryMap};
13 use rustc_data_structures::sync::Lock;
14 use rustc_data_structures::thin_vec::ThinVec;
15 use rustc_errors::Diagnostic;
16 use rustc_serialize::opaque;
17 use rustc_span::def_id::{DefId, LocalDefId};
19 #[derive(Copy, Clone)]
20 pub struct QueryCtxt<'tcx> {
21 pub tcx: TyCtxt<'tcx>,
22 pub queries: &'tcx super::Queries<'tcx>,
25 impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
26 type Target = TyCtxt<'tcx>;
28 fn deref(&self) -> &Self::Target {
33 impl HasDepContext for QueryCtxt<'tcx> {
34 type DepKind = rustc_middle::dep_graph::DepKind;
35 type StableHashingContext = rustc_middle::ich::StableHashingContext<'tcx>;
36 type DepContext = TyCtxt<'tcx>;
39 fn dep_context(&self) -> &Self::DepContext {
44 impl QueryContext for QueryCtxt<'tcx> {
45 fn def_path_str(&self, def_id: DefId) -> String {
46 self.tcx.def_path_str(def_id)
49 fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>> {
50 tls::with_related_context(**self, |icx| icx.query)
53 fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind>> {
54 self.queries.try_collect_active_jobs(**self)
57 fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) {
58 let cb = &super::QUERY_CALLBACKS[dep_node.kind as usize];
59 (cb.try_load_from_on_disk_cache)(*self, dep_node)
62 fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool {
63 // FIXME: This match is just a workaround for incremental bugs and should
64 // be removed. https://github.com/rust-lang/rust/issues/62649 is one such
65 // bug that must be fixed before removing this.
67 DepKind::hir_owner | DepKind::hir_owner_nodes => {
68 if let Some(def_id) = dep_node.extract_def_id(**self) {
69 let def_id = def_id.expect_local();
70 let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
71 if def_id != hir_id.owner {
72 // This `DefPath` does not have a
73 // corresponding `DepNode` (e.g. a
74 // struct field), and the ` DefPath`
75 // collided with the `DefPath` of a
76 // proper item that existed in the
77 // previous compilation session.
79 // Since the given `DefPath` does not
80 // denote the item that previously
81 // existed, we just fail to mark green.
85 // If the node does not exist anymore, we
86 // just fail to mark green.
91 // For other kinds of nodes it's OK to be
96 debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
98 // We must avoid ever having to call `force_from_dep_node()` for a
99 // `DepNode::codegen_unit`:
100 // Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
101 // would always end up having to evaluate the first caller of the
102 // `codegen_unit` query that *is* reconstructible. This might very well be
103 // the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
104 // to re-trigger calling the `codegen_unit` query with the right key. At
105 // that point we would already have re-done all the work we are trying to
106 // avoid doing in the first place.
107 // The solution is simple: Just explicitly call the `codegen_unit` query for
108 // each CGU, right after partitioning. This way `try_mark_green` will always
109 // hit the cache instead of having to go through `force_from_dep_node`.
110 // This assertion makes sure, we actually keep applying the solution above.
112 dep_node.kind != DepKind::codegen_unit,
113 "calling force_from_dep_node() on DepKind::codegen_unit"
116 let cb = &super::QUERY_CALLBACKS[dep_node.kind as usize];
117 (cb.force_from_dep_node)(*self, dep_node)
120 // Interactions with on_disk_cache
121 fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
124 .map(|c| c.load_diagnostics(**self, prev_dep_node_index))
128 fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
129 if let Some(c) = self.on_disk_cache.as_ref() {
130 c.store_diagnostics(dep_node_index, diagnostics)
134 fn store_diagnostics_for_anon_node(
136 dep_node_index: DepNodeIndex,
137 diagnostics: ThinVec<Diagnostic>,
139 if let Some(c) = self.on_disk_cache.as_ref() {
140 c.store_diagnostics_for_anon_node(dep_node_index, diagnostics)
144 /// Executes a job by changing the `ImplicitCtxt` to point to the
145 /// new query job while it executes. It returns the diagnostics
146 /// captured during execution and the actual result.
150 token: QueryJobId<Self::DepKind>,
151 diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
152 compute: impl FnOnce() -> R,
154 // The `TyCtxt` stored in TLS has the same global interner lifetime
155 // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
156 // when accessing the `ImplicitCtxt`.
157 tls::with_related_context(**self, move |current_icx| {
158 // Update the `ImplicitCtxt` to point to our new query job.
159 let new_icx = ImplicitCtxt {
163 layout_depth: current_icx.layout_depth,
164 task_deps: current_icx.task_deps,
167 // Use the `ImplicitCtxt` while we execute the query.
168 tls::enter_context(&new_icx, |_| {
169 rustc_data_structures::stack::ensure_sufficient_stack(compute)
175 impl<'tcx> QueryCtxt<'tcx> {
176 pub(super) fn encode_query_results(
178 encoder: &mut on_disk_cache::CacheEncoder<'a, 'tcx, opaque::FileEncoder>,
179 query_result_index: &mut on_disk_cache::EncodedQueryResultIndex,
180 ) -> opaque::FileEncodeResult {
181 macro_rules! encode_queries {
182 ($($query:ident,)*) => {
184 on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>(
193 rustc_cached_queries!(encode_queries!);
199 /// This struct stores metadata about each Query.
201 /// Information is retrieved by indexing the `QUERIES` array using the integer value
202 /// of the `DepKind`. Overall, this allows to implement `QueryContext` using this manual
203 /// jump table instead of large matches.
204 pub struct QueryStruct {
205 /// The red/green evaluation system will try to mark a specific DepNode in the
206 /// dependency graph as green by recursively trying to mark the dependencies of
207 /// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode`
208 /// where we don't know if it is red or green and we therefore actually have
209 /// to recompute its value in order to find out. Since the only piece of
210 /// information that we have at that point is the `DepNode` we are trying to
211 /// re-evaluate, we need some way to re-run a query from just that. This is what
212 /// `force_from_dep_node()` implements.
214 /// In the general case, a `DepNode` consists of a `DepKind` and an opaque
215 /// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
216 /// is usually constructed by computing a stable hash of the query-key that the
217 /// `DepNode` corresponds to. Consequently, it is not in general possible to go
218 /// back from hash to query-key (since hash functions are not reversible). For
219 /// this reason `force_from_dep_node()` is expected to fail from time to time
220 /// because we just cannot find out, from the `DepNode` alone, what the
221 /// corresponding query-key is and therefore cannot re-run the query.
223 /// The system deals with this case letting `try_mark_green` fail which forces
224 /// the root query to be re-evaluated.
226 /// Now, if `force_from_dep_node()` would always fail, it would be pretty useless.
227 /// Fortunately, we can use some contextual information that will allow us to
228 /// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we
229 /// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a
230 /// valid `DefPathHash`. Since we also always build a huge table that maps every
231 /// `DefPathHash` in the current codebase to the corresponding `DefId`, we have
232 /// everything we need to re-run the query.
234 /// Take the `mir_promoted` query as an example. Like many other queries, it
235 /// just has a single parameter: the `DefId` of the item it will compute the
236 /// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode`
237 /// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
238 /// is actually a `DefPathHash`, and can therefore just look up the corresponding
239 /// `DefId` in `tcx.def_path_hash_to_def_id`.
241 /// When you implement a new query, it will likely have a corresponding new
242 /// `DepKind`, and you'll have to support it here in `force_from_dep_node()`. As
243 /// a rule of thumb, if your query takes a `DefId` or `LocalDefId` as sole parameter,
244 /// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
245 /// add it to the "We don't have enough information to reconstruct..." group in
247 pub(crate) force_from_dep_node: fn(tcx: QueryCtxt<'_>, dep_node: &DepNode) -> bool,
249 /// Invoke a query to put the on-disk cached value in memory.
250 pub(crate) try_load_from_on_disk_cache: fn(QueryCtxt<'_>, &DepNode),
253 macro_rules! handle_cycle_error {
254 ([][$tcx: expr, $error:expr]) => {{
256 Value::from_cycle_error($tcx)
258 ([fatal_cycle $($rest:tt)*][$tcx:expr, $error:expr]) => {{
260 $tcx.sess.abort_if_errors();
263 ([cycle_delay_bug $($rest:tt)*][$tcx:expr, $error:expr]) => {{
264 $error.delay_as_bug();
265 Value::from_cycle_error($tcx)
267 ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
268 handle_cycle_error!([$($($modifiers)*)*][$($args)*])
272 macro_rules! is_anon {
276 ([anon $($rest:tt)*]) => {{
279 ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => {
280 is_anon!([$($($modifiers)*)*])
284 macro_rules! is_eval_always {
288 ([eval_always $($rest:tt)*]) => {{
291 ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => {
292 is_eval_always!([$($($modifiers)*)*])
296 macro_rules! hash_result {
297 ([][$hcx:expr, $result:expr]) => {{
298 dep_graph::hash_result($hcx, &$result)
300 ([no_hash $($rest:tt)*][$hcx:expr, $result:expr]) => {{
303 ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
304 hash_result!([$($($modifiers)*)*][$($args)*])
308 macro_rules! define_queries {
311 [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
313 define_queries_struct! {
315 input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
321 // Create an eponymous constructor for each query.
322 $(#[allow(nonstandard_style)] $(#[$attr])*
323 pub fn $name<$tcx>(tcx: QueryCtxt<$tcx>, key: query_keys::$name<$tcx>) -> QueryStackFrame {
324 let kind = dep_graph::DepKind::$name;
325 let name = stringify!($name);
326 let description = ty::print::with_forced_impl_filename_line(
327 // Force filename-line mode to avoid invoking `type_of` query.
328 || queries::$name::describe(tcx, key)
330 let description = if tcx.sess.verbose() {
331 format!("{} [{}]", description, name)
335 let span = if kind == dep_graph::DepKind::def_span {
336 // The `def_span` query is used to calculate `default_span`,
337 // so exit to avoid infinite recursion.
340 Some(key.default_span(*tcx))
343 let mut hcx = tcx.create_stable_hashing_context();
344 let mut hasher = StableHasher::new();
345 std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
346 key.hash_stable(&mut hcx, &mut hasher);
347 hasher.finish::<u64>()
350 QueryStackFrame::new(name, description, span, hash)
354 #[allow(nonstandard_style)]
356 use std::marker::PhantomData;
358 $(pub struct $name<$tcx> {
359 data: PhantomData<&$tcx ()>
363 $(impl<$tcx> QueryConfig for queries::$name<$tcx> {
364 type Key = query_keys::$name<$tcx>;
365 type Value = query_values::$name<$tcx>;
366 type Stored = query_stored::$name<$tcx>;
367 const NAME: &'static str = stringify!($name);
370 impl<$tcx> QueryAccessors<QueryCtxt<$tcx>> for queries::$name<$tcx> {
371 const ANON: bool = is_anon!([$($modifiers)*]);
372 const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
373 const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$name;
375 type Cache = query_storage::$name<$tcx>;
378 fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, Self::Key>
379 where QueryCtxt<$tcx>: 'a
385 fn query_cache<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryCacheStore<Self::Cache>
388 &tcx.query_caches.$name
392 fn compute(tcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value {
393 let is_local = key.query_crate() == LOCAL_CRATE;
394 let provider = if is_local {
395 tcx.queries.local_providers.$name
397 tcx.queries.extern_providers.$name
403 _hcx: &mut StableHashingContext<'_>,
404 _result: &Self::Value
405 ) -> Option<Fingerprint> {
406 hash_result!([$($modifiers)*][_hcx, _result])
409 fn handle_cycle_error(
410 tcx: QueryCtxt<'tcx>,
411 mut error: DiagnosticBuilder<'_>,
413 handle_cycle_error!([$($modifiers)*][tcx, error])
417 #[allow(non_upper_case_globals)]
418 pub mod query_callbacks {
420 use rustc_middle::dep_graph::DepNode;
421 use rustc_middle::ty::query::query_keys;
422 use rustc_query_system::dep_graph::DepNodeParams;
423 use rustc_query_system::query::{force_query, QueryDescription};
425 // We use this for most things when incr. comp. is turned off.
426 pub const Null: QueryStruct = QueryStruct {
427 force_from_dep_node: |_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node),
428 try_load_from_on_disk_cache: |_, _| {},
431 pub const TraitSelect: QueryStruct = QueryStruct {
432 force_from_dep_node: |_, _| false,
433 try_load_from_on_disk_cache: |_, _| {},
436 pub const CompileCodegenUnit: QueryStruct = QueryStruct {
437 force_from_dep_node: |_, _| false,
438 try_load_from_on_disk_cache: |_, _| {},
441 $(pub const $name: QueryStruct = {
442 const is_anon: bool = is_anon!([$($modifiers)*]);
445 fn can_reconstruct_query_key() -> bool {
446 <query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>
447 ::can_reconstruct_query_key()
450 fn recover<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<query_keys::$name<'tcx>> {
451 <query_keys::$name<'_> as DepNodeParams<TyCtxt<'_>>>::recover(tcx, dep_node)
454 fn force_from_dep_node(tcx: QueryCtxt<'_>, dep_node: &DepNode) -> bool {
459 if !can_reconstruct_query_key() {
463 if let Some(key) = recover(*tcx, dep_node) {
464 force_query::<queries::$name<'_>, _>(tcx, key, DUMMY_SP, *dep_node);
471 fn try_load_from_on_disk_cache(tcx: QueryCtxt<'_>, dep_node: &DepNode) {
476 if !can_reconstruct_query_key() {
480 debug_assert!(tcx.dep_graph
481 .node_color(dep_node)
482 .map(|c| c.is_green())
485 let key = recover(*tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
486 if queries::$name::cache_on_disk(tcx, &key, None) {
487 let _ = tcx.$name(key);
493 try_load_from_on_disk_cache,
498 static QUERY_CALLBACKS: &[QueryStruct] = &make_dep_kind_array!(query_callbacks);
502 // FIXME(eddyb) this macro (and others?) use `$tcx` and `'tcx` interchangeably.
503 // We should either not take `$tcx` at all and use `'tcx` everywhere, or use
504 // `$tcx` everywhere (even if that isn't necessary due to lack of hygiene).
505 macro_rules! define_queries_struct {
507 input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
508 pub struct Queries<$tcx> {
509 local_providers: Box<Providers>,
510 extern_providers: Box<Providers>,
512 $($(#[$attr])* $name: QueryState<
513 crate::dep_graph::DepKind,
514 query_keys::$name<$tcx>,
518 impl<$tcx> Queries<$tcx> {
520 local_providers: Providers,
521 extern_providers: Providers,
524 local_providers: Box::new(local_providers),
525 extern_providers: Box::new(extern_providers),
526 $($name: Default::default()),*
530 pub(crate) fn try_collect_active_jobs(
533 ) -> Option<QueryMap<crate::dep_graph::DepKind>> {
534 let tcx = QueryCtxt { tcx, queries: self };
535 let mut jobs = QueryMap::default();
538 self.$name.try_collect_active_jobs(
540 dep_graph::DepKind::$name,
550 impl QueryEngine<'tcx> for Queries<'tcx> {
551 unsafe fn deadlock(&'tcx self, _tcx: TyCtxt<'tcx>, _registry: &rustc_rayon_core::Registry) {
552 #[cfg(parallel_compiler)]
554 let tcx = QueryCtxt { tcx: _tcx, queries: self };
555 rustc_query_system::query::deadlock(tcx, _registry)
559 fn encode_query_results(
562 encoder: &mut on_disk_cache::CacheEncoder<'a, 'tcx, opaque::FileEncoder>,
563 query_result_index: &mut on_disk_cache::EncodedQueryResultIndex,
564 ) -> opaque::FileEncodeResult {
565 let tcx = QueryCtxt { tcx, queries: self };
566 tcx.encode_query_results(encoder, query_result_index)
569 fn exec_cache_promotions(&'tcx self, tcx: TyCtxt<'tcx>) {
570 let tcx = QueryCtxt { tcx, queries: self };
571 tcx.dep_graph.exec_cache_promotions(tcx)
574 fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
575 let qcx = QueryCtxt { tcx, queries: self };
576 tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
579 fn try_print_query_stack(
582 query: Option<QueryJobId<dep_graph::DepKind>>,
584 num_frames: Option<usize>,
586 let qcx = QueryCtxt { tcx, queries: self };
587 rustc_query_system::query::print_query_stack(qcx, query, handler, num_frames)
596 key: query_keys::$name<$tcx>,
599 ) -> Option<query_stored::$name<$tcx>> {
600 let qcx = QueryCtxt { tcx, queries: self };
601 get_query::<queries::$name<$tcx>, _>(qcx, span, key, lookup, mode)
607 fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String {
608 if def_id.is_top_level_module() {
609 "top-level module".to_string()
611 format!("module `{}`", tcx.def_path_str(def_id.to_def_id()))
615 rustc_query_description! {}