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