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