]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/maps.rs
Auto merge of #41978 - alexcrichton:update-cargo, r=Mark-Simulacrum
[rust.git] / src / librustc / ty / maps.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 use dep_graph::{DepGraph, DepNode, DepTrackingMap, DepTrackingMapConfig};
12 use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
13 use hir::def::Def;
14 use hir;
15 use middle::const_val;
16 use middle::privacy::AccessLevels;
17 use middle::region::RegionMaps;
18 use mir;
19 use mir::transform::{MirSuite, MirPassIndex};
20 use session::CompileResult;
21 use ty::{self, CrateInherentImpls, Ty, TyCtxt};
22 use ty::item_path;
23 use ty::steal::Steal;
24 use ty::subst::Substs;
25 use util::nodemap::{DefIdSet, NodeSet};
26
27 use rustc_data_structures::indexed_vec::IndexVec;
28 use std::cell::{RefCell, RefMut};
29 use std::fmt::Debug;
30 use std::hash::Hash;
31 use std::mem;
32 use std::collections::BTreeMap;
33 use std::ops::Deref;
34 use std::rc::Rc;
35 use syntax_pos::{Span, DUMMY_SP};
36 use syntax::attr;
37 use syntax::ast;
38 use syntax::symbol::Symbol;
39
40 pub trait Key: Clone + Hash + Eq + Debug {
41     fn map_crate(&self) -> CrateNum;
42     fn default_span(&self, tcx: TyCtxt) -> Span;
43 }
44
45 impl<'tcx> Key for ty::InstanceDef<'tcx> {
46     fn map_crate(&self) -> CrateNum {
47         LOCAL_CRATE
48     }
49
50     fn default_span(&self, tcx: TyCtxt) -> Span {
51         tcx.def_span(self.def_id())
52     }
53 }
54
55 impl<'tcx> Key for ty::Instance<'tcx> {
56     fn map_crate(&self) -> CrateNum {
57         LOCAL_CRATE
58     }
59
60     fn default_span(&self, tcx: TyCtxt) -> Span {
61         tcx.def_span(self.def_id())
62     }
63 }
64
65 impl Key for CrateNum {
66     fn map_crate(&self) -> CrateNum {
67         *self
68     }
69     fn default_span(&self, _: TyCtxt) -> Span {
70         DUMMY_SP
71     }
72 }
73
74 impl Key for DefId {
75     fn map_crate(&self) -> CrateNum {
76         self.krate
77     }
78     fn default_span(&self, tcx: TyCtxt) -> Span {
79         tcx.def_span(*self)
80     }
81 }
82
83 impl Key for (DefId, DefId) {
84     fn map_crate(&self) -> CrateNum {
85         self.0.krate
86     }
87     fn default_span(&self, tcx: TyCtxt) -> Span {
88         self.1.default_span(tcx)
89     }
90 }
91
92 impl Key for (CrateNum, DefId) {
93     fn map_crate(&self) -> CrateNum {
94         self.0
95     }
96     fn default_span(&self, tcx: TyCtxt) -> Span {
97         self.1.default_span(tcx)
98     }
99 }
100
101 impl<'tcx> Key for (DefId, &'tcx Substs<'tcx>) {
102     fn map_crate(&self) -> CrateNum {
103         self.0.krate
104     }
105     fn default_span(&self, tcx: TyCtxt) -> Span {
106         self.0.default_span(tcx)
107     }
108 }
109
110 impl Key for (MirSuite, DefId) {
111     fn map_crate(&self) -> CrateNum {
112         self.1.map_crate()
113     }
114     fn default_span(&self, tcx: TyCtxt) -> Span {
115         self.1.default_span(tcx)
116     }
117 }
118
119 impl Key for (MirSuite, MirPassIndex, DefId) {
120     fn map_crate(&self) -> CrateNum {
121         self.2.map_crate()
122     }
123     fn default_span(&self, tcx: TyCtxt) -> Span {
124         self.2.default_span(tcx)
125     }
126 }
127
128 trait Value<'tcx>: Sized {
129     fn from_cycle_error<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self;
130 }
131
132 impl<'tcx, T> Value<'tcx> for T {
133     default fn from_cycle_error<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> T {
134         tcx.sess.abort_if_errors();
135         bug!("Value::from_cycle_error called without errors");
136     }
137 }
138
139 impl<'tcx, T: Default> Value<'tcx> for T {
140     default fn from_cycle_error<'a>(_: TyCtxt<'a, 'tcx, 'tcx>) -> T {
141         T::default()
142     }
143 }
144
145 impl<'tcx> Value<'tcx> for Ty<'tcx> {
146     fn from_cycle_error<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx> {
147         tcx.types.err
148     }
149 }
150
151 impl<'tcx> Value<'tcx> for ty::DtorckConstraint<'tcx> {
152     fn from_cycle_error<'a>(_: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
153         Self::empty()
154     }
155 }
156
157 impl<'tcx> Value<'tcx> for ty::SymbolName {
158     fn from_cycle_error<'a>(_: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
159         ty::SymbolName { name: Symbol::intern("<error>").as_str() }
160     }
161 }
162
163 pub struct CycleError<'a, 'tcx: 'a> {
164     span: Span,
165     cycle: RefMut<'a, [(Span, Query<'tcx>)]>,
166 }
167
168 impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
169     pub fn report_cycle(self, CycleError { span, cycle }: CycleError) {
170         // Subtle: release the refcell lock before invoking `describe()`
171         // below by dropping `cycle`.
172         let stack = cycle.to_vec();
173         mem::drop(cycle);
174
175         assert!(!stack.is_empty());
176
177         // Disable naming impls with types in this path, since that
178         // sometimes cycles itself, leading to extra cycle errors.
179         // (And cycle errors around impls tend to occur during the
180         // collect/coherence phases anyhow.)
181         item_path::with_forced_impl_filename_line(|| {
182             let mut err =
183                 struct_span_err!(self.sess, span, E0391,
184                                  "unsupported cyclic reference between types/traits detected");
185             err.span_label(span, "cyclic reference");
186
187             err.span_note(stack[0].0, &format!("the cycle begins when {}...",
188                                                stack[0].1.describe(self)));
189
190             for &(span, ref query) in &stack[1..] {
191                 err.span_note(span, &format!("...which then requires {}...",
192                                              query.describe(self)));
193             }
194
195             err.note(&format!("...which then again requires {}, completing the cycle.",
196                               stack[0].1.describe(self)));
197
198             err.emit();
199         });
200     }
201
202     fn cycle_check<F, R>(self, span: Span, query: Query<'gcx>, compute: F)
203                          -> Result<R, CycleError<'a, 'gcx>>
204         where F: FnOnce() -> R
205     {
206         {
207             let mut stack = self.maps.query_stack.borrow_mut();
208             if let Some((i, _)) = stack.iter().enumerate().rev()
209                                        .find(|&(_, &(_, ref q))| *q == query) {
210                 return Err(CycleError {
211                     span: span,
212                     cycle: RefMut::map(stack, |stack| &mut stack[i..])
213                 });
214             }
215             stack.push((span, query));
216         }
217
218         let result = compute();
219
220         self.maps.query_stack.borrow_mut().pop();
221
222         Ok(result)
223     }
224 }
225
226 trait QueryDescription: DepTrackingMapConfig {
227     fn describe(tcx: TyCtxt, key: Self::Key) -> String;
228 }
229
230 impl<M: DepTrackingMapConfig<Key=DefId>> QueryDescription for M {
231     default fn describe(tcx: TyCtxt, def_id: DefId) -> String {
232         format!("processing `{}`", tcx.item_path_str(def_id))
233     }
234 }
235
236 impl<'tcx> QueryDescription for queries::super_predicates_of<'tcx> {
237     fn describe(tcx: TyCtxt, def_id: DefId) -> String {
238         format!("computing the supertraits of `{}`",
239                 tcx.item_path_str(def_id))
240     }
241 }
242
243 impl<'tcx> QueryDescription for queries::type_param_predicates<'tcx> {
244     fn describe(tcx: TyCtxt, (_, def_id): (DefId, DefId)) -> String {
245         let id = tcx.hir.as_local_node_id(def_id).unwrap();
246         format!("computing the bounds for type parameter `{}`",
247                 tcx.hir.ty_param_name(id))
248     }
249 }
250
251 impl<'tcx> QueryDescription for queries::coherent_trait<'tcx> {
252     fn describe(tcx: TyCtxt, (_, def_id): (CrateNum, DefId)) -> String {
253         format!("coherence checking all impls of trait `{}`",
254                 tcx.item_path_str(def_id))
255     }
256 }
257
258 impl<'tcx> QueryDescription for queries::crate_inherent_impls<'tcx> {
259     fn describe(_: TyCtxt, k: CrateNum) -> String {
260         format!("all inherent impls defined in crate `{:?}`", k)
261     }
262 }
263
264 impl<'tcx> QueryDescription for queries::crate_inherent_impls_overlap_check<'tcx> {
265     fn describe(_: TyCtxt, _: CrateNum) -> String {
266         format!("check for overlap between inherent impls defined in this crate")
267     }
268 }
269
270 impl<'tcx> QueryDescription for queries::crate_variances<'tcx> {
271     fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
272         format!("computing the variances for items in this crate")
273     }
274 }
275
276 impl<'tcx> QueryDescription for queries::mir_shims<'tcx> {
277     fn describe(tcx: TyCtxt, def: ty::InstanceDef<'tcx>) -> String {
278         format!("generating MIR shim for `{}`",
279                 tcx.item_path_str(def.def_id()))
280     }
281 }
282
283 impl<'tcx> QueryDescription for queries::privacy_access_levels<'tcx> {
284     fn describe(_: TyCtxt, _: CrateNum) -> String {
285         format!("privacy access levels")
286     }
287 }
288
289 impl<'tcx> QueryDescription for queries::typeck_item_bodies<'tcx> {
290     fn describe(_: TyCtxt, _: CrateNum) -> String {
291         format!("type-checking all item bodies")
292     }
293 }
294
295 impl<'tcx> QueryDescription for queries::reachable_set<'tcx> {
296     fn describe(_: TyCtxt, _: CrateNum) -> String {
297         format!("reachability")
298     }
299 }
300
301 impl<'tcx> QueryDescription for queries::const_eval<'tcx> {
302     fn describe(tcx: TyCtxt, (def_id, _): (DefId, &'tcx Substs<'tcx>)) -> String {
303         format!("const-evaluating `{}`", tcx.item_path_str(def_id))
304     }
305 }
306
307 impl<'tcx> QueryDescription for queries::mir_keys<'tcx> {
308     fn describe(_: TyCtxt, _: CrateNum) -> String {
309         format!("getting a list of all mir_keys")
310     }
311 }
312
313 impl<'tcx> QueryDescription for queries::symbol_name<'tcx> {
314     fn describe(_tcx: TyCtxt, instance: ty::Instance<'tcx>) -> String {
315         format!("computing the symbol for `{}`", instance)
316     }
317 }
318
319 impl<'tcx> QueryDescription for queries::describe_def<'tcx> {
320     fn describe(_: TyCtxt, _: DefId) -> String {
321         bug!("describe_def")
322     }
323 }
324
325 impl<'tcx> QueryDescription for queries::def_span<'tcx> {
326     fn describe(_: TyCtxt, _: DefId) -> String {
327         bug!("def_span")
328     }
329 }
330
331
332 impl<'tcx> QueryDescription for queries::stability<'tcx> {
333     fn describe(_: TyCtxt, _: DefId) -> String {
334         bug!("stability")
335     }
336 }
337
338 impl<'tcx> QueryDescription for queries::deprecation<'tcx> {
339     fn describe(_: TyCtxt, _: DefId) -> String {
340         bug!("deprecation")
341     }
342 }
343
344 impl<'tcx> QueryDescription for queries::item_attrs<'tcx> {
345     fn describe(_: TyCtxt, _: DefId) -> String {
346         bug!("item_attrs")
347     }
348 }
349
350 impl<'tcx> QueryDescription for queries::is_exported_symbol<'tcx> {
351     fn describe(_: TyCtxt, _: DefId) -> String {
352         bug!("is_exported_symbol")
353     }
354 }
355
356 impl<'tcx> QueryDescription for queries::fn_arg_names<'tcx> {
357     fn describe(_: TyCtxt, _: DefId) -> String {
358         bug!("fn_arg_names")
359     }
360 }
361
362 impl<'tcx> QueryDescription for queries::impl_parent<'tcx> {
363     fn describe(_: TyCtxt, _: DefId) -> String {
364         bug!("impl_parent")
365     }
366 }
367
368 impl<'tcx> QueryDescription for queries::trait_of_item<'tcx> {
369     fn describe(_: TyCtxt, _: DefId) -> String {
370         bug!("trait_of_item")
371     }
372 }
373
374 impl<'tcx> QueryDescription for queries::item_body_nested_bodies<'tcx> {
375     fn describe(tcx: TyCtxt, def_id: DefId) -> String {
376         format!("nested item bodies of `{}`", tcx.item_path_str(def_id))
377     }
378 }
379
380 impl<'tcx> QueryDescription for queries::const_is_rvalue_promotable_to_static<'tcx> {
381     fn describe(tcx: TyCtxt, def_id: DefId) -> String {
382         format!("const checking if rvalue is promotable to static `{}`",
383             tcx.item_path_str(def_id))
384     }
385 }
386
387 impl<'tcx> QueryDescription for queries::is_mir_available<'tcx> {
388     fn describe(tcx: TyCtxt, def_id: DefId) -> String {
389         format!("checking if item is mir available: `{}`",
390             tcx.item_path_str(def_id))
391     }
392 }
393
394 macro_rules! define_maps {
395     (<$tcx:tt>
396      $($(#[$attr:meta])*
397        [$($modifiers:tt)*] $name:ident: $node:ident($K:ty) -> $V:ty,)*) => {
398         define_map_struct! {
399             tcx: $tcx,
400             input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
401         }
402
403         impl<$tcx> Maps<$tcx> {
404             pub fn new(dep_graph: DepGraph,
405                        providers: IndexVec<CrateNum, Providers<$tcx>>)
406                        -> Self {
407                 Maps {
408                     providers,
409                     query_stack: RefCell::new(vec![]),
410                     $($name: RefCell::new(DepTrackingMap::new(dep_graph.clone()))),*
411                 }
412             }
413         }
414
415         #[allow(bad_style)]
416         #[derive(Copy, Clone, Debug, PartialEq, Eq)]
417         pub enum Query<$tcx> {
418             $($(#[$attr])* $name($K)),*
419         }
420
421         impl<$tcx> Query<$tcx> {
422             pub fn describe(&self, tcx: TyCtxt) -> String {
423                 match *self {
424                     $(Query::$name(key) => queries::$name::describe(tcx, key)),*
425                 }
426             }
427         }
428
429         pub mod queries {
430             use std::marker::PhantomData;
431
432             $(#[allow(bad_style)]
433             pub struct $name<$tcx> {
434                 data: PhantomData<&$tcx ()>
435             })*
436         }
437
438         $(impl<$tcx> DepTrackingMapConfig for queries::$name<$tcx> {
439             type Key = $K;
440             type Value = $V;
441
442             #[allow(unused)]
443             fn to_dep_node(key: &$K) -> DepNode<DefId> {
444                 use dep_graph::DepNode::*;
445
446                 $node(*key)
447             }
448         }
449         impl<'a, $tcx, 'lcx> queries::$name<$tcx> {
450             fn try_get_with<F, R>(tcx: TyCtxt<'a, $tcx, 'lcx>,
451                                   mut span: Span,
452                                   key: $K,
453                                   f: F)
454                                   -> Result<R, CycleError<'a, $tcx>>
455                 where F: FnOnce(&$V) -> R
456             {
457                 debug!("ty::queries::{}::try_get_with(key={:?}, span={:?})",
458                        stringify!($name),
459                        key,
460                        span);
461
462                 if let Some(result) = tcx.maps.$name.borrow().get(&key) {
463                     return Ok(f(result));
464                 }
465
466                 // FIXME(eddyb) Get more valid Span's on queries.
467                 // def_span guard is necesary to prevent a recursive loop,
468                 // default_span calls def_span query internally.
469                 if span == DUMMY_SP && stringify!($name) != "def_span" {
470                     span = key.default_span(tcx)
471                 }
472
473                 let _task = tcx.dep_graph.in_task(Self::to_dep_node(&key));
474
475                 let result = tcx.cycle_check(span, Query::$name(key), || {
476                     let provider = tcx.maps.providers[key.map_crate()].$name;
477                     provider(tcx.global_tcx(), key)
478                 })?;
479
480                 Ok(f(tcx.maps.$name.borrow_mut().entry(key).or_insert(result)))
481             }
482
483             pub fn try_get(tcx: TyCtxt<'a, $tcx, 'lcx>, span: Span, key: $K)
484                            -> Result<$V, CycleError<'a, $tcx>> {
485                 Self::try_get_with(tcx, span, key, Clone::clone)
486             }
487
488             pub fn force(tcx: TyCtxt<'a, $tcx, 'lcx>, span: Span, key: $K) {
489                 // FIXME(eddyb) Move away from using `DepTrackingMap`
490                 // so we don't have to explicitly ignore a false edge:
491                 // we can't observe a value dependency, only side-effects,
492                 // through `force`, and once everything has been updated,
493                 // perhaps only diagnostics, if those, will remain.
494                 let _ignore = tcx.dep_graph.in_ignore();
495                 match Self::try_get_with(tcx, span, key, |_| ()) {
496                     Ok(()) => {}
497                     Err(e) => tcx.report_cycle(e)
498                 }
499             }
500         })*
501
502         #[derive(Copy, Clone)]
503         pub struct TyCtxtAt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
504             pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
505             pub span: Span,
506         }
507
508         impl<'a, 'gcx, 'tcx> Deref for TyCtxtAt<'a, 'gcx, 'tcx> {
509             type Target = TyCtxt<'a, 'gcx, 'tcx>;
510             fn deref(&self) -> &Self::Target {
511                 &self.tcx
512             }
513         }
514
515         impl<'a, $tcx, 'lcx> TyCtxt<'a, $tcx, 'lcx> {
516             /// Return a transparent wrapper for `TyCtxt` which uses
517             /// `span` as the location of queries performed through it.
518             pub fn at(self, span: Span) -> TyCtxtAt<'a, $tcx, 'lcx> {
519                 TyCtxtAt {
520                     tcx: self,
521                     span
522                 }
523             }
524
525             $($(#[$attr])*
526             pub fn $name(self, key: $K) -> $V {
527                 self.at(DUMMY_SP).$name(key)
528             })*
529         }
530
531         impl<'a, $tcx, 'lcx> TyCtxtAt<'a, $tcx, 'lcx> {
532             $($(#[$attr])*
533             pub fn $name(self, key: $K) -> $V {
534                 queries::$name::try_get(self.tcx, self.span, key).unwrap_or_else(|e| {
535                     self.report_cycle(e);
536                     Value::from_cycle_error(self.global_tcx())
537                 })
538             })*
539         }
540
541         define_provider_struct! {
542             tcx: $tcx,
543             input: ($(([$($modifiers)*] [$name] [$K] [$V]))*),
544             output: ()
545         }
546
547         impl<$tcx> Copy for Providers<$tcx> {}
548         impl<$tcx> Clone for Providers<$tcx> {
549             fn clone(&self) -> Self { *self }
550         }
551     }
552 }
553
554 macro_rules! define_map_struct {
555     // Initial state
556     (tcx: $tcx:tt,
557      input: $input:tt) => {
558         define_map_struct! {
559             tcx: $tcx,
560             input: $input,
561             output: ()
562         }
563     };
564
565     // Final output
566     (tcx: $tcx:tt,
567      input: (),
568      output: ($($output:tt)*)) => {
569         pub struct Maps<$tcx> {
570             providers: IndexVec<CrateNum, Providers<$tcx>>,
571             query_stack: RefCell<Vec<(Span, Query<$tcx>)>>,
572             $($output)*
573         }
574     };
575
576     // Field recognized and ready to shift into the output
577     (tcx: $tcx:tt,
578      ready: ([$($pub:tt)*] [$($attr:tt)*] [$name:ident]),
579      input: $input:tt,
580      output: ($($output:tt)*)) => {
581         define_map_struct! {
582             tcx: $tcx,
583             input: $input,
584             output: ($($output)*
585                      $(#[$attr])* $($pub)* $name: RefCell<DepTrackingMap<queries::$name<$tcx>>>,)
586         }
587     };
588
589     // No modifiers left? This is a private item.
590     (tcx: $tcx:tt,
591      input: (([] $attrs:tt $name:tt) $($input:tt)*),
592      output: $output:tt) => {
593         define_map_struct! {
594             tcx: $tcx,
595             ready: ([pub] $attrs $name),
596             input: ($($input)*),
597             output: $output
598         }
599     };
600
601     // Skip other modifiers
602     (tcx: $tcx:tt,
603      input: (([$other_modifier:tt $($modifiers:tt)*] $($fields:tt)*) $($input:tt)*),
604      output: $output:tt) => {
605         define_map_struct! {
606             tcx: $tcx,
607             input: (([$($modifiers)*] $($fields)*) $($input)*),
608             output: $output
609         }
610     };
611 }
612
613 macro_rules! define_provider_struct {
614     // Initial state:
615     (tcx: $tcx:tt, input: $input:tt) => {
616         define_provider_struct! {
617             tcx: $tcx,
618             input: $input,
619             output: ()
620         }
621     };
622
623     // Final state:
624     (tcx: $tcx:tt,
625      input: (),
626      output: ($(([$name:ident] [$K:ty] [$R:ty]))*)) => {
627         pub struct Providers<$tcx> {
628             $(pub $name: for<'a> fn(TyCtxt<'a, $tcx, $tcx>, $K) -> $R,)*
629         }
630
631         impl<$tcx> Default for Providers<$tcx> {
632             fn default() -> Self {
633                 $(fn $name<'a, $tcx>(_: TyCtxt<'a, $tcx, $tcx>, key: $K) -> $R {
634                     bug!("tcx.maps.{}({:?}) unsupported by its crate",
635                          stringify!($name), key);
636                 })*
637                 Providers { $($name),* }
638             }
639         }
640     };
641
642     // Something ready to shift:
643     (tcx: $tcx:tt,
644      ready: ($name:tt $K:tt $V:tt),
645      input: $input:tt,
646      output: ($($output:tt)*)) => {
647         define_provider_struct! {
648             tcx: $tcx,
649             input: $input,
650             output: ($($output)* ($name $K $V))
651         }
652     };
653
654     // Regular queries produce a `V` only.
655     (tcx: $tcx:tt,
656      input: (([] $name:tt $K:tt $V:tt) $($input:tt)*),
657      output: $output:tt) => {
658         define_provider_struct! {
659             tcx: $tcx,
660             ready: ($name $K $V),
661             input: ($($input)*),
662             output: $output
663         }
664     };
665
666     // Skip modifiers.
667     (tcx: $tcx:tt,
668      input: (([$other_modifier:tt $($modifiers:tt)*] $($fields:tt)*) $($input:tt)*),
669      output: $output:tt) => {
670         define_provider_struct! {
671             tcx: $tcx,
672             input: (([$($modifiers)*] $($fields)*) $($input)*),
673             output: $output
674         }
675     };
676 }
677
678 // Each of these maps also corresponds to a method on a
679 // `Provider` trait for requesting a value of that type,
680 // and a method on `Maps` itself for doing that in a
681 // a way that memoizes and does dep-graph tracking,
682 // wrapping around the actual chain of providers that
683 // the driver creates (using several `rustc_*` crates).
684 define_maps! { <'tcx>
685     /// Records the type of every item.
686     [] type_of: ItemSignature(DefId) -> Ty<'tcx>,
687
688     /// Maps from the def-id of an item (trait/struct/enum/fn) to its
689     /// associated generics and predicates.
690     [] generics_of: ItemSignature(DefId) -> &'tcx ty::Generics,
691     [] predicates_of: ItemSignature(DefId) -> ty::GenericPredicates<'tcx>,
692
693     /// Maps from the def-id of a trait to the list of
694     /// super-predicates. This is a subset of the full list of
695     /// predicates. We store these in a separate map because we must
696     /// evaluate them even during type conversion, often before the
697     /// full predicates are available (note that supertraits have
698     /// additional acyclicity requirements).
699     [] super_predicates_of: ItemSignature(DefId) -> ty::GenericPredicates<'tcx>,
700
701     /// To avoid cycles within the predicates of a single item we compute
702     /// per-type-parameter predicates for resolving `T::AssocTy`.
703     [] type_param_predicates: TypeParamPredicates((DefId, DefId))
704         -> ty::GenericPredicates<'tcx>,
705
706     [] trait_def: ItemSignature(DefId) -> &'tcx ty::TraitDef,
707     [] adt_def: ItemSignature(DefId) -> &'tcx ty::AdtDef,
708     [] adt_destructor: AdtDestructor(DefId) -> Option<ty::Destructor>,
709     [] adt_sized_constraint: SizedConstraint(DefId) -> &'tcx [Ty<'tcx>],
710     [] adt_dtorck_constraint: DtorckConstraint(DefId) -> ty::DtorckConstraint<'tcx>,
711
712     /// True if this is a foreign item (i.e., linked via `extern { ... }`).
713     [] is_foreign_item: IsForeignItem(DefId) -> bool,
714
715     /// Get a map with the variance of every item; use `item_variance`
716     /// instead.
717     [] crate_variances: crate_variances(CrateNum) -> Rc<ty::CrateVariancesMap>,
718
719     /// Maps from def-id of a type or region parameter to its
720     /// (inferred) variance.
721     [] variances_of: ItemVariances(DefId) -> Rc<Vec<ty::Variance>>,
722
723     /// Maps from an impl/trait def-id to a list of the def-ids of its items
724     [] associated_item_def_ids: AssociatedItemDefIds(DefId) -> Rc<Vec<DefId>>,
725
726     /// Maps from a trait item to the trait item "descriptor"
727     [] associated_item: AssociatedItems(DefId) -> ty::AssociatedItem,
728
729     [] impl_trait_ref: ItemSignature(DefId) -> Option<ty::TraitRef<'tcx>>,
730     [] impl_polarity: ItemSignature(DefId) -> hir::ImplPolarity,
731
732     /// Maps a DefId of a type to a list of its inherent impls.
733     /// Contains implementations of methods that are inherent to a type.
734     /// Methods in these implementations don't need to be exported.
735     [] inherent_impls: InherentImpls(DefId) -> Rc<Vec<DefId>>,
736
737     /// Set of all the def-ids in this crate that have MIR associated with
738     /// them. This includes all the body owners, but also things like struct
739     /// constructors.
740     [] mir_keys: mir_keys(CrateNum) -> Rc<DefIdSet>,
741
742     /// Maps DefId's that have an associated Mir to the result
743     /// of the MIR qualify_consts pass. The actual meaning of
744     /// the value isn't known except to the pass itself.
745     [] mir_const_qualif: Mir(DefId) -> u8,
746
747     /// Fetch the MIR for a given def-id up till the point where it is
748     /// ready for const evaluation.
749     ///
750     /// See the README for the `mir` module for details.
751     [] mir_const: Mir(DefId) -> &'tcx Steal<mir::Mir<'tcx>>,
752
753     [] mir_validated: Mir(DefId) -> &'tcx Steal<mir::Mir<'tcx>>,
754
755     /// MIR after our optimization passes have run. This is MIR that is ready
756     /// for trans. This is also the only query that can fetch non-local MIR, at present.
757     [] optimized_mir: Mir(DefId) -> &'tcx mir::Mir<'tcx>,
758
759     /// Records the type of each closure. The def ID is the ID of the
760     /// expression defining the closure.
761     [] closure_kind: ItemSignature(DefId) -> ty::ClosureKind,
762
763     /// Records the type of each closure. The def ID is the ID of the
764     /// expression defining the closure.
765     [] closure_type: ItemSignature(DefId) -> ty::PolyFnSig<'tcx>,
766
767     /// Caches CoerceUnsized kinds for impls on custom types.
768     [] coerce_unsized_info: ItemSignature(DefId)
769         -> ty::adjustment::CoerceUnsizedInfo,
770
771     [] typeck_item_bodies: typeck_item_bodies_dep_node(CrateNum) -> CompileResult,
772
773     [] typeck_tables_of: TypeckTables(DefId) -> &'tcx ty::TypeckTables<'tcx>,
774
775     [] has_typeck_tables: TypeckTables(DefId) -> bool,
776
777     [] coherent_trait: coherent_trait_dep_node((CrateNum, DefId)) -> (),
778
779     [] borrowck: BorrowCheck(DefId) -> (),
780
781     /// Gets a complete map from all types to their inherent impls.
782     /// Not meant to be used directly outside of coherence.
783     /// (Defined only for LOCAL_CRATE)
784     [] crate_inherent_impls: crate_inherent_impls_dep_node(CrateNum) -> CrateInherentImpls,
785
786     /// Checks all types in the krate for overlap in their inherent impls. Reports errors.
787     /// Not meant to be used directly outside of coherence.
788     /// (Defined only for LOCAL_CRATE)
789     [] crate_inherent_impls_overlap_check: crate_inherent_impls_dep_node(CrateNum) -> (),
790
791     /// Results of evaluating const items or constants embedded in
792     /// other items (such as enum variant explicit discriminants).
793     [] const_eval: const_eval_dep_node((DefId, &'tcx Substs<'tcx>))
794         -> const_val::EvalResult<'tcx>,
795
796     /// Performs the privacy check and computes "access levels".
797     [] privacy_access_levels: PrivacyAccessLevels(CrateNum) -> Rc<AccessLevels>,
798
799     [] reachable_set: reachability_dep_node(CrateNum) -> Rc<NodeSet>,
800
801     /// Per-function `RegionMaps`. The `DefId` should be the owner-def-id for the fn body;
802     /// in the case of closures or "inline" expressions, this will be redirected to the enclosing
803     /// fn item.
804     [] region_maps: RegionMaps(DefId) -> Rc<RegionMaps>,
805
806     [] mir_shims: mir_shim_dep_node(ty::InstanceDef<'tcx>) -> &'tcx mir::Mir<'tcx>,
807
808     [] def_symbol_name: SymbolName(DefId) -> ty::SymbolName,
809     [] symbol_name: symbol_name_dep_node(ty::Instance<'tcx>) -> ty::SymbolName,
810
811     [] describe_def: DescribeDef(DefId) -> Option<Def>,
812     [] def_span: DefSpan(DefId) -> Span,
813     [] stability: Stability(DefId) -> Option<attr::Stability>,
814     [] deprecation: Deprecation(DefId) -> Option<attr::Deprecation>,
815     [] item_attrs: ItemAttrs(DefId) -> Rc<[ast::Attribute]>,
816     [] fn_arg_names: FnArgNames(DefId) -> Vec<ast::Name>,
817     [] impl_parent: ImplParent(DefId) -> Option<DefId>,
818     [] trait_of_item: TraitOfItem(DefId) -> Option<DefId>,
819     [] is_exported_symbol: IsExportedSymbol(DefId) -> bool,
820     [] item_body_nested_bodies: ItemBodyNestedBodies(DefId) -> Rc<BTreeMap<hir::BodyId, hir::Body>>,
821     [] const_is_rvalue_promotable_to_static: ConstIsRvaluePromotableToStatic(DefId) -> bool,
822     [] is_mir_available: IsMirAvailable(DefId) -> bool,
823 }
824
825 fn coherent_trait_dep_node((_, def_id): (CrateNum, DefId)) -> DepNode<DefId> {
826     DepNode::CoherenceCheckTrait(def_id)
827 }
828
829 fn crate_inherent_impls_dep_node(_: CrateNum) -> DepNode<DefId> {
830     DepNode::Coherence
831 }
832
833 fn reachability_dep_node(_: CrateNum) -> DepNode<DefId> {
834     DepNode::Reachability
835 }
836
837 fn mir_shim_dep_node(instance: ty::InstanceDef) -> DepNode<DefId> {
838     instance.dep_node()
839 }
840
841 fn symbol_name_dep_node(instance: ty::Instance) -> DepNode<DefId> {
842     // symbol_name uses the substs only to traverse them to find the
843     // hash, and that does not create any new dep-nodes.
844     DepNode::SymbolName(instance.def.def_id())
845 }
846
847 fn typeck_item_bodies_dep_node(_: CrateNum) -> DepNode<DefId> {
848     DepNode::TypeckBodiesKrate
849 }
850
851 fn const_eval_dep_node((def_id, _): (DefId, &Substs)) -> DepNode<DefId> {
852     DepNode::ConstEval(def_id)
853 }
854
855 fn mir_keys(_: CrateNum) -> DepNode<DefId> {
856     DepNode::MirKeys
857 }
858
859 fn crate_variances(_: CrateNum) -> DepNode<DefId> {
860     DepNode::CrateVariances
861 }