]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/query/plumbing.rs
Auto merge of #58406 - Disasm:rv64-support, r=nagisa
[rust.git] / src / librustc / ty / query / plumbing.rs
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.
4
5 use crate::dep_graph::{DepNodeIndex, DepNode, DepKind, SerializedDepNodeIndex};
6 use crate::ty::tls;
7 use crate::ty::{TyCtxt};
8 use crate::ty::query::Query;
9 use crate::ty::query::config::{QueryConfig, QueryDescription};
10 use crate::ty::query::job::{QueryJob, QueryResult, QueryInfo};
11 use crate::ty::item_path;
12
13 use crate::util::common::{profq_msg, ProfileQueriesMsg, QueryMsg};
14
15 use errors::DiagnosticBuilder;
16 use errors::Level;
17 use errors::Diagnostic;
18 use errors::FatalError;
19 use rustc_data_structures::fx::{FxHashMap};
20 use rustc_data_structures::sync::{Lrc, Lock};
21 use rustc_data_structures::thin_vec::ThinVec;
22 use std::mem;
23 use std::ptr;
24 use std::collections::hash_map::Entry;
25 use syntax_pos::Span;
26 use syntax::source_map::DUMMY_SP;
27
28 pub struct QueryCache<'tcx, D: QueryConfig<'tcx> + ?Sized> {
29     pub(super) results: FxHashMap<D::Key, QueryValue<D::Value>>,
30     pub(super) active: FxHashMap<D::Key, QueryResult<'tcx>>,
31     #[cfg(debug_assertions)]
32     pub(super) cache_hits: usize,
33 }
34
35 pub(super) struct QueryValue<T> {
36     pub(super) value: T,
37     pub(super) index: DepNodeIndex,
38 }
39
40 impl<T> QueryValue<T> {
41     pub(super) fn new(value: T,
42                       dep_node_index: DepNodeIndex)
43                       -> QueryValue<T> {
44         QueryValue {
45             value,
46             index: dep_node_index,
47         }
48     }
49 }
50
51 impl<'tcx, M: QueryConfig<'tcx>> Default for QueryCache<'tcx, M> {
52     fn default() -> QueryCache<'tcx, M> {
53         QueryCache {
54             results: FxHashMap::default(),
55             active: FxHashMap::default(),
56             #[cfg(debug_assertions)]
57             cache_hits: 0,
58         }
59     }
60 }
61
62 // If enabled, send a message to the profile-queries thread
63 macro_rules! profq_msg {
64     ($tcx:expr, $msg:expr) => {
65         if cfg!(debug_assertions) {
66             if $tcx.sess.profile_queries() {
67                 profq_msg($tcx.sess, $msg)
68             }
69         }
70     }
71 }
72
73 // If enabled, format a key using its debug string, which can be
74 // expensive to compute (in terms of time).
75 macro_rules! profq_query_msg {
76     ($query:expr, $tcx:expr, $key:expr) => {{
77         let msg = if cfg!(debug_assertions) {
78             if $tcx.sess.profile_queries_and_keys() {
79                 Some(format!("{:?}", $key))
80             } else { None }
81         } else { None };
82         QueryMsg {
83             query: $query,
84             msg,
85         }
86     }}
87 }
88
89 /// A type representing the responsibility to execute the job in the `job` field.
90 /// This will poison the relevant query if dropped.
91 pub(super) struct JobOwner<'a, 'tcx: 'a, Q: QueryDescription<'tcx> + 'a> {
92     cache: &'a Lock<QueryCache<'tcx, Q>>,
93     key: Q::Key,
94     job: Lrc<QueryJob<'tcx>>,
95 }
96
97 impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
98     /// Either gets a JobOwner corresponding the query, allowing us to
99     /// start executing the query, or it returns with the result of the query.
100     /// If the query is executing elsewhere, this will wait for it.
101     /// If the query panicked, this will silently panic.
102     ///
103     /// This function is inlined because that results in a noticeable speedup
104     /// for some compile-time benchmarks.
105     #[inline(always)]
106     pub(super) fn try_get(
107         tcx: TyCtxt<'a, 'tcx, '_>,
108         span: Span,
109         key: &Q::Key,
110     ) -> TryGetJob<'a, 'tcx, Q> {
111         let cache = Q::query_cache(tcx);
112         loop {
113             let mut lock = cache.borrow_mut();
114             if let Some(value) = lock.results.get(key) {
115                 profq_msg!(tcx, ProfileQueriesMsg::CacheHit);
116                 tcx.sess.profiler(|p| p.record_query_hit(Q::NAME, Q::CATEGORY));
117                 let result = Ok((value.value.clone(), value.index));
118                 #[cfg(debug_assertions)]
119                 {
120                     lock.cache_hits += 1;
121                 }
122                 return TryGetJob::JobCompleted(result);
123             }
124             let job = match lock.active.entry((*key).clone()) {
125                 Entry::Occupied(entry) => {
126                     match *entry.get() {
127                         QueryResult::Started(ref job) => {
128                             //For parallel queries, we'll block and wait until the query running
129                             //in another thread has completed. Record how long we wait in the
130                             //self-profiler
131                             #[cfg(parallel_compiler)]
132                             tcx.sess.profiler(|p| p.query_blocked_start(Q::NAME, Q::CATEGORY));
133
134                             job.clone()
135                         },
136                         QueryResult::Poisoned => FatalError.raise(),
137                     }
138                 }
139                 Entry::Vacant(entry) => {
140                     // No job entry for this query. Return a new one to be started later
141                     return tls::with_related_context(tcx, |icx| {
142                         // Create the `parent` variable before `info`. This allows LLVM
143                         // to elide the move of `info`
144                         let parent = icx.query.clone();
145                         let info = QueryInfo {
146                             span,
147                             query: Q::query(key.clone()),
148                         };
149                         let job = Lrc::new(QueryJob::new(info, parent));
150                         let owner = JobOwner {
151                             cache,
152                             job: job.clone(),
153                             key: (*key).clone(),
154                         };
155                         entry.insert(QueryResult::Started(job));
156                         TryGetJob::NotYetStarted(owner)
157                     })
158                 }
159             };
160             mem::drop(lock);
161
162             // If we are single-threaded we know that we have cycle error,
163             // so we just turn the errror
164             #[cfg(not(parallel_compiler))]
165             return job.cycle_error(tcx, span);
166
167             // With parallel queries we might just have to wait on some other
168             // thread
169             #[cfg(parallel_compiler)]
170             {
171                 let result = job.r#await(tcx, span);
172                 tcx.sess.profiler(|p| p.query_blocked_end(Q::NAME, Q::CATEGORY));
173
174                 if let Err(cycle) = result {
175                     return TryGetJob::JobCompleted(Err(cycle));
176                 }
177             }
178         }
179     }
180
181     /// Completes the query by updating the query cache with the `result`,
182     /// signals the waiter and forgets the JobOwner, so it won't poison the query
183     #[inline(always)]
184     pub(super) fn complete(self, result: &Q::Value, dep_node_index: DepNodeIndex) {
185         // We can move out of `self` here because we `mem::forget` it below
186         let key = unsafe { ptr::read(&self.key) };
187         let job = unsafe { ptr::read(&self.job) };
188         let cache = self.cache;
189
190         // Forget ourself so our destructor won't poison the query
191         mem::forget(self);
192
193         let value = QueryValue::new(result.clone(), dep_node_index);
194         {
195             let mut lock = cache.borrow_mut();
196             lock.active.remove(&key);
197             lock.results.insert(key, value);
198         }
199
200         job.signal_complete();
201     }
202 }
203
204 #[inline(always)]
205 fn with_diagnostics<F, R>(f: F) -> (R, ThinVec<Diagnostic>)
206 where
207     F: FnOnce(Option<&Lock<ThinVec<Diagnostic>>>) -> R
208 {
209     let diagnostics = Lock::new(ThinVec::new());
210     let result = f(Some(&diagnostics));
211     (result, diagnostics.into_inner())
212 }
213
214 impl<'a, 'tcx, Q: QueryDescription<'tcx>> Drop for JobOwner<'a, 'tcx, Q> {
215     #[inline(never)]
216     #[cold]
217     fn drop(&mut self) {
218         // Poison the query so jobs waiting on it panic
219         self.cache.borrow_mut().active.insert(self.key.clone(), QueryResult::Poisoned);
220         // Also signal the completion of the job, so waiters
221         // will continue execution
222         self.job.signal_complete();
223     }
224 }
225
226 #[derive(Clone)]
227 pub struct CycleError<'tcx> {
228     /// The query and related span which uses the cycle
229     pub(super) usage: Option<(Span, Query<'tcx>)>,
230     pub(super) cycle: Vec<QueryInfo<'tcx>>,
231 }
232
233 /// The result of `try_get_lock`
234 pub(super) enum TryGetJob<'a, 'tcx: 'a, D: QueryDescription<'tcx> + 'a> {
235     /// The query is not yet started. Contains a guard to the cache eventually used to start it.
236     NotYetStarted(JobOwner<'a, 'tcx, D>),
237
238     /// The query was already completed.
239     /// Returns the result of the query and its dep node index
240     /// if it succeeded or a cycle error if it failed
241     JobCompleted(Result<(D::Value, DepNodeIndex), Box<CycleError<'tcx>>>),
242 }
243
244 impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
245     /// Executes a job by changing the ImplicitCtxt to point to the
246     /// new query job while it executes. It returns the diagnostics
247     /// captured during execution and the actual result.
248     #[inline(always)]
249     pub(super) fn start_query<F, R>(
250         self,
251         job: Lrc<QueryJob<'gcx>>,
252         diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
253         compute: F)
254     -> R
255     where
256         F: for<'b, 'lcx> FnOnce(TyCtxt<'b, 'gcx, 'lcx>) -> R
257     {
258         // The TyCtxt stored in TLS has the same global interner lifetime
259         // as `self`, so we use `with_related_context` to relate the 'gcx lifetimes
260         // when accessing the ImplicitCtxt
261         tls::with_related_context(self, move |current_icx| {
262             // Update the ImplicitCtxt to point to our new query job
263             let new_icx = tls::ImplicitCtxt {
264                 tcx: self.global_tcx(),
265                 query: Some(job),
266                 diagnostics,
267                 layout_depth: current_icx.layout_depth,
268                 task_deps: current_icx.task_deps,
269             };
270
271             // Use the ImplicitCtxt while we execute the query
272             tls::enter_context(&new_icx, |_| {
273                 compute(self.global_tcx())
274             })
275         })
276     }
277
278     #[inline(never)]
279     #[cold]
280     pub(super) fn report_cycle(
281         self,
282         box CycleError { usage, cycle: stack }: Box<CycleError<'gcx>>
283     ) -> Box<DiagnosticBuilder<'a>>
284     {
285         assert!(!stack.is_empty());
286
287         let fix_span = |span: Span, query: &Query<'gcx>| {
288             self.sess.source_map().def_span(query.default_span(self, span))
289         };
290
291         // Disable naming impls with types in this path, since that
292         // sometimes cycles itself, leading to extra cycle errors.
293         // (And cycle errors around impls tend to occur during the
294         // collect/coherence phases anyhow.)
295         item_path::with_forced_impl_filename_line(|| {
296             let span = fix_span(stack[1 % stack.len()].span, &stack[0].query);
297             let mut err = struct_span_err!(self.sess,
298                                            span,
299                                            E0391,
300                                            "cycle detected when {}",
301                                            stack[0].query.describe(self));
302
303             for i in 1..stack.len() {
304                 let query = &stack[i].query;
305                 let span = fix_span(stack[(i + 1) % stack.len()].span, query);
306                 err.span_note(span, &format!("...which requires {}...", query.describe(self)));
307             }
308
309             err.note(&format!("...which again requires {}, completing the cycle",
310                               stack[0].query.describe(self)));
311
312             if let Some((span, query)) = usage {
313                 err.span_note(fix_span(span, &query),
314                               &format!("cycle used when {}", query.describe(self)));
315             }
316
317             return Box::new(err)
318         })
319     }
320
321     pub fn try_print_query_stack() {
322         eprintln!("query stack during panic:");
323
324         tls::with_context_opt(|icx| {
325             if let Some(icx) = icx {
326                 let mut current_query = icx.query.clone();
327                 let mut i = 0;
328
329                 while let Some(query) = current_query {
330                     let mut db = DiagnosticBuilder::new(icx.tcx.sess.diagnostic(),
331                         Level::FailureNote,
332                         &format!("#{} [{}] {}",
333                                  i,
334                                  query.info.query.name(),
335                                  query.info.query.describe(icx.tcx)));
336                     db.set_span(icx.tcx.sess.source_map().def_span(query.info.span));
337                     icx.tcx.sess.diagnostic().force_print_db(db);
338
339                     current_query = query.parent.clone();
340                     i += 1;
341                 }
342             }
343         });
344
345         eprintln!("end of query stack");
346     }
347
348     #[inline(never)]
349     fn try_get_with<Q: QueryDescription<'gcx>>(
350         self,
351         span: Span,
352         key: Q::Key)
353     -> Result<Q::Value, Box<CycleError<'gcx>>>
354     {
355         debug!("ty::queries::{}::try_get_with(key={:?}, span={:?})",
356                Q::NAME,
357                key,
358                span);
359
360         profq_msg!(self,
361             ProfileQueriesMsg::QueryBegin(
362                 span.data(),
363                 profq_query_msg!(Q::NAME, self, key),
364             )
365         );
366
367         let job = match JobOwner::try_get(self, span, &key) {
368             TryGetJob::NotYetStarted(job) => job,
369             TryGetJob::JobCompleted(result) => {
370                 return result.map(|(v, index)| {
371                     self.dep_graph.read_index(index);
372                     v
373                 })
374             }
375         };
376
377         // Fast path for when incr. comp. is off. `to_dep_node` is
378         // expensive for some DepKinds.
379         if !self.dep_graph.is_fully_enabled() {
380             let null_dep_node = DepNode::new_no_params(crate::dep_graph::DepKind::Null);
381             return Ok(self.force_query_with_job::<Q>(key, job, null_dep_node).0);
382         }
383
384         let dep_node = Q::to_dep_node(self, &key);
385
386         if dep_node.kind.is_anon() {
387             profq_msg!(self, ProfileQueriesMsg::ProviderBegin);
388             self.sess.profiler(|p| p.start_query(Q::NAME, Q::CATEGORY));
389
390             let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
391                 self.start_query(job.job.clone(), diagnostics, |tcx| {
392                     tcx.dep_graph.with_anon_task(dep_node.kind, || {
393                         Q::compute(tcx.global_tcx(), key)
394                     })
395                 })
396             });
397
398             self.sess.profiler(|p| p.end_query(Q::NAME, Q::CATEGORY));
399             profq_msg!(self, ProfileQueriesMsg::ProviderEnd);
400
401             self.dep_graph.read_index(dep_node_index);
402
403             if unlikely!(!diagnostics.is_empty()) {
404                 self.queries.on_disk_cache
405                     .store_diagnostics_for_anon_node(dep_node_index, diagnostics);
406             }
407
408             job.complete(&result, dep_node_index);
409
410             return Ok(result);
411         }
412
413         if !dep_node.kind.is_input() {
414             // The diagnostics for this query will be
415             // promoted to the current session during
416             // try_mark_green(), so we can ignore them here.
417             let loaded = self.start_query(job.job.clone(), None, |tcx| {
418                 let marked = tcx.dep_graph.try_mark_green_and_read(tcx, &dep_node);
419                 marked.map(|(prev_dep_node_index, dep_node_index)| {
420                     (tcx.load_from_disk_and_cache_in_memory::<Q>(
421                         key.clone(),
422                         prev_dep_node_index,
423                         dep_node_index,
424                         &dep_node
425                     ), dep_node_index)
426                 })
427             });
428             if let Some((result, dep_node_index)) = loaded {
429                 job.complete(&result, dep_node_index);
430                 return Ok(result);
431             }
432         }
433
434         let (result, dep_node_index) = self.force_query_with_job::<Q>(key, job, dep_node);
435         self.dep_graph.read_index(dep_node_index);
436         Ok(result)
437     }
438
439     fn load_from_disk_and_cache_in_memory<Q: QueryDescription<'gcx>>(
440         self,
441         key: Q::Key,
442         prev_dep_node_index: SerializedDepNodeIndex,
443         dep_node_index: DepNodeIndex,
444         dep_node: &DepNode
445     ) -> Q::Value
446     {
447         // Note this function can be called concurrently from the same query
448         // We must ensure that this is handled correctly
449
450         debug_assert!(self.dep_graph.is_green(dep_node));
451
452         // First we try to load the result from the on-disk cache
453         let result = if Q::cache_on_disk(self.global_tcx(), key.clone()) &&
454                         self.sess.opts.debugging_opts.incremental_queries {
455             self.sess.profiler(|p| p.incremental_load_result_start(Q::NAME));
456             let result = Q::try_load_from_disk(self.global_tcx(), prev_dep_node_index);
457             self.sess.profiler(|p| p.incremental_load_result_end(Q::NAME));
458
459             // We always expect to find a cached result for things that
460             // can be forced from DepNode.
461             debug_assert!(!dep_node.kind.can_reconstruct_query_key() ||
462                           result.is_some(),
463                           "Missing on-disk cache entry for {:?}",
464                           dep_node);
465             result
466         } else {
467             // Some things are never cached on disk.
468             None
469         };
470
471         let result = if let Some(result) = result {
472             profq_msg!(self, ProfileQueriesMsg::CacheHit);
473             self.sess.profiler(|p| p.record_query_hit(Q::NAME, Q::CATEGORY));
474
475             result
476         } else {
477             // We could not load a result from the on-disk cache, so
478             // recompute.
479
480             self.sess.profiler(|p| p.start_query(Q::NAME, Q::CATEGORY));
481
482             // The dep-graph for this computation is already in
483             // place
484             let result = self.dep_graph.with_ignore(|| {
485                 Q::compute(self, key)
486             });
487
488             self.sess.profiler(|p| p.end_query(Q::NAME, Q::CATEGORY));
489             result
490         };
491
492         // If -Zincremental-verify-ich is specified, re-hash results from
493         // the cache and make sure that they have the expected fingerprint.
494         if unlikely!(self.sess.opts.debugging_opts.incremental_verify_ich) {
495             self.incremental_verify_ich::<Q>(&result, dep_node, dep_node_index);
496         }
497
498         if unlikely!(self.sess.opts.debugging_opts.query_dep_graph) {
499             self.dep_graph.mark_loaded_from_cache(dep_node_index, true);
500         }
501
502         result
503     }
504
505     #[inline(never)]
506     #[cold]
507     fn incremental_verify_ich<Q: QueryDescription<'gcx>>(
508         self,
509         result: &Q::Value,
510         dep_node: &DepNode,
511         dep_node_index: DepNodeIndex,
512     ) {
513         use crate::ich::Fingerprint;
514
515         assert!(Some(self.dep_graph.fingerprint_of(dep_node_index)) ==
516                 self.dep_graph.prev_fingerprint_of(dep_node),
517                 "Fingerprint for green query instance not loaded \
518                     from cache: {:?}", dep_node);
519
520         debug!("BEGIN verify_ich({:?})", dep_node);
521         let mut hcx = self.create_stable_hashing_context();
522
523         let new_hash = Q::hash_result(&mut hcx, result).unwrap_or(Fingerprint::ZERO);
524         debug!("END verify_ich({:?})", dep_node);
525
526         let old_hash = self.dep_graph.fingerprint_of(dep_node_index);
527
528         assert!(new_hash == old_hash, "Found unstable fingerprints \
529             for {:?}", dep_node);
530     }
531
532     #[inline(always)]
533     fn force_query_with_job<Q: QueryDescription<'gcx>>(
534         self,
535         key: Q::Key,
536         job: JobOwner<'_, 'gcx, Q>,
537         dep_node: DepNode)
538     -> (Q::Value, DepNodeIndex) {
539         // If the following assertion triggers, it can have two reasons:
540         // 1. Something is wrong with DepNode creation, either here or
541         //    in DepGraph::try_mark_green()
542         // 2. Two distinct query keys get mapped to the same DepNode
543         //    (see for example #48923)
544         assert!(!self.dep_graph.dep_node_exists(&dep_node),
545                 "Forcing query with already existing DepNode.\n\
546                  - query-key: {:?}\n\
547                  - dep-node: {:?}",
548                 key, dep_node);
549
550         profq_msg!(self, ProfileQueriesMsg::ProviderBegin);
551         self.sess.profiler(|p| p.start_query(Q::NAME, Q::CATEGORY));
552
553         let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
554             self.start_query(job.job.clone(), diagnostics, |tcx| {
555                 if dep_node.kind.is_eval_always() {
556                     tcx.dep_graph.with_eval_always_task(dep_node,
557                                                         tcx,
558                                                         key,
559                                                         Q::compute,
560                                                         Q::hash_result)
561                 } else {
562                     tcx.dep_graph.with_task(dep_node,
563                                             tcx,
564                                             key,
565                                             Q::compute,
566                                             Q::hash_result)
567                 }
568             })
569         });
570
571         self.sess.profiler(|p| p.end_query(Q::NAME, Q::CATEGORY));
572         profq_msg!(self, ProfileQueriesMsg::ProviderEnd);
573
574         if unlikely!(self.sess.opts.debugging_opts.query_dep_graph) {
575             self.dep_graph.mark_loaded_from_cache(dep_node_index, false);
576         }
577
578         if dep_node.kind != crate::dep_graph::DepKind::Null {
579             if unlikely!(!diagnostics.is_empty()) {
580                 self.queries.on_disk_cache
581                     .store_diagnostics(dep_node_index, diagnostics);
582             }
583         }
584
585         job.complete(&result, dep_node_index);
586
587         (result, dep_node_index)
588     }
589
590     /// Ensure that either this query has all green inputs or been executed.
591     /// Executing query::ensure(D) is considered a read of the dep-node D.
592     ///
593     /// This function is particularly useful when executing passes for their
594     /// side-effects -- e.g., in order to report errors for erroneous programs.
595     ///
596     /// Note: The optimization is only available during incr. comp.
597     pub(super) fn ensure_query<Q: QueryDescription<'gcx>>(self, key: Q::Key) -> () {
598         let dep_node = Q::to_dep_node(self, &key);
599
600         // Ensuring an "input" or anonymous query makes no sense
601         assert!(!dep_node.kind.is_anon());
602         assert!(!dep_node.kind.is_input());
603         if self.dep_graph.try_mark_green_and_read(self, &dep_node).is_none() {
604             // A None return from `try_mark_green_and_read` means that this is either
605             // a new dep node or that the dep node has already been marked red.
606             // Either way, we can't call `dep_graph.read()` as we don't have the
607             // DepNodeIndex. We must invoke the query itself. The performance cost
608             // this introduces should be negligible as we'll immediately hit the
609             // in-memory cache, or another query down the line will.
610
611             let _ = self.get_query::<Q>(DUMMY_SP, key);
612         } else {
613             profq_msg!(self, ProfileQueriesMsg::CacheHit);
614             self.sess.profiler(|p| p.record_query_hit(Q::NAME, Q::CATEGORY));
615         }
616     }
617
618     #[allow(dead_code)]
619     fn force_query<Q: QueryDescription<'gcx>>(
620         self,
621         key: Q::Key,
622         span: Span,
623         dep_node: DepNode
624     ) {
625         profq_msg!(
626             self,
627             ProfileQueriesMsg::QueryBegin(span.data(), profq_query_msg!(Q::NAME, self, key))
628         );
629
630         // We may be concurrently trying both execute and force a query
631         // Ensure that only one of them runs the query
632         let job = match JobOwner::try_get(self, span, &key) {
633             TryGetJob::NotYetStarted(job) => job,
634             TryGetJob::JobCompleted(result) => {
635                 if let Err(e) = result {
636                     self.report_cycle(e).emit();
637                 }
638                 return
639             }
640         };
641         self.force_query_with_job::<Q>(key, job, dep_node);
642     }
643
644     pub(super) fn try_get_query<Q: QueryDescription<'gcx>>(
645         self,
646         span: Span,
647         key: Q::Key,
648     ) -> Result<Q::Value, Box<DiagnosticBuilder<'a>>> {
649         match self.try_get_with::<Q>(span, key) {
650             Ok(e) => Ok(e),
651             Err(e) => Err(self.report_cycle(e)),
652         }
653     }
654
655     // FIXME: Try uninlining this
656     #[inline(always)]
657     pub(super) fn get_query<Q: QueryDescription<'gcx>>(
658         self,
659         span: Span,
660         key: Q::Key,
661     ) -> Q::Value {
662         self.try_get_with::<Q>(span, key).unwrap_or_else(|e| {
663             self.emit_error::<Q>(e)
664         })
665     }
666
667     #[inline(never)]
668     #[cold]
669     fn emit_error<Q: QueryDescription<'gcx>>(
670         self,
671         e: Box<CycleError<'gcx>>,
672     ) -> Q::Value {
673         self.report_cycle(e).emit();
674         Q::handle_cycle_error(self)
675     }
676 }
677
678 macro_rules! handle_cycle_error {
679     ([][$this: expr]) => {{
680         Value::from_cycle_error($this.global_tcx())
681     }};
682     ([fatal_cycle$(, $modifiers:ident)*][$this:expr]) => {{
683         $this.sess.abort_if_errors();
684         unreachable!();
685     }};
686     ([$other:ident$(, $modifiers:ident)*][$($args:tt)*]) => {
687         handle_cycle_error!([$($modifiers),*][$($args)*])
688     };
689 }
690
691 macro_rules! hash_result {
692     ([][$hcx:expr, $result:expr]) => {{
693         dep_graph::hash_result($hcx, &$result)
694     }};
695     ([no_hash$(, $modifiers:ident)*][$hcx:expr, $result:expr]) => {{
696         None
697     }};
698     ([$other:ident$(, $modifiers:ident)*][$($args:tt)*]) => {
699         hash_result!([$($modifiers),*][$($args)*])
700     };
701 }
702
703 macro_rules! define_queries {
704     (<$tcx:tt> $($category:tt {
705         $($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident: $node:ident($K:ty) -> $V:ty,)*
706     },)*) => {
707         define_queries_inner! { <$tcx>
708             $($( $(#[$attr])* category<$category> [$($modifiers)*] fn $name: $node($K) -> $V,)*)*
709         }
710     }
711 }
712
713 macro_rules! define_queries_inner {
714     (<$tcx:tt>
715      $($(#[$attr:meta])* category<$category:tt>
716         [$($modifiers:tt)*] fn $name:ident: $node:ident($K:ty) -> $V:ty,)*) => {
717
718         use std::mem;
719         #[cfg(parallel_compiler)]
720         use ty::query::job::QueryResult;
721         use rustc_data_structures::sync::Lock;
722         use crate::{
723             rustc_data_structures::stable_hasher::HashStable,
724             rustc_data_structures::stable_hasher::StableHasherResult,
725             rustc_data_structures::stable_hasher::StableHasher,
726             ich::StableHashingContext
727         };
728         use crate::util::profiling::ProfileCategory;
729
730         define_queries_struct! {
731             tcx: $tcx,
732             input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
733         }
734
735         impl<$tcx> Queries<$tcx> {
736             pub fn new(
737                 providers: IndexVec<CrateNum, Providers<$tcx>>,
738                 fallback_extern_providers: Providers<$tcx>,
739                 on_disk_cache: OnDiskCache<'tcx>,
740             ) -> Self {
741                 Queries {
742                     providers,
743                     fallback_extern_providers: Box::new(fallback_extern_providers),
744                     on_disk_cache,
745                     $($name: Default::default()),*
746                 }
747             }
748
749             pub fn record_computed_queries(&self, sess: &Session) {
750                 sess.profiler(|p| {
751                     $(
752                         p.record_computed_queries(
753                             <queries::$name<'_> as QueryConfig<'_>>::NAME,
754                             <queries::$name<'_> as QueryConfig<'_>>::CATEGORY,
755                             self.$name.lock().results.len()
756                         );
757                     )*
758                 });
759             }
760
761             #[cfg(parallel_compiler)]
762             pub fn collect_active_jobs(&self) -> Vec<Lrc<QueryJob<$tcx>>> {
763                 let mut jobs = Vec::new();
764
765                 // We use try_lock here since we are only called from the
766                 // deadlock handler, and this shouldn't be locked
767                 $(
768                     jobs.extend(
769                         self.$name.try_lock().unwrap().active.values().filter_map(|v|
770                             if let QueryResult::Started(ref job) = *v {
771                                 Some(job.clone())
772                             } else {
773                                 None
774                             }
775                         )
776                     );
777                 )*
778
779                 jobs
780             }
781
782             pub fn print_stats(&self) {
783                 let mut queries = Vec::new();
784
785                 #[derive(Clone)]
786                 struct QueryStats {
787                     name: &'static str,
788                     cache_hits: usize,
789                     key_size: usize,
790                     key_type: &'static str,
791                     value_size: usize,
792                     value_type: &'static str,
793                     entry_count: usize,
794                 }
795
796                 fn stats<'tcx, Q: QueryConfig<'tcx>>(
797                     name: &'static str,
798                     map: &QueryCache<'tcx, Q>
799                 ) -> QueryStats {
800                     QueryStats {
801                         name,
802                         #[cfg(debug_assertions)]
803                         cache_hits: map.cache_hits,
804                         #[cfg(not(debug_assertions))]
805                         cache_hits: 0,
806                         key_size: mem::size_of::<Q::Key>(),
807                         key_type: unsafe { type_name::<Q::Key>() },
808                         value_size: mem::size_of::<Q::Value>(),
809                         value_type: unsafe { type_name::<Q::Value>() },
810                         entry_count: map.results.len(),
811                     }
812                 }
813
814                 $(
815                     queries.push(stats::<queries::$name<'_>>(
816                         stringify!($name),
817                         &*self.$name.lock()
818                     ));
819                 )*
820
821                 if cfg!(debug_assertions) {
822                     let hits: usize = queries.iter().map(|s| s.cache_hits).sum();
823                     let results: usize = queries.iter().map(|s| s.entry_count).sum();
824                     println!("\nQuery cache hit rate: {}", hits as f64 / (hits + results) as f64);
825                 }
826
827                 let mut query_key_sizes = queries.clone();
828                 query_key_sizes.sort_by_key(|q| q.key_size);
829                 println!("\nLarge query keys:");
830                 for q in query_key_sizes.iter().rev()
831                                         .filter(|q| q.key_size > 8) {
832                     println!(
833                         "   {} - {} x {} - {}",
834                         q.name,
835                         q.key_size,
836                         q.entry_count,
837                         q.key_type
838                     );
839                 }
840
841                 let mut query_value_sizes = queries.clone();
842                 query_value_sizes.sort_by_key(|q| q.value_size);
843                 println!("\nLarge query values:");
844                 for q in query_value_sizes.iter().rev()
845                                           .filter(|q| q.value_size > 8) {
846                     println!(
847                         "   {} - {} x {} - {}",
848                         q.name,
849                         q.value_size,
850                         q.entry_count,
851                         q.value_type
852                     );
853                 }
854
855                 if cfg!(debug_assertions) {
856                     let mut query_cache_hits = queries.clone();
857                     query_cache_hits.sort_by_key(|q| q.cache_hits);
858                     println!("\nQuery cache hits:");
859                     for q in query_cache_hits.iter().rev() {
860                         println!(
861                             "   {} - {} ({}%)",
862                             q.name,
863                             q.cache_hits,
864                             q.cache_hits as f64 / (q.cache_hits + q.entry_count) as f64
865                         );
866                     }
867                 }
868
869                 let mut query_value_count = queries.clone();
870                 query_value_count.sort_by_key(|q| q.entry_count);
871                 println!("\nQuery value count:");
872                 for q in query_value_count.iter().rev() {
873                     println!("   {} - {}", q.name, q.entry_count);
874                 }
875             }
876         }
877
878         #[allow(nonstandard_style)]
879         #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
880         pub enum Query<$tcx> {
881             $($(#[$attr])* $name($K)),*
882         }
883
884         impl<$tcx> Query<$tcx> {
885             pub fn name(&self) -> &'static str {
886                 match *self {
887                     $(Query::$name(_) => stringify!($name),)*
888                 }
889             }
890
891             pub fn describe(&self, tcx: TyCtxt<'_, '_, '_>) -> Cow<'static, str> {
892                 let (r, name) = match *self {
893                     $(Query::$name(key) => {
894                         (queries::$name::describe(tcx, key), stringify!($name))
895                     })*
896                 };
897                 if tcx.sess.verbose() {
898                     format!("{} [{}]", r, name).into()
899                 } else {
900                     r
901                 }
902             }
903
904             // FIXME(eddyb) Get more valid Span's on queries.
905             pub fn default_span(&self, tcx: TyCtxt<'_, $tcx, '_>, span: Span) -> Span {
906                 if !span.is_dummy() {
907                     return span;
908                 }
909                 // The def_span query is used to calculate default_span,
910                 // so exit to avoid infinite recursion
911                 if let Query::def_span(..) = *self {
912                     return span
913                 }
914                 match *self {
915                     $(Query::$name(key) => key.default_span(tcx),)*
916                 }
917             }
918         }
919
920         impl<'a, $tcx> HashStable<StableHashingContext<'a>> for Query<$tcx> {
921             fn hash_stable<W: StableHasherResult>(&self,
922                                                 hcx: &mut StableHashingContext<'a>,
923                                                 hasher: &mut StableHasher<W>) {
924                 mem::discriminant(self).hash_stable(hcx, hasher);
925                 match *self {
926                     $(Query::$name(key) => key.hash_stable(hcx, hasher),)*
927                 }
928             }
929         }
930
931         pub mod queries {
932             use std::marker::PhantomData;
933
934             $(#[allow(nonstandard_style)]
935             pub struct $name<$tcx> {
936                 data: PhantomData<&$tcx ()>
937             })*
938         }
939
940         // This module and the functions in it exist only to provide a
941         // predictable symbol name prefix for query providers. This is helpful
942         // for analyzing queries in profilers.
943         pub(super) mod __query_compute {
944             $(#[inline(never)]
945             pub fn $name<F: FnOnce() -> R, R>(f: F) -> R {
946                 f()
947             })*
948         }
949
950         $(impl<$tcx> QueryConfig<$tcx> for queries::$name<$tcx> {
951             type Key = $K;
952             type Value = $V;
953
954             const NAME: &'static str = stringify!($name);
955             const CATEGORY: ProfileCategory = $category;
956         }
957
958         impl<$tcx> QueryAccessors<$tcx> for queries::$name<$tcx> {
959             #[inline(always)]
960             fn query(key: Self::Key) -> Query<'tcx> {
961                 Query::$name(key)
962             }
963
964             #[inline(always)]
965             fn query_cache<'a>(tcx: TyCtxt<'a, $tcx, '_>) -> &'a Lock<QueryCache<$tcx, Self>> {
966                 &tcx.queries.$name
967             }
968
969             #[allow(unused)]
970             #[inline(always)]
971             fn to_dep_node(tcx: TyCtxt<'_, $tcx, '_>, key: &Self::Key) -> DepNode {
972                 use crate::dep_graph::DepConstructor::*;
973
974                 DepNode::new(tcx, $node(*key))
975             }
976
977             #[inline]
978             fn compute(tcx: TyCtxt<'_, 'tcx, '_>, key: Self::Key) -> Self::Value {
979                 __query_compute::$name(move || {
980                     let provider = tcx.queries.providers.get(key.query_crate())
981                         // HACK(eddyb) it's possible crates may be loaded after
982                         // the query engine is created, and because crate loading
983                         // is not yet integrated with the query engine, such crates
984                         // would be missing appropriate entries in `providers`.
985                         .unwrap_or(&tcx.queries.fallback_extern_providers)
986                         .$name;
987                     provider(tcx.global_tcx(), key)
988                 })
989             }
990
991             fn hash_result(
992                 _hcx: &mut StableHashingContext<'_>,
993                 _result: &Self::Value
994             ) -> Option<Fingerprint> {
995                 hash_result!([$($modifiers)*][_hcx, _result])
996             }
997
998             fn handle_cycle_error(tcx: TyCtxt<'_, 'tcx, '_>) -> Self::Value {
999                 handle_cycle_error!([$($modifiers)*][tcx])
1000             }
1001         })*
1002
1003         #[derive(Copy, Clone)]
1004         pub struct TyCtxtEnsure<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
1005             pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
1006         }
1007
1008         impl<'a, $tcx, 'lcx> TyCtxtEnsure<'a, $tcx, 'lcx> {
1009             $($(#[$attr])*
1010             #[inline(always)]
1011             pub fn $name(self, key: $K) {
1012                 self.tcx.ensure_query::<queries::$name<'_>>(key)
1013             })*
1014         }
1015
1016         #[derive(Copy, Clone)]
1017         pub struct TyCtxtAt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
1018             pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
1019             pub span: Span,
1020         }
1021
1022         impl<'a, 'gcx, 'tcx> Deref for TyCtxtAt<'a, 'gcx, 'tcx> {
1023             type Target = TyCtxt<'a, 'gcx, 'tcx>;
1024             #[inline(always)]
1025             fn deref(&self) -> &Self::Target {
1026                 &self.tcx
1027             }
1028         }
1029
1030         impl<'a, $tcx, 'lcx> TyCtxt<'a, $tcx, 'lcx> {
1031             /// Returns a transparent wrapper for `TyCtxt`, which ensures queries
1032             /// are executed instead of just returing their results.
1033             #[inline(always)]
1034             pub fn ensure(self) -> TyCtxtEnsure<'a, $tcx, 'lcx> {
1035                 TyCtxtEnsure {
1036                     tcx: self,
1037                 }
1038             }
1039
1040             /// Returns a transparent wrapper for `TyCtxt` which uses
1041             /// `span` as the location of queries performed through it.
1042             #[inline(always)]
1043             pub fn at(self, span: Span) -> TyCtxtAt<'a, $tcx, 'lcx> {
1044                 TyCtxtAt {
1045                     tcx: self,
1046                     span
1047                 }
1048             }
1049
1050             $($(#[$attr])*
1051             #[inline(always)]
1052             pub fn $name(self, key: $K) -> $V {
1053                 self.at(DUMMY_SP).$name(key)
1054             })*
1055         }
1056
1057         impl<'a, $tcx, 'lcx> TyCtxtAt<'a, $tcx, 'lcx> {
1058             $($(#[$attr])*
1059             #[inline(always)]
1060             pub fn $name(self, key: $K) -> $V {
1061                 self.tcx.get_query::<queries::$name<'_>>(self.span, key)
1062             })*
1063         }
1064
1065         define_provider_struct! {
1066             tcx: $tcx,
1067             input: ($(([$($modifiers)*] [$name] [$K] [$V]))*)
1068         }
1069
1070         impl<$tcx> Copy for Providers<$tcx> {}
1071         impl<$tcx> Clone for Providers<$tcx> {
1072             fn clone(&self) -> Self { *self }
1073         }
1074     }
1075 }
1076
1077 macro_rules! define_queries_struct {
1078     (tcx: $tcx:tt,
1079      input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
1080         pub struct Queries<$tcx> {
1081             /// This provides access to the incrimental comilation on-disk cache for query results.
1082             /// Do not access this directly. It is only meant to be used by
1083             /// `DepGraph::try_mark_green()` and the query infrastructure.
1084             pub(crate) on_disk_cache: OnDiskCache<'tcx>,
1085
1086             providers: IndexVec<CrateNum, Providers<$tcx>>,
1087             fallback_extern_providers: Box<Providers<$tcx>>,
1088
1089             $($(#[$attr])*  $name: Lock<QueryCache<$tcx, queries::$name<$tcx>>>,)*
1090         }
1091     };
1092 }
1093
1094 macro_rules! define_provider_struct {
1095     (tcx: $tcx:tt,
1096      input: ($(([$($modifiers:tt)*] [$name:ident] [$K:ty] [$R:ty]))*)) => {
1097         pub struct Providers<$tcx> {
1098             $(pub $name: for<'a> fn(TyCtxt<'a, $tcx, $tcx>, $K) -> $R,)*
1099         }
1100
1101         impl<$tcx> Default for Providers<$tcx> {
1102             fn default() -> Self {
1103                 $(fn $name<'a, $tcx>(_: TyCtxt<'a, $tcx, $tcx>, key: $K) -> $R {
1104                     bug!("tcx.{}({:?}) unsupported by its crate",
1105                          stringify!($name), key);
1106                 })*
1107                 Providers { $($name),* }
1108             }
1109         }
1110     };
1111 }
1112
1113
1114 /// The red/green evaluation system will try to mark a specific DepNode in the
1115 /// dependency graph as green by recursively trying to mark the dependencies of
1116 /// that DepNode as green. While doing so, it will sometimes encounter a DepNode
1117 /// where we don't know if it is red or green and we therefore actually have
1118 /// to recompute its value in order to find out. Since the only piece of
1119 /// information that we have at that point is the DepNode we are trying to
1120 /// re-evaluate, we need some way to re-run a query from just that. This is what
1121 /// `force_from_dep_node()` implements.
1122 ///
1123 /// In the general case, a DepNode consists of a DepKind and an opaque
1124 /// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
1125 /// is usually constructed by computing a stable hash of the query-key that the
1126 /// DepNode corresponds to. Consequently, it is not in general possible to go
1127 /// back from hash to query-key (since hash functions are not reversible). For
1128 /// this reason `force_from_dep_node()` is expected to fail from time to time
1129 /// because we just cannot find out, from the DepNode alone, what the
1130 /// corresponding query-key is and therefore cannot re-run the query.
1131 ///
1132 /// The system deals with this case letting `try_mark_green` fail which forces
1133 /// the root query to be re-evaluated.
1134 ///
1135 /// Now, if force_from_dep_node() would always fail, it would be pretty useless.
1136 /// Fortunately, we can use some contextual information that will allow us to
1137 /// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we
1138 /// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a
1139 /// valid `DefPathHash`. Since we also always build a huge table that maps every
1140 /// `DefPathHash` in the current codebase to the corresponding `DefId`, we have
1141 /// everything we need to re-run the query.
1142 ///
1143 /// Take the `mir_validated` query as an example. Like many other queries, it
1144 /// just has a single parameter: the `DefId` of the item it will compute the
1145 /// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode`
1146 /// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
1147 /// is actually a `DefPathHash`, and can therefore just look up the corresponding
1148 /// `DefId` in `tcx.def_path_hash_to_def_id`.
1149 ///
1150 /// When you implement a new query, it will likely have a corresponding new
1151 /// `DepKind`, and you'll have to support it here in `force_from_dep_node()`. As
1152 /// a rule of thumb, if your query takes a `DefId` or `DefIndex` as sole parameter,
1153 /// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
1154 /// add it to the "We don't have enough information to reconstruct..." group in
1155 /// the match below.
1156 pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
1157                                            dep_node: &DepNode)
1158                                            -> bool {
1159     use crate::hir::def_id::LOCAL_CRATE;
1160
1161     // We must avoid ever having to call force_from_dep_node() for a
1162     // DepNode::CodegenUnit:
1163     // Since we cannot reconstruct the query key of a DepNode::CodegenUnit, we
1164     // would always end up having to evaluate the first caller of the
1165     // `codegen_unit` query that *is* reconstructible. This might very well be
1166     // the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
1167     // to re-trigger calling the `codegen_unit` query with the right key. At
1168     // that point we would already have re-done all the work we are trying to
1169     // avoid doing in the first place.
1170     // The solution is simple: Just explicitly call the `codegen_unit` query for
1171     // each CGU, right after partitioning. This way `try_mark_green` will always
1172     // hit the cache instead of having to go through `force_from_dep_node`.
1173     // This assertion makes sure, we actually keep applying the solution above.
1174     debug_assert!(dep_node.kind != DepKind::CodegenUnit,
1175                   "calling force_from_dep_node() on DepKind::CodegenUnit");
1176
1177     if !dep_node.kind.can_reconstruct_query_key() {
1178         return false
1179     }
1180
1181     macro_rules! def_id {
1182         () => {
1183             if let Some(def_id) = dep_node.extract_def_id(tcx) {
1184                 def_id
1185             } else {
1186                 // return from the whole function
1187                 return false
1188             }
1189         }
1190     };
1191
1192     macro_rules! krate {
1193         () => { (def_id!()).krate }
1194     };
1195
1196     macro_rules! force {
1197         ($query:ident, $key:expr) => {
1198             {
1199                 tcx.force_query::<crate::ty::query::queries::$query<'_>>($key, DUMMY_SP, *dep_node);
1200             }
1201         }
1202     };
1203
1204     // FIXME(#45015): We should try move this boilerplate code into a macro
1205     //                somehow.
1206     match dep_node.kind {
1207         // These are inputs that are expected to be pre-allocated and that
1208         // should therefore always be red or green already
1209         DepKind::AllLocalTraitImpls |
1210         DepKind::Krate |
1211         DepKind::CrateMetadata |
1212         DepKind::HirBody |
1213         DepKind::Hir |
1214
1215         // This are anonymous nodes
1216         DepKind::TraitSelect |
1217
1218         // We don't have enough information to reconstruct the query key of
1219         // these
1220         DepKind::IsCopy |
1221         DepKind::IsSized |
1222         DepKind::IsFreeze |
1223         DepKind::NeedsDrop |
1224         DepKind::Layout |
1225         DepKind::ConstEval |
1226         DepKind::ConstEvalRaw |
1227         DepKind::InstanceSymbolName |
1228         DepKind::MirShim |
1229         DepKind::BorrowCheckKrate |
1230         DepKind::Specializes |
1231         DepKind::ImplementationsOfTrait |
1232         DepKind::TypeParamPredicates |
1233         DepKind::CodegenUnit |
1234         DepKind::CompileCodegenUnit |
1235         DepKind::FulfillObligation |
1236         DepKind::VtableMethods |
1237         DepKind::EraseRegionsTy |
1238         DepKind::NormalizeProjectionTy |
1239         DepKind::NormalizeTyAfterErasingRegions |
1240         DepKind::ImpliedOutlivesBounds |
1241         DepKind::DropckOutlives |
1242         DepKind::EvaluateObligation |
1243         DepKind::EvaluateGoal |
1244         DepKind::TypeOpAscribeUserType |
1245         DepKind::TypeOpEq |
1246         DepKind::TypeOpSubtype |
1247         DepKind::TypeOpProvePredicate |
1248         DepKind::TypeOpNormalizeTy |
1249         DepKind::TypeOpNormalizePredicate |
1250         DepKind::TypeOpNormalizePolyFnSig |
1251         DepKind::TypeOpNormalizeFnSig |
1252         DepKind::SubstituteNormalizeAndTestPredicates |
1253         DepKind::MethodAutoderefSteps |
1254         DepKind::InstanceDefSizeEstimate |
1255         DepKind::ProgramClausesForEnv |
1256
1257         // This one should never occur in this context
1258         DepKind::Null => {
1259             bug!("force_from_dep_node() - Encountered {:?}", dep_node)
1260         }
1261
1262         // These are not queries
1263         DepKind::CoherenceCheckTrait |
1264         DepKind::ItemVarianceConstraints => {
1265             return false
1266         }
1267
1268         DepKind::RegionScopeTree => { force!(region_scope_tree, def_id!()); }
1269
1270         DepKind::Coherence => { force!(crate_inherent_impls, LOCAL_CRATE); }
1271         DepKind::CoherenceInherentImplOverlapCheck => {
1272             force!(crate_inherent_impls_overlap_check, LOCAL_CRATE)
1273         },
1274         DepKind::PrivacyAccessLevels => { force!(privacy_access_levels, LOCAL_CRATE); }
1275         DepKind::MirBuilt => { force!(mir_built, def_id!()); }
1276         DepKind::MirConstQualif => { force!(mir_const_qualif, def_id!()); }
1277         DepKind::MirConst => { force!(mir_const, def_id!()); }
1278         DepKind::MirValidated => { force!(mir_validated, def_id!()); }
1279         DepKind::MirOptimized => { force!(optimized_mir, def_id!()); }
1280
1281         DepKind::BorrowCheck => { force!(borrowck, def_id!()); }
1282         DepKind::MirBorrowCheck => { force!(mir_borrowck, def_id!()); }
1283         DepKind::UnsafetyCheckResult => { force!(unsafety_check_result, def_id!()); }
1284         DepKind::UnsafeDeriveOnReprPacked => { force!(unsafe_derive_on_repr_packed, def_id!()); }
1285         DepKind::CheckModAttrs => { force!(check_mod_attrs, def_id!()); }
1286         DepKind::CheckModLoops => { force!(check_mod_loops, def_id!()); }
1287         DepKind::CheckModUnstableApiUsage => { force!(check_mod_unstable_api_usage, def_id!()); }
1288         DepKind::CheckModItemTypes => { force!(check_mod_item_types, def_id!()); }
1289         DepKind::CheckModPrivacy => { force!(check_mod_privacy, def_id!()); }
1290         DepKind::CheckModIntrinsics => { force!(check_mod_intrinsics, def_id!()); }
1291         DepKind::CheckModLiveness => { force!(check_mod_liveness, def_id!()); }
1292         DepKind::CheckModImplWf => { force!(check_mod_impl_wf, def_id!()); }
1293         DepKind::CollectModItemTypes => { force!(collect_mod_item_types, def_id!()); }
1294         DepKind::Reachability => { force!(reachable_set, LOCAL_CRATE); }
1295         DepKind::MirKeys => { force!(mir_keys, LOCAL_CRATE); }
1296         DepKind::CrateVariances => { force!(crate_variances, LOCAL_CRATE); }
1297         DepKind::AssociatedItems => { force!(associated_item, def_id!()); }
1298         DepKind::TypeOfItem => { force!(type_of, def_id!()); }
1299         DepKind::GenericsOfItem => { force!(generics_of, def_id!()); }
1300         DepKind::PredicatesOfItem => { force!(predicates_of, def_id!()); }
1301         DepKind::PredicatesDefinedOnItem => { force!(predicates_defined_on, def_id!()); }
1302         DepKind::ExplicitPredicatesOfItem => { force!(explicit_predicates_of, def_id!()); }
1303         DepKind::InferredOutlivesOf => { force!(inferred_outlives_of, def_id!()); }
1304         DepKind::InferredOutlivesCrate => { force!(inferred_outlives_crate, LOCAL_CRATE); }
1305         DepKind::SuperPredicatesOfItem => { force!(super_predicates_of, def_id!()); }
1306         DepKind::TraitDefOfItem => { force!(trait_def, def_id!()); }
1307         DepKind::AdtDefOfItem => { force!(adt_def, def_id!()); }
1308         DepKind::ImplTraitRef => { force!(impl_trait_ref, def_id!()); }
1309         DepKind::ImplPolarity => { force!(impl_polarity, def_id!()); }
1310         DepKind::Issue33140SelfTy => { force!(issue33140_self_ty, def_id!()); }
1311         DepKind::FnSignature => { force!(fn_sig, def_id!()); }
1312         DepKind::CoerceUnsizedInfo => { force!(coerce_unsized_info, def_id!()); }
1313         DepKind::ItemVariances => { force!(variances_of, def_id!()); }
1314         DepKind::IsConstFn => { force!(is_const_fn_raw, def_id!()); }
1315         DepKind::IsPromotableConstFn => { force!(is_promotable_const_fn, def_id!()); }
1316         DepKind::IsForeignItem => { force!(is_foreign_item, def_id!()); }
1317         DepKind::SizedConstraint => { force!(adt_sized_constraint, def_id!()); }
1318         DepKind::DtorckConstraint => { force!(adt_dtorck_constraint, def_id!()); }
1319         DepKind::AdtDestructor => { force!(adt_destructor, def_id!()); }
1320         DepKind::AssociatedItemDefIds => { force!(associated_item_def_ids, def_id!()); }
1321         DepKind::InherentImpls => { force!(inherent_impls, def_id!()); }
1322         DepKind::TypeckBodiesKrate => { force!(typeck_item_bodies, LOCAL_CRATE); }
1323         DepKind::TypeckTables => { force!(typeck_tables_of, def_id!()); }
1324         DepKind::UsedTraitImports => { force!(used_trait_imports, def_id!()); }
1325         DepKind::HasTypeckTables => { force!(has_typeck_tables, def_id!()); }
1326         DepKind::SymbolName => { force!(def_symbol_name, def_id!()); }
1327         DepKind::SpecializationGraph => { force!(specialization_graph_of, def_id!()); }
1328         DepKind::ObjectSafety => { force!(is_object_safe, def_id!()); }
1329         DepKind::TraitImpls => { force!(trait_impls_of, def_id!()); }
1330         DepKind::CheckMatch => { force!(check_match, def_id!()); }
1331
1332         DepKind::ParamEnv => { force!(param_env, def_id!()); }
1333         DepKind::Environment => { force!(environment, def_id!()); }
1334         DepKind::DescribeDef => { force!(describe_def, def_id!()); }
1335         DepKind::DefSpan => { force!(def_span, def_id!()); }
1336         DepKind::LookupStability => { force!(lookup_stability, def_id!()); }
1337         DepKind::LookupDeprecationEntry => {
1338             force!(lookup_deprecation_entry, def_id!());
1339         }
1340         DepKind::ConstIsRvaluePromotableToStatic => {
1341             force!(const_is_rvalue_promotable_to_static, def_id!());
1342         }
1343         DepKind::RvaluePromotableMap => { force!(rvalue_promotable_map, def_id!()); }
1344         DepKind::ImplParent => { force!(impl_parent, def_id!()); }
1345         DepKind::TraitOfItem => { force!(trait_of_item, def_id!()); }
1346         DepKind::IsReachableNonGeneric => { force!(is_reachable_non_generic, def_id!()); }
1347         DepKind::IsUnreachableLocalDefinition => {
1348             force!(is_unreachable_local_definition, def_id!());
1349         }
1350         DepKind::IsMirAvailable => { force!(is_mir_available, def_id!()); }
1351         DepKind::ItemAttrs => { force!(item_attrs, def_id!()); }
1352         DepKind::CodegenFnAttrs => { force!(codegen_fn_attrs, def_id!()); }
1353         DepKind::FnArgNames => { force!(fn_arg_names, def_id!()); }
1354         DepKind::RenderedConst => { force!(rendered_const, def_id!()); }
1355         DepKind::DylibDepFormats => { force!(dylib_dependency_formats, krate!()); }
1356         DepKind::IsPanicRuntime => { force!(is_panic_runtime, krate!()); }
1357         DepKind::IsCompilerBuiltins => { force!(is_compiler_builtins, krate!()); }
1358         DepKind::HasGlobalAllocator => { force!(has_global_allocator, krate!()); }
1359         DepKind::HasPanicHandler => { force!(has_panic_handler, krate!()); }
1360         DepKind::ExternCrate => { force!(extern_crate, def_id!()); }
1361         DepKind::LintLevels => { force!(lint_levels, LOCAL_CRATE); }
1362         DepKind::InScopeTraits => { force!(in_scope_traits_map, def_id!().index); }
1363         DepKind::ModuleExports => { force!(module_exports, def_id!()); }
1364         DepKind::IsSanitizerRuntime => { force!(is_sanitizer_runtime, krate!()); }
1365         DepKind::IsProfilerRuntime => { force!(is_profiler_runtime, krate!()); }
1366         DepKind::GetPanicStrategy => { force!(panic_strategy, krate!()); }
1367         DepKind::IsNoBuiltins => { force!(is_no_builtins, krate!()); }
1368         DepKind::ImplDefaultness => { force!(impl_defaultness, def_id!()); }
1369         DepKind::CheckItemWellFormed => { force!(check_item_well_formed, def_id!()); }
1370         DepKind::CheckTraitItemWellFormed => { force!(check_trait_item_well_formed, def_id!()); }
1371         DepKind::CheckImplItemWellFormed => { force!(check_impl_item_well_formed, def_id!()); }
1372         DepKind::ReachableNonGenerics => { force!(reachable_non_generics, krate!()); }
1373         DepKind::NativeLibraries => { force!(native_libraries, krate!()); }
1374         DepKind::EntryFn => { force!(entry_fn, krate!()); }
1375         DepKind::PluginRegistrarFn => { force!(plugin_registrar_fn, krate!()); }
1376         DepKind::ProcMacroDeclsStatic => { force!(proc_macro_decls_static, krate!()); }
1377         DepKind::CrateDisambiguator => { force!(crate_disambiguator, krate!()); }
1378         DepKind::CrateHash => { force!(crate_hash, krate!()); }
1379         DepKind::OriginalCrateName => { force!(original_crate_name, krate!()); }
1380         DepKind::ExtraFileName => { force!(extra_filename, krate!()); }
1381
1382         DepKind::AllTraitImplementations => {
1383             force!(all_trait_implementations, krate!());
1384         }
1385
1386         DepKind::DllimportForeignItems => {
1387             force!(dllimport_foreign_items, krate!());
1388         }
1389         DepKind::IsDllimportForeignItem => {
1390             force!(is_dllimport_foreign_item, def_id!());
1391         }
1392         DepKind::IsStaticallyIncludedForeignItem => {
1393             force!(is_statically_included_foreign_item, def_id!());
1394         }
1395         DepKind::NativeLibraryKind => { force!(native_library_kind, def_id!()); }
1396         DepKind::LinkArgs => { force!(link_args, LOCAL_CRATE); }
1397
1398         DepKind::ResolveLifetimes => { force!(resolve_lifetimes, krate!()); }
1399         DepKind::NamedRegion => { force!(named_region_map, def_id!().index); }
1400         DepKind::IsLateBound => { force!(is_late_bound_map, def_id!().index); }
1401         DepKind::ObjectLifetimeDefaults => {
1402             force!(object_lifetime_defaults_map, def_id!().index);
1403         }
1404
1405         DepKind::Visibility => { force!(visibility, def_id!()); }
1406         DepKind::DepKind => { force!(dep_kind, krate!()); }
1407         DepKind::CrateName => { force!(crate_name, krate!()); }
1408         DepKind::ItemChildren => { force!(item_children, def_id!()); }
1409         DepKind::ExternModStmtCnum => { force!(extern_mod_stmt_cnum, def_id!()); }
1410         DepKind::GetLibFeatures => { force!(get_lib_features, LOCAL_CRATE); }
1411         DepKind::DefinedLibFeatures => { force!(defined_lib_features, krate!()); }
1412         DepKind::GetLangItems => { force!(get_lang_items, LOCAL_CRATE); }
1413         DepKind::DefinedLangItems => { force!(defined_lang_items, krate!()); }
1414         DepKind::MissingLangItems => { force!(missing_lang_items, krate!()); }
1415         DepKind::VisibleParentMap => { force!(visible_parent_map, LOCAL_CRATE); }
1416         DepKind::MissingExternCrateItem => {
1417             force!(missing_extern_crate_item, krate!());
1418         }
1419         DepKind::UsedCrateSource => { force!(used_crate_source, krate!()); }
1420         DepKind::PostorderCnums => { force!(postorder_cnums, LOCAL_CRATE); }
1421
1422         DepKind::Freevars => { force!(freevars, def_id!()); }
1423         DepKind::MaybeUnusedTraitImport => {
1424             force!(maybe_unused_trait_import, def_id!());
1425         }
1426         DepKind::NamesImportedByGlobUse => { force!(names_imported_by_glob_use, def_id!()); }
1427         DepKind::MaybeUnusedExternCrates => { force!(maybe_unused_extern_crates, LOCAL_CRATE); }
1428         DepKind::StabilityIndex => { force!(stability_index, LOCAL_CRATE); }
1429         DepKind::AllTraits => { force!(all_traits, LOCAL_CRATE); }
1430         DepKind::AllCrateNums => { force!(all_crate_nums, LOCAL_CRATE); }
1431         DepKind::ExportedSymbols => { force!(exported_symbols, krate!()); }
1432         DepKind::CollectAndPartitionMonoItems => {
1433             force!(collect_and_partition_mono_items, LOCAL_CRATE);
1434         }
1435         DepKind::IsCodegenedItem => { force!(is_codegened_item, def_id!()); }
1436         DepKind::OutputFilenames => { force!(output_filenames, LOCAL_CRATE); }
1437
1438         DepKind::TargetFeaturesWhitelist => { force!(target_features_whitelist, LOCAL_CRATE); }
1439
1440         DepKind::Features => { force!(features_query, LOCAL_CRATE); }
1441
1442         DepKind::ProgramClausesFor => { force!(program_clauses_for, def_id!()); }
1443         DepKind::WasmImportModuleMap => { force!(wasm_import_module_map, krate!()); }
1444         DepKind::ForeignModules => { force!(foreign_modules, krate!()); }
1445
1446         DepKind::UpstreamMonomorphizations => {
1447             force!(upstream_monomorphizations, krate!());
1448         }
1449         DepKind::UpstreamMonomorphizationsFor => {
1450             force!(upstream_monomorphizations_for, def_id!());
1451         }
1452         DepKind::BackendOptimizationLevel => {
1453             force!(backend_optimization_level, krate!());
1454         }
1455     }
1456
1457     true
1458 }
1459
1460
1461 // FIXME(#45015): Another piece of boilerplate code that could be generated in
1462 //                a combined define_dep_nodes!()/define_queries!() macro.
1463 macro_rules! impl_load_from_cache {
1464     ($($dep_kind:ident => $query_name:ident,)*) => {
1465         impl DepNode {
1466             // Check whether the query invocation corresponding to the given
1467             // DepNode is eligible for on-disk-caching.
1468             pub fn cache_on_disk(&self, tcx: TyCtxt<'_, '_, '_>) -> bool {
1469                 use crate::ty::query::queries;
1470                 use crate::ty::query::QueryDescription;
1471
1472                 match self.kind {
1473                     $(DepKind::$dep_kind => {
1474                         let def_id = self.extract_def_id(tcx).unwrap();
1475                         queries::$query_name::cache_on_disk(tcx.global_tcx(), def_id)
1476                     })*
1477                     _ => false
1478                 }
1479             }
1480
1481             // This is method will execute the query corresponding to the given
1482             // DepNode. It is only expected to work for DepNodes where the
1483             // above `cache_on_disk` methods returns true.
1484             // Also, as a sanity check, it expects that the corresponding query
1485             // invocation has been marked as green already.
1486             pub fn load_from_on_disk_cache(&self, tcx: TyCtxt<'_, '_, '_>) {
1487                 match self.kind {
1488                     $(DepKind::$dep_kind => {
1489                         debug_assert!(tcx.dep_graph
1490                                          .node_color(self)
1491                                          .map(|c| c.is_green())
1492                                          .unwrap_or(false));
1493
1494                         let def_id = self.extract_def_id(tcx).unwrap();
1495                         let _ = tcx.$query_name(def_id);
1496                     })*
1497                     _ => {
1498                         bug!()
1499                     }
1500                 }
1501             }
1502         }
1503     }
1504 }
1505
1506 impl_load_from_cache!(
1507     TypeckTables => typeck_tables_of,
1508     MirOptimized => optimized_mir,
1509     UnsafetyCheckResult => unsafety_check_result,
1510     BorrowCheck => borrowck,
1511     MirBorrowCheck => mir_borrowck,
1512     MirConstQualif => mir_const_qualif,
1513     SymbolName => def_symbol_name,
1514     ConstIsRvaluePromotableToStatic => const_is_rvalue_promotable_to_static,
1515     CheckMatch => check_match,
1516     TypeOfItem => type_of,
1517     GenericsOfItem => generics_of,
1518     PredicatesOfItem => predicates_of,
1519     UsedTraitImports => used_trait_imports,
1520     CodegenFnAttrs => codegen_fn_attrs,
1521     SpecializationGraph => specialization_graph_of,
1522 );