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