]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/maps/plumbing.rs
Rename is_translated_fn query to is_translated_item and make it support statics.
[rust.git] / src / librustc / ty / maps / 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 ty::{TyCtxt};
18 use ty::maps::Query; // NB: actually generated by the macros in this file
19 use ty::maps::config::QueryDescription;
20 use ty::item_path;
21
22 use rustc_data_structures::fx::{FxHashMap};
23 use std::cell::{Ref, RefMut};
24 use std::marker::PhantomData;
25 use std::mem;
26 use syntax_pos::Span;
27
28 pub(super) struct QueryMap<'tcx, D: QueryDescription<'tcx>> {
29     phantom: PhantomData<(D, &'tcx ())>,
30     pub(super) map: FxHashMap<D::Key, QueryValue<D::Value>>,
31 }
32
33 pub(super) struct QueryValue<T> {
34     pub(super) value: T,
35     pub(super) index: DepNodeIndex,
36 }
37
38 impl<T> QueryValue<T> {
39     pub(super) fn new(value: T,
40                       dep_node_index: DepNodeIndex)
41                       -> QueryValue<T> {
42         QueryValue {
43             value,
44             index: dep_node_index,
45         }
46     }
47 }
48
49 impl<'tcx, M: QueryDescription<'tcx>> QueryMap<'tcx, M> {
50     pub(super) fn new() -> QueryMap<'tcx, M> {
51         QueryMap {
52             phantom: PhantomData,
53             map: FxHashMap(),
54         }
55     }
56 }
57
58 pub(super) trait GetCacheInternal<'tcx>: QueryDescription<'tcx> + Sized {
59     fn get_cache_internal<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
60                               -> Ref<'a, QueryMap<'tcx, Self>>;
61 }
62
63 pub(super) struct CycleError<'a, 'tcx: 'a> {
64     span: Span,
65     cycle: RefMut<'a, [(Span, Query<'tcx>)]>,
66 }
67
68 impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
69     pub(super) fn report_cycle(self, CycleError { span, cycle }: CycleError)
70         -> DiagnosticBuilder<'a>
71     {
72         // Subtle: release the refcell lock before invoking `describe()`
73         // below by dropping `cycle`.
74         let stack = cycle.to_vec();
75         mem::drop(cycle);
76
77         assert!(!stack.is_empty());
78
79         // Disable naming impls with types in this path, since that
80         // sometimes cycles itself, leading to extra cycle errors.
81         // (And cycle errors around impls tend to occur during the
82         // collect/coherence phases anyhow.)
83         item_path::with_forced_impl_filename_line(|| {
84             let span = self.sess.codemap().def_span(span);
85             let mut err =
86                 struct_span_err!(self.sess, span, E0391,
87                                  "unsupported cyclic reference between types/traits detected");
88             err.span_label(span, "cyclic reference");
89
90             err.span_note(self.sess.codemap().def_span(stack[0].0),
91                           &format!("the cycle begins when {}...", stack[0].1.describe(self)));
92
93             for &(span, ref query) in &stack[1..] {
94                 err.span_note(self.sess.codemap().def_span(span),
95                               &format!("...which then requires {}...", query.describe(self)));
96             }
97
98             err.note(&format!("...which then again requires {}, completing the cycle.",
99                               stack[0].1.describe(self)));
100
101             return err
102         })
103     }
104
105     pub(super) fn cycle_check<F, R>(self, span: Span, query: Query<'gcx>, compute: F)
106                                     -> Result<R, CycleError<'a, 'gcx>>
107         where F: FnOnce() -> R
108     {
109         {
110             let mut stack = self.maps.query_stack.borrow_mut();
111             if let Some((i, _)) = stack.iter().enumerate().rev()
112                                        .find(|&(_, &(_, ref q))| *q == query) {
113                 return Err(CycleError {
114                     span,
115                     cycle: RefMut::map(stack, |stack| &mut stack[i..])
116                 });
117             }
118             stack.push((span, query));
119         }
120
121         let result = compute();
122
123         self.maps.query_stack.borrow_mut().pop();
124
125         Ok(result)
126     }
127
128     /// Try to read a node index for the node dep_node.
129     /// A node will have an index, when it's already been marked green, or when we can mark it
130     /// green. This function will mark the current task as a reader of the specified node, when
131     /// the a node index can be found for that node.
132     pub(super) fn try_mark_green_and_read(self, dep_node: &DepNode) -> Option<DepNodeIndex> {
133         match self.dep_graph.node_color(dep_node) {
134             Some(DepNodeColor::Green(dep_node_index)) => {
135                 self.dep_graph.read_index(dep_node_index);
136                 Some(dep_node_index)
137             }
138             Some(DepNodeColor::Red) => {
139                 None
140             }
141             None => {
142                 // try_mark_green (called below) will panic when full incremental
143                 // compilation is disabled. If that's the case, we can't try to mark nodes
144                 // as green anyway, so we can safely return None here.
145                 if !self.dep_graph.is_fully_enabled() {
146                     return None;
147                 }
148                 match self.dep_graph.try_mark_green(self.global_tcx(), &dep_node) {
149                     Some(dep_node_index) => {
150                         debug_assert!(self.dep_graph.is_green(dep_node_index));
151                         self.dep_graph.read_index(dep_node_index);
152                         Some(dep_node_index)
153                     }
154                     None => {
155                         None
156                     }
157                 }
158             }
159         }
160     }
161 }
162
163 // If enabled, send a message to the profile-queries thread
164 macro_rules! profq_msg {
165     ($tcx:expr, $msg:expr) => {
166         if cfg!(debug_assertions) {
167             if  $tcx.sess.profile_queries() {
168                 profq_msg($msg)
169             }
170         }
171     }
172 }
173
174 // If enabled, format a key using its debug string, which can be
175 // expensive to compute (in terms of time).
176 macro_rules! profq_key {
177     ($tcx:expr, $key:expr) => {
178         if cfg!(debug_assertions) {
179             if $tcx.sess.profile_queries_and_keys() {
180                 Some(format!("{:?}", $key))
181             } else { None }
182         } else { None }
183     }
184 }
185
186 macro_rules! handle_cycle_error {
187     ([][$this: expr]) => {{
188         Value::from_cycle_error($this.global_tcx())
189     }};
190     ([fatal_cycle$(, $modifiers:ident)*][$this:expr]) => {{
191         $this.tcx.sess.abort_if_errors();
192         unreachable!();
193     }};
194     ([$other:ident$(, $modifiers:ident)*][$($args:tt)*]) => {
195         handle_cycle_error!([$($modifiers),*][$($args)*])
196     };
197 }
198
199 macro_rules! define_maps {
200     (<$tcx:tt>
201      $($(#[$attr:meta])*
202        [$($modifiers:tt)*] fn $name:ident: $node:ident($K:ty) -> $V:ty,)*) => {
203
204         use dep_graph::DepNodeIndex;
205         use std::cell::RefCell;
206
207         define_map_struct! {
208             tcx: $tcx,
209             input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
210         }
211
212         impl<$tcx> Maps<$tcx> {
213             pub fn new(providers: IndexVec<CrateNum, Providers<$tcx>>)
214                        -> Self {
215                 Maps {
216                     providers,
217                     query_stack: RefCell::new(vec![]),
218                     $($name: RefCell::new(QueryMap::new())),*
219                 }
220             }
221         }
222
223         #[allow(bad_style)]
224         #[derive(Copy, Clone, Debug, PartialEq, Eq)]
225         pub enum Query<$tcx> {
226             $($(#[$attr])* $name($K)),*
227         }
228
229         #[allow(bad_style)]
230         #[derive(Clone, Debug, PartialEq, Eq)]
231         pub enum QueryMsg {
232             $($name(Option<String>)),*
233         }
234
235         impl<$tcx> Query<$tcx> {
236             pub fn describe(&self, tcx: TyCtxt) -> String {
237                 let (r, name) = match *self {
238                     $(Query::$name(key) => {
239                         (queries::$name::describe(tcx, key), stringify!($name))
240                     })*
241                 };
242                 if tcx.sess.verbose() {
243                     format!("{} [{}]", r, name)
244                 } else {
245                     r
246                 }
247             }
248         }
249
250         pub mod queries {
251             use std::marker::PhantomData;
252
253             $(#[allow(bad_style)]
254             pub struct $name<$tcx> {
255                 data: PhantomData<&$tcx ()>
256             })*
257         }
258
259         $(impl<$tcx> QueryConfig for queries::$name<$tcx> {
260             type Key = $K;
261             type Value = $V;
262         }
263
264         impl<$tcx> GetCacheInternal<$tcx> for queries::$name<$tcx> {
265             fn get_cache_internal<'a>(tcx: TyCtxt<'a, $tcx, $tcx>)
266                                       -> ::std::cell::Ref<'a, QueryMap<$tcx, Self>> {
267                 tcx.maps.$name.borrow()
268             }
269         }
270
271         impl<'a, $tcx, 'lcx> queries::$name<$tcx> {
272
273             #[allow(unused)]
274             fn to_dep_node(tcx: TyCtxt<'a, $tcx, 'lcx>, key: &$K) -> DepNode {
275                 use dep_graph::DepConstructor::*;
276
277                 DepNode::new(tcx, $node(*key))
278             }
279
280             fn try_get_with(tcx: TyCtxt<'a, $tcx, 'lcx>,
281                             mut span: Span,
282                             key: $K)
283                             -> Result<$V, CycleError<'a, $tcx>>
284             {
285                 debug!("ty::queries::{}::try_get_with(key={:?}, span={:?})",
286                        stringify!($name),
287                        key,
288                        span);
289
290                 profq_msg!(tcx,
291                     ProfileQueriesMsg::QueryBegin(
292                         span.data(),
293                         QueryMsg::$name(profq_key!(tcx, key))
294                     )
295                 );
296
297                 if let Some(value) = tcx.maps.$name.borrow().map.get(&key) {
298                     profq_msg!(tcx, ProfileQueriesMsg::CacheHit);
299                     tcx.dep_graph.read_index(value.index);
300                     return Ok((&value.value).clone());
301                 }
302
303                 // FIXME(eddyb) Get more valid Span's on queries.
304                 // def_span guard is necessary to prevent a recursive loop,
305                 // default_span calls def_span query internally.
306                 if span == DUMMY_SP && stringify!($name) != "def_span" {
307                     span = key.default_span(tcx)
308                 }
309
310                 // Fast path for when incr. comp. is off. `to_dep_node` is
311                 // expensive for some DepKinds.
312                 if !tcx.dep_graph.is_fully_enabled() {
313                     let null_dep_node = DepNode::new_no_params(::dep_graph::DepKind::Null);
314                     return Self::force(tcx, key, span, null_dep_node)
315                                 .map(|(v, _)| v);
316                 }
317
318                 let dep_node = Self::to_dep_node(tcx, &key);
319
320                 if dep_node.kind.is_anon() {
321                     profq_msg!(tcx, ProfileQueriesMsg::ProviderBegin);
322
323                     let res = tcx.cycle_check(span, Query::$name(key), || {
324                         tcx.sess.diagnostic().track_diagnostics(|| {
325                             tcx.dep_graph.with_anon_task(dep_node.kind, || {
326                                 Self::compute_result(tcx.global_tcx(), key)
327                             })
328                         })
329                     })?;
330
331                     profq_msg!(tcx, ProfileQueriesMsg::ProviderEnd);
332                     let ((result, dep_node_index), diagnostics) = res;
333
334                     tcx.dep_graph.read_index(dep_node_index);
335
336                     tcx.on_disk_query_result_cache
337                        .store_diagnostics_for_anon_node(dep_node_index, diagnostics);
338
339                     let value = QueryValue::new(result, dep_node_index);
340
341                     return Ok((&tcx.maps
342                                     .$name
343                                     .borrow_mut()
344                                     .map
345                                     .entry(key)
346                                     .or_insert(value)
347                                     .value).clone());
348                 }
349
350                 if !dep_node.kind.is_input() {
351                     if let Some(dep_node_index) = tcx.try_mark_green_and_read(&dep_node) {
352                         profq_msg!(tcx, ProfileQueriesMsg::CacheHit);
353                         return Self::load_from_disk_and_cache_in_memory(tcx,
354                                                                         key,
355                                                                         span,
356                                                                         dep_node_index,
357                                                                         &dep_node)
358                     }
359                 }
360
361                 match Self::force(tcx, key, span, dep_node) {
362                     Ok((result, dep_node_index)) => {
363                         tcx.dep_graph.read_index(dep_node_index);
364                         Ok(result)
365                     }
366                     Err(e) => Err(e)
367                 }
368             }
369
370             /// Ensure that either this query has all green inputs or been executed.
371             /// Executing query::ensure(D) is considered a read of the dep-node D.
372             ///
373             /// This function is particularly useful when executing passes for their
374             /// side-effects -- e.g., in order to report errors for erroneous programs.
375             ///
376             /// Note: The optimization is only available during incr. comp.
377             pub fn ensure(tcx: TyCtxt<'a, $tcx, 'lcx>, key: $K) -> () {
378                 let dep_node = Self::to_dep_node(tcx, &key);
379
380                 // Ensuring an "input" or anonymous query makes no sense
381                 assert!(!dep_node.kind.is_anon());
382                 assert!(!dep_node.kind.is_input());
383                 if tcx.try_mark_green_and_read(&dep_node).is_none() {
384                     // A None return from `try_mark_green_and_read` means that this is either
385                     // a new dep node or that the dep node has already been marked red.
386                     // Either way, we can't call `dep_graph.read()` as we don't have the
387                     // DepNodeIndex. We must invoke the query itself. The performance cost
388                     // this introduces should be negligible as we'll immediately hit the
389                     // in-memory cache, or another query down the line will.
390                     let _ = tcx.$name(key);
391                 }
392             }
393
394             fn compute_result(tcx: TyCtxt<'a, $tcx, 'lcx>, key: $K) -> $V {
395                 let provider = tcx.maps.providers[key.map_crate()].$name;
396                 provider(tcx.global_tcx(), key)
397             }
398
399             fn load_from_disk_and_cache_in_memory(tcx: TyCtxt<'a, $tcx, 'lcx>,
400                                                   key: $K,
401                                                   span: Span,
402                                                   dep_node_index: DepNodeIndex,
403                                                   dep_node: &DepNode)
404                                                   -> Result<$V, CycleError<'a, $tcx>>
405             {
406                 debug_assert!(tcx.dep_graph.is_green(dep_node_index));
407
408                 // First we try to load the result from the on-disk cache
409                 let result = if Self::cache_on_disk(key) &&
410                                 tcx.sess.opts.debugging_opts.incremental_queries {
411                     let prev_dep_node_index =
412                         tcx.dep_graph.prev_dep_node_index_of(dep_node);
413                     let result = Self::try_load_from_disk(tcx.global_tcx(),
414                                                           prev_dep_node_index);
415
416                     // We always expect to find a cached result for things that
417                     // can be forced from DepNode.
418                     debug_assert!(!dep_node.kind.can_reconstruct_query_key() ||
419                                   result.is_some(),
420                                   "Missing on-disk cache entry for {:?}",
421                                   dep_node);
422                     result
423                 } else {
424                     // Some things are never cached on disk.
425                     None
426                 };
427
428                 let result = if let Some(result) = result {
429                     result
430                 } else {
431                     // We could not load a result from the on-disk cache, so
432                     // recompute.
433                     let (result, _ ) = tcx.cycle_check(span, Query::$name(key), || {
434                         // The diagnostics for this query have already been
435                         // promoted to the current session during
436                         // try_mark_green(), so we can ignore them here.
437                         tcx.sess.diagnostic().track_diagnostics(|| {
438                             // The dep-graph for this computation is already in
439                             // place
440                             tcx.dep_graph.with_ignore(|| {
441                                 Self::compute_result(tcx, key)
442                             })
443                         })
444                     })?;
445                     result
446                 };
447
448                 // If -Zincremental-verify-ich is specified, re-hash results from
449                 // the cache and make sure that they have the expected fingerprint.
450                 if tcx.sess.opts.debugging_opts.incremental_verify_ich {
451                     use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
452                     use ich::Fingerprint;
453
454                     assert!(Some(tcx.dep_graph.fingerprint_of(dep_node_index)) ==
455                             tcx.dep_graph.prev_fingerprint_of(dep_node),
456                             "Fingerprint for green query instance not loaded \
457                              from cache: {:?}", dep_node);
458
459                     debug!("BEGIN verify_ich({:?})", dep_node);
460                     let mut hcx = tcx.create_stable_hashing_context();
461                     let mut hasher = StableHasher::new();
462
463                     result.hash_stable(&mut hcx, &mut hasher);
464
465                     let new_hash: Fingerprint = hasher.finish();
466                     debug!("END verify_ich({:?})", dep_node);
467
468                     let old_hash = tcx.dep_graph.fingerprint_of(dep_node_index);
469
470                     assert!(new_hash == old_hash, "Found unstable fingerprints \
471                         for {:?}", dep_node);
472                 }
473
474                 if tcx.sess.opts.debugging_opts.query_dep_graph {
475                     tcx.dep_graph.mark_loaded_from_cache(dep_node_index, true);
476                 }
477
478                 let value = QueryValue::new(result, dep_node_index);
479
480                 Ok((&tcx.maps
481                          .$name
482                          .borrow_mut()
483                          .map
484                          .entry(key)
485                          .or_insert(value)
486                          .value).clone())
487             }
488
489             fn force(tcx: TyCtxt<'a, $tcx, 'lcx>,
490                      key: $K,
491                      span: Span,
492                      dep_node: DepNode)
493                      -> Result<($V, DepNodeIndex), CycleError<'a, $tcx>> {
494                 debug_assert!(tcx.dep_graph.node_color(&dep_node).is_none());
495
496                 profq_msg!(tcx, ProfileQueriesMsg::ProviderBegin);
497                 let res = tcx.cycle_check(span, Query::$name(key), || {
498                     tcx.sess.diagnostic().track_diagnostics(|| {
499                         if dep_node.kind.is_eval_always() {
500                             tcx.dep_graph.with_eval_always_task(dep_node,
501                                                                 tcx,
502                                                                 key,
503                                                                 Self::compute_result)
504                         } else {
505                             tcx.dep_graph.with_task(dep_node,
506                                                     tcx,
507                                                     key,
508                                                     Self::compute_result)
509                         }
510                     })
511                 })?;
512                 profq_msg!(tcx, ProfileQueriesMsg::ProviderEnd);
513
514                 let ((result, dep_node_index), diagnostics) = res;
515
516                 if tcx.sess.opts.debugging_opts.query_dep_graph {
517                     tcx.dep_graph.mark_loaded_from_cache(dep_node_index, false);
518                 }
519
520                 if dep_node.kind != ::dep_graph::DepKind::Null {
521                     tcx.on_disk_query_result_cache
522                        .store_diagnostics(dep_node_index, diagnostics);
523                 }
524
525                 let value = QueryValue::new(result, dep_node_index);
526
527                 Ok(((&tcx.maps
528                          .$name
529                          .borrow_mut()
530                          .map
531                          .entry(key)
532                          .or_insert(value)
533                          .value).clone(),
534                    dep_node_index))
535             }
536
537             pub fn try_get(tcx: TyCtxt<'a, $tcx, 'lcx>, span: Span, key: $K)
538                            -> Result<$V, DiagnosticBuilder<'a>> {
539                 match Self::try_get_with(tcx, span, key) {
540                     Ok(e) => Ok(e),
541                     Err(e) => Err(tcx.report_cycle(e)),
542                 }
543             }
544         })*
545
546         #[derive(Copy, Clone)]
547         pub struct TyCtxtAt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
548             pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
549             pub span: Span,
550         }
551
552         impl<'a, 'gcx, 'tcx> Deref for TyCtxtAt<'a, 'gcx, 'tcx> {
553             type Target = TyCtxt<'a, 'gcx, 'tcx>;
554             fn deref(&self) -> &Self::Target {
555                 &self.tcx
556             }
557         }
558
559         impl<'a, $tcx, 'lcx> TyCtxt<'a, $tcx, 'lcx> {
560             /// Return a transparent wrapper for `TyCtxt` which uses
561             /// `span` as the location of queries performed through it.
562             pub fn at(self, span: Span) -> TyCtxtAt<'a, $tcx, 'lcx> {
563                 TyCtxtAt {
564                     tcx: self,
565                     span
566                 }
567             }
568
569             $($(#[$attr])*
570             pub fn $name(self, key: $K) -> $V {
571                 self.at(DUMMY_SP).$name(key)
572             })*
573         }
574
575         impl<'a, $tcx, 'lcx> TyCtxtAt<'a, $tcx, 'lcx> {
576             $($(#[$attr])*
577             pub fn $name(self, key: $K) -> $V {
578                 queries::$name::try_get(self.tcx, self.span, key).unwrap_or_else(|mut e| {
579                     e.emit();
580                     handle_cycle_error!([$($modifiers)*][self])
581                 })
582             })*
583         }
584
585         define_provider_struct! {
586             tcx: $tcx,
587             input: ($(([$($modifiers)*] [$name] [$K] [$V]))*)
588         }
589
590         impl<$tcx> Copy for Providers<$tcx> {}
591         impl<$tcx> Clone for Providers<$tcx> {
592             fn clone(&self) -> Self { *self }
593         }
594     }
595 }
596
597 macro_rules! define_map_struct {
598     (tcx: $tcx:tt,
599      input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
600         pub struct Maps<$tcx> {
601             providers: IndexVec<CrateNum, Providers<$tcx>>,
602             query_stack: RefCell<Vec<(Span, Query<$tcx>)>>,
603             $($(#[$attr])*  $name: RefCell<QueryMap<$tcx, queries::$name<$tcx>>>,)*
604         }
605     };
606 }
607
608 macro_rules! define_provider_struct {
609     (tcx: $tcx:tt,
610      input: ($(([$($modifiers:tt)*] [$name:ident] [$K:ty] [$R:ty]))*)) => {
611         pub struct Providers<$tcx> {
612             $(pub $name: for<'a> fn(TyCtxt<'a, $tcx, $tcx>, $K) -> $R,)*
613         }
614
615         impl<$tcx> Default for Providers<$tcx> {
616             fn default() -> Self {
617                 $(fn $name<'a, $tcx>(_: TyCtxt<'a, $tcx, $tcx>, key: $K) -> $R {
618                     bug!("tcx.maps.{}({:?}) unsupported by its crate",
619                          stringify!($name), key);
620                 })*
621                 Providers { $($name),* }
622             }
623         }
624     };
625 }
626
627
628 /// The red/green evaluation system will try to mark a specific DepNode in the
629 /// dependency graph as green by recursively trying to mark the dependencies of
630 /// that DepNode as green. While doing so, it will sometimes encounter a DepNode
631 /// where we don't know if it is red or green and we therefore actually have
632 /// to recompute its value in order to find out. Since the only piece of
633 /// information that we have at that point is the DepNode we are trying to
634 /// re-evaluate, we need some way to re-run a query from just that. This is what
635 /// `force_from_dep_node()` implements.
636 ///
637 /// In the general case, a DepNode consists of a DepKind and an opaque
638 /// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
639 /// is usually constructed by computing a stable hash of the query-key that the
640 /// DepNode corresponds to. Consequently, it is not in general possible to go
641 /// back from hash to query-key (since hash functions are not reversible). For
642 /// this reason `force_from_dep_node()` is expected to fail from time to time
643 /// because we just cannot find out, from the DepNode alone, what the
644 /// corresponding query-key is and therefore cannot re-run the query.
645 ///
646 /// The system deals with this case letting `try_mark_green` fail which forces
647 /// the root query to be re-evaluated.
648 ///
649 /// Now, if force_from_dep_node() would always fail, it would be pretty useless.
650 /// Fortunately, we can use some contextual information that will allow us to
651 /// reconstruct query-keys for certain kinds of DepNodes. In particular, we
652 /// enforce by construction that the GUID/fingerprint of certain DepNodes is a
653 /// valid DefPathHash. Since we also always build a huge table that maps every
654 /// DefPathHash in the current codebase to the corresponding DefId, we have
655 /// everything we need to re-run the query.
656 ///
657 /// Take the `mir_validated` query as an example. Like many other queries, it
658 /// just has a single parameter: the DefId of the item it will compute the
659 /// validated MIR for. Now, when we call `force_from_dep_node()` on a dep-node
660 /// with kind `MirValidated`, we know that the GUID/fingerprint of the dep-node
661 /// is actually a DefPathHash, and can therefore just look up the corresponding
662 /// DefId in `tcx.def_path_hash_to_def_id`.
663 ///
664 /// When you implement a new query, it will likely have a corresponding new
665 /// DepKind, and you'll have to support it here in `force_from_dep_node()`. As
666 /// a rule of thumb, if your query takes a DefId or DefIndex as sole parameter,
667 /// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
668 /// add it to the "We don't have enough information to reconstruct..." group in
669 /// the match below.
670 pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
671                                            dep_node: &DepNode)
672                                            -> bool {
673     use ty::maps::keys::Key;
674     use hir::def_id::LOCAL_CRATE;
675
676     // We must avoid ever having to call force_from_dep_node() for a
677     // DepNode::CodegenUnit:
678     // Since we cannot reconstruct the query key of a DepNode::CodegenUnit, we
679     // would always end up having to evaluate the first caller of the
680     // `codegen_unit` query that *is* reconstructible. This might very well be
681     // the `compile_codegen_unit` query, thus re-translating the whole CGU just
682     // to re-trigger calling the `codegen_unit` query with the right key. At
683     // that point we would already have re-done all the work we are trying to
684     // avoid doing in the first place.
685     // The solution is simple: Just explicitly call the `codegen_unit` query for
686     // each CGU, right after partitioning. This way `try_mark_green` will always
687     // hit the cache instead of having to go through `force_from_dep_node`.
688     // This assertion makes sure, we actually keep applying the solution above.
689     debug_assert!(dep_node.kind != DepKind::CodegenUnit,
690                   "calling force_from_dep_node() on DepKind::CodegenUnit");
691
692     if !dep_node.kind.can_reconstruct_query_key() {
693         return false
694     }
695
696     macro_rules! def_id {
697         () => {
698             if let Some(def_id) = dep_node.extract_def_id(tcx) {
699                 def_id
700             } else {
701                 // return from the whole function
702                 return false
703             }
704         }
705     };
706
707     macro_rules! krate {
708         () => { (def_id!()).krate }
709     };
710
711     macro_rules! force {
712         ($query:ident, $key:expr) => {
713             {
714                 use $crate::util::common::{ProfileQueriesMsg, profq_msg};
715
716                 // FIXME(eddyb) Get more valid Span's on queries.
717                 // def_span guard is necessary to prevent a recursive loop,
718                 // default_span calls def_span query internally.
719                 let span = if stringify!($query) != "def_span" {
720                     $key.default_span(tcx)
721                 } else {
722                     ::syntax_pos::DUMMY_SP
723                 };
724
725                 profq_msg!(tcx,
726                     ProfileQueriesMsg::QueryBegin(
727                         span.data(),
728                         ::ty::maps::QueryMsg::$query(profq_key!(tcx, $key))
729                     )
730                 );
731
732                 match ::ty::maps::queries::$query::force(tcx, $key, span, *dep_node) {
733                     Ok(_) => {},
734                     Err(e) => {
735                         tcx.report_cycle(e).emit();
736                     }
737                 }
738             }
739         }
740     };
741
742     // FIXME(#45015): We should try move this boilerplate code into a macro
743     //                somehow.
744     match dep_node.kind {
745         // These are inputs that are expected to be pre-allocated and that
746         // should therefore always be red or green already
747         DepKind::AllLocalTraitImpls |
748         DepKind::Krate |
749         DepKind::CrateMetadata |
750         DepKind::HirBody |
751         DepKind::Hir |
752
753         // This are anonymous nodes
754         DepKind::TraitSelect |
755
756         // We don't have enough information to reconstruct the query key of
757         // these
758         DepKind::IsCopy |
759         DepKind::IsSized |
760         DepKind::IsFreeze |
761         DepKind::NeedsDrop |
762         DepKind::Layout |
763         DepKind::ConstEval |
764         DepKind::InstanceSymbolName |
765         DepKind::MirShim |
766         DepKind::BorrowCheckKrate |
767         DepKind::Specializes |
768         DepKind::ImplementationsOfTrait |
769         DepKind::TypeParamPredicates |
770         DepKind::CodegenUnit |
771         DepKind::CompileCodegenUnit |
772         DepKind::FulfillObligation |
773         DepKind::VtableMethods |
774         DepKind::EraseRegionsTy |
775         DepKind::NormalizeTy |
776         DepKind::SubstituteNormalizeAndTestPredicates |
777         DepKind::InstanceDefSizeEstimate |
778
779         // This one should never occur in this context
780         DepKind::Null => {
781             bug!("force_from_dep_node() - Encountered {:?}", dep_node)
782         }
783
784         // These are not queries
785         DepKind::CoherenceCheckTrait |
786         DepKind::ItemVarianceConstraints => {
787             return false
788         }
789
790         DepKind::RegionScopeTree => { force!(region_scope_tree, def_id!()); }
791
792         DepKind::Coherence => { force!(crate_inherent_impls, LOCAL_CRATE); }
793         DepKind::CoherenceInherentImplOverlapCheck => {
794             force!(crate_inherent_impls_overlap_check, LOCAL_CRATE)
795         },
796         DepKind::PrivacyAccessLevels => { force!(privacy_access_levels, LOCAL_CRATE); }
797         DepKind::MirBuilt => { force!(mir_built, def_id!()); }
798         DepKind::MirConstQualif => { force!(mir_const_qualif, def_id!()); }
799         DepKind::MirConst => { force!(mir_const, def_id!()); }
800         DepKind::MirValidated => { force!(mir_validated, def_id!()); }
801         DepKind::MirOptimized => { force!(optimized_mir, def_id!()); }
802
803         DepKind::BorrowCheck => { force!(borrowck, def_id!()); }
804         DepKind::MirBorrowCheck => { force!(mir_borrowck, def_id!()); }
805         DepKind::UnsafetyCheckResult => { force!(unsafety_check_result, def_id!()); }
806         DepKind::UnsafeDeriveOnReprPacked => { force!(unsafe_derive_on_repr_packed, def_id!()); }
807         DepKind::Reachability => { force!(reachable_set, LOCAL_CRATE); }
808         DepKind::MirKeys => { force!(mir_keys, LOCAL_CRATE); }
809         DepKind::CrateVariances => { force!(crate_variances, LOCAL_CRATE); }
810         DepKind::AssociatedItems => { force!(associated_item, def_id!()); }
811         DepKind::TypeOfItem => { force!(type_of, def_id!()); }
812         DepKind::GenericsOfItem => { force!(generics_of, def_id!()); }
813         DepKind::PredicatesOfItem => { force!(predicates_of, def_id!()); }
814         DepKind::InferredOutlivesOf => { force!(inferred_outlives_of, def_id!()); }
815         DepKind::SuperPredicatesOfItem => { force!(super_predicates_of, def_id!()); }
816         DepKind::TraitDefOfItem => { force!(trait_def, def_id!()); }
817         DepKind::AdtDefOfItem => { force!(adt_def, def_id!()); }
818         DepKind::ImplTraitRef => { force!(impl_trait_ref, def_id!()); }
819         DepKind::ImplPolarity => { force!(impl_polarity, def_id!()); }
820         DepKind::FnSignature => { force!(fn_sig, def_id!()); }
821         DepKind::CoerceUnsizedInfo => { force!(coerce_unsized_info, def_id!()); }
822         DepKind::ItemVariances => { force!(variances_of, def_id!()); }
823         DepKind::IsConstFn => { force!(is_const_fn, def_id!()); }
824         DepKind::IsForeignItem => { force!(is_foreign_item, def_id!()); }
825         DepKind::SizedConstraint => { force!(adt_sized_constraint, def_id!()); }
826         DepKind::DtorckConstraint => { force!(adt_dtorck_constraint, def_id!()); }
827         DepKind::AdtDestructor => { force!(adt_destructor, def_id!()); }
828         DepKind::AssociatedItemDefIds => { force!(associated_item_def_ids, def_id!()); }
829         DepKind::InherentImpls => { force!(inherent_impls, def_id!()); }
830         DepKind::TypeckBodiesKrate => { force!(typeck_item_bodies, LOCAL_CRATE); }
831         DepKind::TypeckTables => { force!(typeck_tables_of, def_id!()); }
832         DepKind::UsedTraitImports => { force!(used_trait_imports, def_id!()); }
833         DepKind::HasTypeckTables => { force!(has_typeck_tables, def_id!()); }
834         DepKind::SymbolName => { force!(def_symbol_name, def_id!()); }
835         DepKind::SpecializationGraph => { force!(specialization_graph_of, def_id!()); }
836         DepKind::ObjectSafety => { force!(is_object_safe, def_id!()); }
837         DepKind::TraitImpls => { force!(trait_impls_of, def_id!()); }
838         DepKind::CheckMatch => { force!(check_match, def_id!()); }
839
840         DepKind::ParamEnv => { force!(param_env, def_id!()); }
841         DepKind::DescribeDef => { force!(describe_def, def_id!()); }
842         DepKind::DefSpan => { force!(def_span, def_id!()); }
843         DepKind::LookupStability => { force!(lookup_stability, def_id!()); }
844         DepKind::LookupDeprecationEntry => {
845             force!(lookup_deprecation_entry, def_id!());
846         }
847         DepKind::ItemBodyNestedBodies => { force!(item_body_nested_bodies, def_id!()); }
848         DepKind::ConstIsRvaluePromotableToStatic => {
849             force!(const_is_rvalue_promotable_to_static, def_id!());
850         }
851         DepKind::RvaluePromotableMap => { force!(rvalue_promotable_map, def_id!()); }
852         DepKind::ImplParent => { force!(impl_parent, def_id!()); }
853         DepKind::TraitOfItem => { force!(trait_of_item, def_id!()); }
854         DepKind::IsExportedSymbol => { force!(is_exported_symbol, def_id!()); }
855         DepKind::IsMirAvailable => { force!(is_mir_available, def_id!()); }
856         DepKind::ItemAttrs => { force!(item_attrs, def_id!()); }
857         DepKind::FnArgNames => { force!(fn_arg_names, def_id!()); }
858         DepKind::DylibDepFormats => { force!(dylib_dependency_formats, krate!()); }
859         DepKind::IsPanicRuntime => { force!(is_panic_runtime, krate!()); }
860         DepKind::IsCompilerBuiltins => { force!(is_compiler_builtins, krate!()); }
861         DepKind::HasGlobalAllocator => { force!(has_global_allocator, krate!()); }
862         DepKind::ExternCrate => { force!(extern_crate, def_id!()); }
863         DepKind::LintLevels => { force!(lint_levels, LOCAL_CRATE); }
864         DepKind::InScopeTraits => { force!(in_scope_traits_map, def_id!().index); }
865         DepKind::ModuleExports => { force!(module_exports, def_id!()); }
866         DepKind::IsSanitizerRuntime => { force!(is_sanitizer_runtime, krate!()); }
867         DepKind::IsProfilerRuntime => { force!(is_profiler_runtime, krate!()); }
868         DepKind::GetPanicStrategy => { force!(panic_strategy, krate!()); }
869         DepKind::IsNoBuiltins => { force!(is_no_builtins, krate!()); }
870         DepKind::ImplDefaultness => { force!(impl_defaultness, def_id!()); }
871         DepKind::ExportedSymbolIds => { force!(exported_symbol_ids, krate!()); }
872         DepKind::NativeLibraries => { force!(native_libraries, krate!()); }
873         DepKind::PluginRegistrarFn => { force!(plugin_registrar_fn, krate!()); }
874         DepKind::DeriveRegistrarFn => { force!(derive_registrar_fn, krate!()); }
875         DepKind::CrateDisambiguator => { force!(crate_disambiguator, krate!()); }
876         DepKind::CrateHash => { force!(crate_hash, krate!()); }
877         DepKind::OriginalCrateName => { force!(original_crate_name, krate!()); }
878
879         DepKind::AllTraitImplementations => {
880             force!(all_trait_implementations, krate!());
881         }
882
883         DepKind::IsDllimportForeignItem => {
884             force!(is_dllimport_foreign_item, def_id!());
885         }
886         DepKind::IsStaticallyIncludedForeignItem => {
887             force!(is_statically_included_foreign_item, def_id!());
888         }
889         DepKind::NativeLibraryKind => { force!(native_library_kind, def_id!()); }
890         DepKind::LinkArgs => { force!(link_args, LOCAL_CRATE); }
891
892         DepKind::ResolveLifetimes => { force!(resolve_lifetimes, krate!()); }
893         DepKind::NamedRegion => { force!(named_region_map, def_id!().index); }
894         DepKind::IsLateBound => { force!(is_late_bound_map, def_id!().index); }
895         DepKind::ObjectLifetimeDefaults => {
896             force!(object_lifetime_defaults_map, def_id!().index);
897         }
898
899         DepKind::Visibility => { force!(visibility, def_id!()); }
900         DepKind::DepKind => { force!(dep_kind, krate!()); }
901         DepKind::CrateName => { force!(crate_name, krate!()); }
902         DepKind::ItemChildren => { force!(item_children, def_id!()); }
903         DepKind::ExternModStmtCnum => { force!(extern_mod_stmt_cnum, def_id!()); }
904         DepKind::GetLangItems => { force!(get_lang_items, LOCAL_CRATE); }
905         DepKind::DefinedLangItems => { force!(defined_lang_items, krate!()); }
906         DepKind::MissingLangItems => { force!(missing_lang_items, krate!()); }
907         DepKind::ExternConstBody => { force!(extern_const_body, def_id!()); }
908         DepKind::VisibleParentMap => { force!(visible_parent_map, LOCAL_CRATE); }
909         DepKind::MissingExternCrateItem => {
910             force!(missing_extern_crate_item, krate!());
911         }
912         DepKind::UsedCrateSource => { force!(used_crate_source, krate!()); }
913         DepKind::PostorderCnums => { force!(postorder_cnums, LOCAL_CRATE); }
914         DepKind::HasCloneClosures => { force!(has_clone_closures, krate!()); }
915         DepKind::HasCopyClosures => { force!(has_copy_closures, krate!()); }
916
917         DepKind::Freevars => { force!(freevars, def_id!()); }
918         DepKind::MaybeUnusedTraitImport => {
919             force!(maybe_unused_trait_import, def_id!());
920         }
921         DepKind::MaybeUnusedExternCrates => { force!(maybe_unused_extern_crates, LOCAL_CRATE); }
922         DepKind::StabilityIndex => { force!(stability_index, LOCAL_CRATE); }
923         DepKind::AllCrateNums => { force!(all_crate_nums, LOCAL_CRATE); }
924         DepKind::ExportedSymbols => { force!(exported_symbols, krate!()); }
925         DepKind::CollectAndPartitionTranslationItems => {
926             force!(collect_and_partition_translation_items, LOCAL_CRATE);
927         }
928         DepKind::ExportName => { force!(export_name, def_id!()); }
929         DepKind::ContainsExternIndicator => {
930             force!(contains_extern_indicator, def_id!());
931         }
932         DepKind::IsTranslatedItem => { force!(is_translated_item, def_id!()); }
933         DepKind::OutputFilenames => { force!(output_filenames, LOCAL_CRATE); }
934
935         DepKind::TargetFeaturesWhitelist => { force!(target_features_whitelist, LOCAL_CRATE); }
936         DepKind::TargetFeaturesEnabled => { force!(target_features_enabled, def_id!()); }
937
938         DepKind::GetSymbolExportLevel => { force!(symbol_export_level, def_id!()); }
939     }
940
941     true
942 }
943
944
945 // FIXME(#45015): Another piece of boilerplate code that could be generated in
946 //                a combined define_dep_nodes!()/define_maps!() macro.
947 macro_rules! impl_load_from_cache {
948     ($($dep_kind:ident => $query_name:ident,)*) => {
949         impl DepNode {
950             // Check whether the query invocation corresponding to the given
951             // DepNode is eligible for on-disk-caching.
952             pub fn cache_on_disk(&self, tcx: TyCtxt) -> bool {
953                 use ty::maps::queries;
954                 use ty::maps::QueryDescription;
955
956                 match self.kind {
957                     $(DepKind::$dep_kind => {
958                         let def_id = self.extract_def_id(tcx).unwrap();
959                         queries::$query_name::cache_on_disk(def_id)
960                     })*
961                     _ => false
962                 }
963             }
964
965             // This is method will execute the query corresponding to the given
966             // DepNode. It is only expected to work for DepNodes where the
967             // above `cache_on_disk` methods returns true.
968             // Also, as a sanity check, it expects that the corresponding query
969             // invocation has been marked as green already.
970             pub fn load_from_on_disk_cache(&self, tcx: TyCtxt) {
971                 match self.kind {
972                     $(DepKind::$dep_kind => {
973                         debug_assert!(tcx.dep_graph
974                                          .node_color(self)
975                                          .map(|c| c.is_green())
976                                          .unwrap_or(false));
977
978                         let def_id = self.extract_def_id(tcx).unwrap();
979                         let _ = tcx.$query_name(def_id);
980                     })*
981                     _ => {
982                         bug!()
983                     }
984                 }
985             }
986         }
987     }
988 }
989
990 impl_load_from_cache!(
991     TypeckTables => typeck_tables_of,
992     MirOptimized => optimized_mir,
993     UnsafetyCheckResult => unsafety_check_result,
994     BorrowCheck => borrowck,
995     MirBorrowCheck => mir_borrowck,
996     MirConstQualif => mir_const_qualif,
997     SymbolName => def_symbol_name,
998     ConstIsRvaluePromotableToStatic => const_is_rvalue_promotable_to_static,
999     ContainsExternIndicator => contains_extern_indicator,
1000     CheckMatch => check_match,
1001     TypeOfItem => type_of,
1002     GenericsOfItem => generics_of,
1003     PredicatesOfItem => predicates_of,
1004     UsedTraitImports => used_trait_imports,
1005 );