]> git.proxmox.com Git - rustc.git/blob - src/librustc/ty/maps.rs
New upstream version 1.21.0+dfsg1
[rustc.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::{DepConstructor, DepNode, DepNodeIndex};
12 use errors::{Diagnostic, DiagnosticBuilder};
13 use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
14 use hir::def::Def;
15 use hir;
16 use lint;
17 use middle::const_val;
18 use middle::cstore::{ExternCrate, LinkagePreference};
19 use middle::privacy::AccessLevels;
20 use middle::region::RegionMaps;
21 use mir;
22 use mir::transform::{MirSuite, MirPassIndex};
23 use session::CompileResult;
24 use traits::specialization_graph;
25 use ty::{self, CrateInherentImpls, Ty, TyCtxt};
26 use ty::layout::{Layout, LayoutError};
27 use ty::item_path;
28 use ty::steal::Steal;
29 use ty::subst::Substs;
30 use ty::fast_reject::SimplifiedType;
31 use util::nodemap::{DefIdSet, NodeSet};
32 use util::common::{profq_msg, ProfileQueriesMsg};
33
34 use rustc_data_structures::indexed_vec::IndexVec;
35 use rustc_data_structures::fx::FxHashMap;
36 use std::cell::{RefCell, RefMut, Cell};
37 use std::fmt::Debug;
38 use std::hash::Hash;
39 use std::marker::PhantomData;
40 use std::mem;
41 use std::collections::BTreeMap;
42 use std::ops::Deref;
43 use std::rc::Rc;
44 use syntax_pos::{Span, DUMMY_SP};
45 use syntax::attr;
46 use syntax::ast;
47 use syntax::symbol::Symbol;
48
49 pub trait Key: Clone + Hash + Eq + Debug {
50 fn map_crate(&self) -> CrateNum;
51 fn default_span(&self, tcx: TyCtxt) -> Span;
52 }
53
54 impl<'tcx> Key for ty::InstanceDef<'tcx> {
55 fn map_crate(&self) -> CrateNum {
56 LOCAL_CRATE
57 }
58
59 fn default_span(&self, tcx: TyCtxt) -> Span {
60 tcx.def_span(self.def_id())
61 }
62 }
63
64 impl<'tcx> Key for ty::Instance<'tcx> {
65 fn map_crate(&self) -> CrateNum {
66 LOCAL_CRATE
67 }
68
69 fn default_span(&self, tcx: TyCtxt) -> Span {
70 tcx.def_span(self.def_id())
71 }
72 }
73
74 impl Key for CrateNum {
75 fn map_crate(&self) -> CrateNum {
76 *self
77 }
78 fn default_span(&self, _: TyCtxt) -> Span {
79 DUMMY_SP
80 }
81 }
82
83 impl Key for DefId {
84 fn map_crate(&self) -> CrateNum {
85 self.krate
86 }
87 fn default_span(&self, tcx: TyCtxt) -> Span {
88 tcx.def_span(*self)
89 }
90 }
91
92 impl Key for (DefId, DefId) {
93 fn map_crate(&self) -> CrateNum {
94 self.0.krate
95 }
96 fn default_span(&self, tcx: TyCtxt) -> Span {
97 self.1.default_span(tcx)
98 }
99 }
100
101 impl Key for (CrateNum, DefId) {
102 fn map_crate(&self) -> CrateNum {
103 self.0
104 }
105 fn default_span(&self, tcx: TyCtxt) -> Span {
106 self.1.default_span(tcx)
107 }
108 }
109
110 impl Key for (DefId, SimplifiedType) {
111 fn map_crate(&self) -> CrateNum {
112 self.0.krate
113 }
114 fn default_span(&self, tcx: TyCtxt) -> Span {
115 self.0.default_span(tcx)
116 }
117 }
118
119 impl<'tcx> Key for (DefId, &'tcx Substs<'tcx>) {
120 fn map_crate(&self) -> CrateNum {
121 self.0.krate
122 }
123 fn default_span(&self, tcx: TyCtxt) -> Span {
124 self.0.default_span(tcx)
125 }
126 }
127
128 impl Key for (MirSuite, DefId) {
129 fn map_crate(&self) -> CrateNum {
130 self.1.map_crate()
131 }
132 fn default_span(&self, tcx: TyCtxt) -> Span {
133 self.1.default_span(tcx)
134 }
135 }
136
137 impl Key for (MirSuite, MirPassIndex, DefId) {
138 fn map_crate(&self) -> CrateNum {
139 self.2.map_crate()
140 }
141 fn default_span(&self, tcx: TyCtxt) -> Span {
142 self.2.default_span(tcx)
143 }
144 }
145
146 impl<'tcx, T: Clone + Hash + Eq + Debug> Key for ty::ParamEnvAnd<'tcx, T> {
147 fn map_crate(&self) -> CrateNum {
148 LOCAL_CRATE
149 }
150 fn default_span(&self, _: TyCtxt) -> Span {
151 DUMMY_SP
152 }
153 }
154
155 trait Value<'tcx>: Sized {
156 fn from_cycle_error<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self;
157 }
158
159 impl<'tcx, T> Value<'tcx> for T {
160 default fn from_cycle_error<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> T {
161 tcx.sess.abort_if_errors();
162 bug!("Value::from_cycle_error called without errors");
163 }
164 }
165
166 impl<'tcx, T: Default> Value<'tcx> for T {
167 default fn from_cycle_error<'a>(_: TyCtxt<'a, 'tcx, 'tcx>) -> T {
168 T::default()
169 }
170 }
171
172 impl<'tcx> Value<'tcx> for Ty<'tcx> {
173 fn from_cycle_error<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx> {
174 tcx.types.err
175 }
176 }
177
178 impl<'tcx> Value<'tcx> for ty::DtorckConstraint<'tcx> {
179 fn from_cycle_error<'a>(_: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
180 Self::empty()
181 }
182 }
183
184 impl<'tcx> Value<'tcx> for ty::SymbolName {
185 fn from_cycle_error<'a>(_: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
186 ty::SymbolName { name: Symbol::intern("<error>").as_str() }
187 }
188 }
189
190 struct QueryMap<D: QueryDescription> {
191 phantom: PhantomData<D>,
192 map: FxHashMap<D::Key, QueryValue<D::Value>>,
193 }
194
195 struct QueryValue<T> {
196 value: T,
197 index: DepNodeIndex,
198 diagnostics: Option<Box<QueryDiagnostics>>,
199 }
200
201 struct QueryDiagnostics {
202 diagnostics: Vec<Diagnostic>,
203 emitted_diagnostics: Cell<bool>,
204 }
205
206 impl<M: QueryDescription> QueryMap<M> {
207 fn new() -> QueryMap<M> {
208 QueryMap {
209 phantom: PhantomData,
210 map: FxHashMap(),
211 }
212 }
213 }
214
215 struct CycleError<'a, 'tcx: 'a> {
216 span: Span,
217 cycle: RefMut<'a, [(Span, Query<'tcx>)]>,
218 }
219
220 impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
221 fn report_cycle(self, CycleError { span, cycle }: CycleError)
222 -> DiagnosticBuilder<'a>
223 {
224 // Subtle: release the refcell lock before invoking `describe()`
225 // below by dropping `cycle`.
226 let stack = cycle.to_vec();
227 mem::drop(cycle);
228
229 assert!(!stack.is_empty());
230
231 // Disable naming impls with types in this path, since that
232 // sometimes cycles itself, leading to extra cycle errors.
233 // (And cycle errors around impls tend to occur during the
234 // collect/coherence phases anyhow.)
235 item_path::with_forced_impl_filename_line(|| {
236 let mut err =
237 struct_span_err!(self.sess, span, E0391,
238 "unsupported cyclic reference between types/traits detected");
239 err.span_label(span, "cyclic reference");
240
241 err.span_note(stack[0].0, &format!("the cycle begins when {}...",
242 stack[0].1.describe(self)));
243
244 for &(span, ref query) in &stack[1..] {
245 err.span_note(span, &format!("...which then requires {}...",
246 query.describe(self)));
247 }
248
249 err.note(&format!("...which then again requires {}, completing the cycle.",
250 stack[0].1.describe(self)));
251
252 return err
253 })
254 }
255
256 fn cycle_check<F, R>(self, span: Span, query: Query<'gcx>, compute: F)
257 -> Result<R, CycleError<'a, 'gcx>>
258 where F: FnOnce() -> R
259 {
260 {
261 let mut stack = self.maps.query_stack.borrow_mut();
262 if let Some((i, _)) = stack.iter().enumerate().rev()
263 .find(|&(_, &(_, ref q))| *q == query) {
264 return Err(CycleError {
265 span,
266 cycle: RefMut::map(stack, |stack| &mut stack[i..])
267 });
268 }
269 stack.push((span, query));
270 }
271
272 let result = compute();
273
274 self.maps.query_stack.borrow_mut().pop();
275
276 Ok(result)
277 }
278 }
279
280 pub trait QueryConfig {
281 type Key: Eq + Hash + Clone;
282 type Value;
283 }
284
285 trait QueryDescription: QueryConfig {
286 fn describe(tcx: TyCtxt, key: Self::Key) -> String;
287 }
288
289 impl<M: QueryConfig<Key=DefId>> QueryDescription for M {
290 default fn describe(tcx: TyCtxt, def_id: DefId) -> String {
291 format!("processing `{}`", tcx.item_path_str(def_id))
292 }
293 }
294
295 impl<'tcx> QueryDescription for queries::is_copy_raw<'tcx> {
296 fn describe(_tcx: TyCtxt, env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> String {
297 format!("computing whether `{}` is `Copy`", env.value)
298 }
299 }
300
301 impl<'tcx> QueryDescription for queries::is_sized_raw<'tcx> {
302 fn describe(_tcx: TyCtxt, env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> String {
303 format!("computing whether `{}` is `Sized`", env.value)
304 }
305 }
306
307 impl<'tcx> QueryDescription for queries::is_freeze_raw<'tcx> {
308 fn describe(_tcx: TyCtxt, env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> String {
309 format!("computing whether `{}` is freeze", env.value)
310 }
311 }
312
313 impl<'tcx> QueryDescription for queries::needs_drop_raw<'tcx> {
314 fn describe(_tcx: TyCtxt, env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> String {
315 format!("computing whether `{}` needs drop", env.value)
316 }
317 }
318
319 impl<'tcx> QueryDescription for queries::layout_raw<'tcx> {
320 fn describe(_tcx: TyCtxt, env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> String {
321 format!("computing layout of `{}`", env.value)
322 }
323 }
324
325 impl<'tcx> QueryDescription for queries::super_predicates_of<'tcx> {
326 fn describe(tcx: TyCtxt, def_id: DefId) -> String {
327 format!("computing the supertraits of `{}`",
328 tcx.item_path_str(def_id))
329 }
330 }
331
332 impl<'tcx> QueryDescription for queries::type_param_predicates<'tcx> {
333 fn describe(tcx: TyCtxt, (_, def_id): (DefId, DefId)) -> String {
334 let id = tcx.hir.as_local_node_id(def_id).unwrap();
335 format!("computing the bounds for type parameter `{}`",
336 tcx.hir.ty_param_name(id))
337 }
338 }
339
340 impl<'tcx> QueryDescription for queries::coherent_trait<'tcx> {
341 fn describe(tcx: TyCtxt, (_, def_id): (CrateNum, DefId)) -> String {
342 format!("coherence checking all impls of trait `{}`",
343 tcx.item_path_str(def_id))
344 }
345 }
346
347 impl<'tcx> QueryDescription for queries::crate_inherent_impls<'tcx> {
348 fn describe(_: TyCtxt, k: CrateNum) -> String {
349 format!("all inherent impls defined in crate `{:?}`", k)
350 }
351 }
352
353 impl<'tcx> QueryDescription for queries::crate_inherent_impls_overlap_check<'tcx> {
354 fn describe(_: TyCtxt, _: CrateNum) -> String {
355 format!("check for overlap between inherent impls defined in this crate")
356 }
357 }
358
359 impl<'tcx> QueryDescription for queries::crate_variances<'tcx> {
360 fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
361 format!("computing the variances for items in this crate")
362 }
363 }
364
365 impl<'tcx> QueryDescription for queries::mir_shims<'tcx> {
366 fn describe(tcx: TyCtxt, def: ty::InstanceDef<'tcx>) -> String {
367 format!("generating MIR shim for `{}`",
368 tcx.item_path_str(def.def_id()))
369 }
370 }
371
372 impl<'tcx> QueryDescription for queries::privacy_access_levels<'tcx> {
373 fn describe(_: TyCtxt, _: CrateNum) -> String {
374 format!("privacy access levels")
375 }
376 }
377
378 impl<'tcx> QueryDescription for queries::typeck_item_bodies<'tcx> {
379 fn describe(_: TyCtxt, _: CrateNum) -> String {
380 format!("type-checking all item bodies")
381 }
382 }
383
384 impl<'tcx> QueryDescription for queries::reachable_set<'tcx> {
385 fn describe(_: TyCtxt, _: CrateNum) -> String {
386 format!("reachability")
387 }
388 }
389
390 impl<'tcx> QueryDescription for queries::const_eval<'tcx> {
391 fn describe(tcx: TyCtxt, key: ty::ParamEnvAnd<'tcx, (DefId, &'tcx Substs<'tcx>)>) -> String {
392 format!("const-evaluating `{}`", tcx.item_path_str(key.value.0))
393 }
394 }
395
396 impl<'tcx> QueryDescription for queries::mir_keys<'tcx> {
397 fn describe(_: TyCtxt, _: CrateNum) -> String {
398 format!("getting a list of all mir_keys")
399 }
400 }
401
402 impl<'tcx> QueryDescription for queries::symbol_name<'tcx> {
403 fn describe(_tcx: TyCtxt, instance: ty::Instance<'tcx>) -> String {
404 format!("computing the symbol for `{}`", instance)
405 }
406 }
407
408 impl<'tcx> QueryDescription for queries::describe_def<'tcx> {
409 fn describe(_: TyCtxt, _: DefId) -> String {
410 bug!("describe_def")
411 }
412 }
413
414 impl<'tcx> QueryDescription for queries::def_span<'tcx> {
415 fn describe(_: TyCtxt, _: DefId) -> String {
416 bug!("def_span")
417 }
418 }
419
420
421 impl<'tcx> QueryDescription for queries::stability<'tcx> {
422 fn describe(_: TyCtxt, _: DefId) -> String {
423 bug!("stability")
424 }
425 }
426
427 impl<'tcx> QueryDescription for queries::deprecation<'tcx> {
428 fn describe(_: TyCtxt, _: DefId) -> String {
429 bug!("deprecation")
430 }
431 }
432
433 impl<'tcx> QueryDescription for queries::item_attrs<'tcx> {
434 fn describe(_: TyCtxt, _: DefId) -> String {
435 bug!("item_attrs")
436 }
437 }
438
439 impl<'tcx> QueryDescription for queries::is_exported_symbol<'tcx> {
440 fn describe(_: TyCtxt, _: DefId) -> String {
441 bug!("is_exported_symbol")
442 }
443 }
444
445 impl<'tcx> QueryDescription for queries::fn_arg_names<'tcx> {
446 fn describe(_: TyCtxt, _: DefId) -> String {
447 bug!("fn_arg_names")
448 }
449 }
450
451 impl<'tcx> QueryDescription for queries::impl_parent<'tcx> {
452 fn describe(_: TyCtxt, _: DefId) -> String {
453 bug!("impl_parent")
454 }
455 }
456
457 impl<'tcx> QueryDescription for queries::trait_of_item<'tcx> {
458 fn describe(_: TyCtxt, _: DefId) -> String {
459 bug!("trait_of_item")
460 }
461 }
462
463 impl<'tcx> QueryDescription for queries::item_body_nested_bodies<'tcx> {
464 fn describe(tcx: TyCtxt, def_id: DefId) -> String {
465 format!("nested item bodies of `{}`", tcx.item_path_str(def_id))
466 }
467 }
468
469 impl<'tcx> QueryDescription for queries::const_is_rvalue_promotable_to_static<'tcx> {
470 fn describe(tcx: TyCtxt, def_id: DefId) -> String {
471 format!("const checking if rvalue is promotable to static `{}`",
472 tcx.item_path_str(def_id))
473 }
474 }
475
476 impl<'tcx> QueryDescription for queries::is_mir_available<'tcx> {
477 fn describe(tcx: TyCtxt, def_id: DefId) -> String {
478 format!("checking if item is mir available: `{}`",
479 tcx.item_path_str(def_id))
480 }
481 }
482
483 impl<'tcx> QueryDescription for queries::trait_impls_of<'tcx> {
484 fn describe(tcx: TyCtxt, def_id: DefId) -> String {
485 format!("trait impls of `{}`", tcx.item_path_str(def_id))
486 }
487 }
488
489 impl<'tcx> QueryDescription for queries::is_object_safe<'tcx> {
490 fn describe(tcx: TyCtxt, def_id: DefId) -> String {
491 format!("determine object safety of trait `{}`", tcx.item_path_str(def_id))
492 }
493 }
494
495 impl<'tcx> QueryDescription for queries::is_const_fn<'tcx> {
496 fn describe(tcx: TyCtxt, def_id: DefId) -> String {
497 format!("checking if item is const fn: `{}`", tcx.item_path_str(def_id))
498 }
499 }
500
501 impl<'tcx> QueryDescription for queries::dylib_dependency_formats<'tcx> {
502 fn describe(_: TyCtxt, _: DefId) -> String {
503 "dylib dependency formats of crate".to_string()
504 }
505 }
506
507 impl<'tcx> QueryDescription for queries::is_allocator<'tcx> {
508 fn describe(_: TyCtxt, _: DefId) -> String {
509 "checking if the crate is_allocator".to_string()
510 }
511 }
512
513 impl<'tcx> QueryDescription for queries::is_panic_runtime<'tcx> {
514 fn describe(_: TyCtxt, _: DefId) -> String {
515 "checking if the crate is_panic_runtime".to_string()
516 }
517 }
518
519 impl<'tcx> QueryDescription for queries::is_compiler_builtins<'tcx> {
520 fn describe(_: TyCtxt, _: DefId) -> String {
521 "checking if the crate is_compiler_builtins".to_string()
522 }
523 }
524
525 impl<'tcx> QueryDescription for queries::has_global_allocator<'tcx> {
526 fn describe(_: TyCtxt, _: DefId) -> String {
527 "checking if the crate has_global_allocator".to_string()
528 }
529 }
530
531 impl<'tcx> QueryDescription for queries::extern_crate<'tcx> {
532 fn describe(_: TyCtxt, _: DefId) -> String {
533 "getting crate's ExternCrateData".to_string()
534 }
535 }
536
537 impl<'tcx> QueryDescription for queries::lint_levels<'tcx> {
538 fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
539 format!("computing the lint levels for items in this crate")
540 }
541 }
542
543 // If enabled, send a message to the profile-queries thread
544 macro_rules! profq_msg {
545 ($tcx:expr, $msg:expr) => {
546 if cfg!(debug_assertions) {
547 if $tcx.sess.profile_queries() {
548 profq_msg($msg)
549 }
550 }
551 }
552 }
553
554 // If enabled, format a key using its debug string, which can be
555 // expensive to compute (in terms of time).
556 macro_rules! profq_key {
557 ($tcx:expr, $key:expr) => {
558 if cfg!(debug_assertions) {
559 if $tcx.sess.profile_queries_and_keys() {
560 Some(format!("{:?}", $key))
561 } else { None }
562 } else { None }
563 }
564 }
565
566 macro_rules! define_maps {
567 (<$tcx:tt>
568 $($(#[$attr:meta])*
569 [$($modifiers:tt)*] $name:ident: $node:ident($K:ty) -> $V:ty,)*) => {
570 define_map_struct! {
571 tcx: $tcx,
572 input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
573 }
574
575 impl<$tcx> Maps<$tcx> {
576 pub fn new(providers: IndexVec<CrateNum, Providers<$tcx>>)
577 -> Self {
578 Maps {
579 providers,
580 query_stack: RefCell::new(vec![]),
581 $($name: RefCell::new(QueryMap::new())),*
582 }
583 }
584 }
585
586 #[allow(bad_style)]
587 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
588 pub enum Query<$tcx> {
589 $($(#[$attr])* $name($K)),*
590 }
591
592 #[allow(bad_style)]
593 #[derive(Clone, Debug, PartialEq, Eq)]
594 pub enum QueryMsg {
595 $($name(Option<String>)),*
596 }
597
598 impl<$tcx> Query<$tcx> {
599 pub fn describe(&self, tcx: TyCtxt) -> String {
600 match *self {
601 $(Query::$name(key) => queries::$name::describe(tcx, key)),*
602 }
603 }
604 }
605
606 pub mod queries {
607 use std::marker::PhantomData;
608
609 $(#[allow(bad_style)]
610 pub struct $name<$tcx> {
611 data: PhantomData<&$tcx ()>
612 })*
613 }
614
615 $(impl<$tcx> QueryConfig for queries::$name<$tcx> {
616 type Key = $K;
617 type Value = $V;
618 }
619
620 impl<'a, $tcx, 'lcx> queries::$name<$tcx> {
621 #[allow(unused)]
622 fn to_dep_node(tcx: TyCtxt<'a, $tcx, 'lcx>, key: &$K) -> DepNode {
623 use dep_graph::DepConstructor::*;
624
625 DepNode::new(tcx, $node(*key))
626 }
627
628 fn try_get_with<F, R>(tcx: TyCtxt<'a, $tcx, 'lcx>,
629 mut span: Span,
630 key: $K,
631 f: F)
632 -> Result<R, CycleError<'a, $tcx>>
633 where F: FnOnce(&$V) -> R
634 {
635 debug!("ty::queries::{}::try_get_with(key={:?}, span={:?})",
636 stringify!($name),
637 key,
638 span);
639
640 profq_msg!(tcx,
641 ProfileQueriesMsg::QueryBegin(
642 span.clone(),
643 QueryMsg::$name(profq_key!(tcx, key))
644 )
645 );
646
647 if let Some(value) = tcx.maps.$name.borrow().map.get(&key) {
648 if let Some(ref d) = value.diagnostics {
649 if !d.emitted_diagnostics.get() {
650 d.emitted_diagnostics.set(true);
651 let handle = tcx.sess.diagnostic();
652 for diagnostic in d.diagnostics.iter() {
653 DiagnosticBuilder::new_diagnostic(handle, diagnostic.clone())
654 .emit();
655 }
656 }
657 }
658 profq_msg!(tcx, ProfileQueriesMsg::CacheHit);
659 tcx.dep_graph.read_index(value.index);
660 return Ok(f(&value.value));
661 }
662 // else, we are going to run the provider:
663 profq_msg!(tcx, ProfileQueriesMsg::ProviderBegin);
664
665 // FIXME(eddyb) Get more valid Span's on queries.
666 // def_span guard is necessary to prevent a recursive loop,
667 // default_span calls def_span query internally.
668 if span == DUMMY_SP && stringify!($name) != "def_span" {
669 span = key.default_span(tcx)
670 }
671
672 let res = tcx.cycle_check(span, Query::$name(key), || {
673 let dep_node = Self::to_dep_node(tcx, &key);
674
675 tcx.sess.diagnostic().track_diagnostics(|| {
676 if dep_node.kind.is_anon() {
677 tcx.dep_graph.with_anon_task(dep_node.kind, || {
678 let provider = tcx.maps.providers[key.map_crate()].$name;
679 provider(tcx.global_tcx(), key)
680 })
681 } else {
682 fn run_provider<'a, 'tcx, 'lcx>(tcx: TyCtxt<'a, 'tcx, 'lcx>,
683 key: $K)
684 -> $V {
685 let provider = tcx.maps.providers[key.map_crate()].$name;
686 provider(tcx.global_tcx(), key)
687 }
688
689 tcx.dep_graph.with_task(dep_node, tcx, key, run_provider)
690 }
691 })
692 })?;
693 profq_msg!(tcx, ProfileQueriesMsg::ProviderEnd);
694 let ((result, dep_node_index), diagnostics) = res;
695
696 tcx.dep_graph.read_index(dep_node_index);
697
698 let value = QueryValue {
699 value: result,
700 index: dep_node_index,
701 diagnostics: if diagnostics.len() == 0 {
702 None
703 } else {
704 Some(Box::new(QueryDiagnostics {
705 diagnostics,
706 emitted_diagnostics: Cell::new(true),
707 }))
708 },
709 };
710
711 Ok(f(&tcx.maps
712 .$name
713 .borrow_mut()
714 .map
715 .entry(key)
716 .or_insert(value)
717 .value))
718 }
719
720 pub fn try_get(tcx: TyCtxt<'a, $tcx, 'lcx>, span: Span, key: $K)
721 -> Result<$V, DiagnosticBuilder<'a>> {
722 match Self::try_get_with(tcx, span, key, Clone::clone) {
723 Ok(e) => Ok(e),
724 Err(e) => Err(tcx.report_cycle(e)),
725 }
726 }
727
728 pub fn force(tcx: TyCtxt<'a, $tcx, 'lcx>, span: Span, key: $K) {
729 // Ignore dependencies, since we not reading the computed value
730 let _task = tcx.dep_graph.in_ignore();
731
732 match Self::try_get_with(tcx, span, key, |_| ()) {
733 Ok(()) => {}
734 Err(e) => tcx.report_cycle(e).emit(),
735 }
736 }
737 })*
738
739 #[derive(Copy, Clone)]
740 pub struct TyCtxtAt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
741 pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
742 pub span: Span,
743 }
744
745 impl<'a, 'gcx, 'tcx> Deref for TyCtxtAt<'a, 'gcx, 'tcx> {
746 type Target = TyCtxt<'a, 'gcx, 'tcx>;
747 fn deref(&self) -> &Self::Target {
748 &self.tcx
749 }
750 }
751
752 impl<'a, $tcx, 'lcx> TyCtxt<'a, $tcx, 'lcx> {
753 /// Return a transparent wrapper for `TyCtxt` which uses
754 /// `span` as the location of queries performed through it.
755 pub fn at(self, span: Span) -> TyCtxtAt<'a, $tcx, 'lcx> {
756 TyCtxtAt {
757 tcx: self,
758 span
759 }
760 }
761
762 $($(#[$attr])*
763 pub fn $name(self, key: $K) -> $V {
764 self.at(DUMMY_SP).$name(key)
765 })*
766 }
767
768 impl<'a, $tcx, 'lcx> TyCtxtAt<'a, $tcx, 'lcx> {
769 $($(#[$attr])*
770 pub fn $name(self, key: $K) -> $V {
771 queries::$name::try_get(self.tcx, self.span, key).unwrap_or_else(|mut e| {
772 e.emit();
773 Value::from_cycle_error(self.global_tcx())
774 })
775 })*
776 }
777
778 define_provider_struct! {
779 tcx: $tcx,
780 input: ($(([$($modifiers)*] [$name] [$K] [$V]))*),
781 output: ()
782 }
783
784 impl<$tcx> Copy for Providers<$tcx> {}
785 impl<$tcx> Clone for Providers<$tcx> {
786 fn clone(&self) -> Self { *self }
787 }
788 }
789 }
790
791 macro_rules! define_map_struct {
792 // Initial state
793 (tcx: $tcx:tt,
794 input: $input:tt) => {
795 define_map_struct! {
796 tcx: $tcx,
797 input: $input,
798 output: ()
799 }
800 };
801
802 // Final output
803 (tcx: $tcx:tt,
804 input: (),
805 output: ($($output:tt)*)) => {
806 pub struct Maps<$tcx> {
807 providers: IndexVec<CrateNum, Providers<$tcx>>,
808 query_stack: RefCell<Vec<(Span, Query<$tcx>)>>,
809 $($output)*
810 }
811 };
812
813 // Field recognized and ready to shift into the output
814 (tcx: $tcx:tt,
815 ready: ([$($pub:tt)*] [$($attr:tt)*] [$name:ident]),
816 input: $input:tt,
817 output: ($($output:tt)*)) => {
818 define_map_struct! {
819 tcx: $tcx,
820 input: $input,
821 output: ($($output)*
822 $(#[$attr])* $($pub)* $name: RefCell<QueryMap<queries::$name<$tcx>>>,)
823 }
824 };
825
826 // No modifiers left? This is a private item.
827 (tcx: $tcx:tt,
828 input: (([] $attrs:tt $name:tt) $($input:tt)*),
829 output: $output:tt) => {
830 define_map_struct! {
831 tcx: $tcx,
832 ready: ([] $attrs $name),
833 input: ($($input)*),
834 output: $output
835 }
836 };
837
838 // Skip other modifiers
839 (tcx: $tcx:tt,
840 input: (([$other_modifier:tt $($modifiers:tt)*] $($fields:tt)*) $($input:tt)*),
841 output: $output:tt) => {
842 define_map_struct! {
843 tcx: $tcx,
844 input: (([$($modifiers)*] $($fields)*) $($input)*),
845 output: $output
846 }
847 };
848 }
849
850 macro_rules! define_provider_struct {
851 // Initial state:
852 (tcx: $tcx:tt, input: $input:tt) => {
853 define_provider_struct! {
854 tcx: $tcx,
855 input: $input,
856 output: ()
857 }
858 };
859
860 // Final state:
861 (tcx: $tcx:tt,
862 input: (),
863 output: ($(([$name:ident] [$K:ty] [$R:ty]))*)) => {
864 pub struct Providers<$tcx> {
865 $(pub $name: for<'a> fn(TyCtxt<'a, $tcx, $tcx>, $K) -> $R,)*
866 }
867
868 impl<$tcx> Default for Providers<$tcx> {
869 fn default() -> Self {
870 $(fn $name<'a, $tcx>(_: TyCtxt<'a, $tcx, $tcx>, key: $K) -> $R {
871 bug!("tcx.maps.{}({:?}) unsupported by its crate",
872 stringify!($name), key);
873 })*
874 Providers { $($name),* }
875 }
876 }
877 };
878
879 // Something ready to shift:
880 (tcx: $tcx:tt,
881 ready: ($name:tt $K:tt $V:tt),
882 input: $input:tt,
883 output: ($($output:tt)*)) => {
884 define_provider_struct! {
885 tcx: $tcx,
886 input: $input,
887 output: ($($output)* ($name $K $V))
888 }
889 };
890
891 // Regular queries produce a `V` only.
892 (tcx: $tcx:tt,
893 input: (([] $name:tt $K:tt $V:tt) $($input:tt)*),
894 output: $output:tt) => {
895 define_provider_struct! {
896 tcx: $tcx,
897 ready: ($name $K $V),
898 input: ($($input)*),
899 output: $output
900 }
901 };
902
903 // Skip modifiers.
904 (tcx: $tcx:tt,
905 input: (([$other_modifier:tt $($modifiers:tt)*] $($fields:tt)*) $($input:tt)*),
906 output: $output:tt) => {
907 define_provider_struct! {
908 tcx: $tcx,
909 input: (([$($modifiers)*] $($fields)*) $($input)*),
910 output: $output
911 }
912 };
913 }
914
915 // Each of these maps also corresponds to a method on a
916 // `Provider` trait for requesting a value of that type,
917 // and a method on `Maps` itself for doing that in a
918 // a way that memoizes and does dep-graph tracking,
919 // wrapping around the actual chain of providers that
920 // the driver creates (using several `rustc_*` crates).
921 define_maps! { <'tcx>
922 /// Records the type of every item.
923 [] type_of: TypeOfItem(DefId) -> Ty<'tcx>,
924
925 /// Maps from the def-id of an item (trait/struct/enum/fn) to its
926 /// associated generics and predicates.
927 [] generics_of: GenericsOfItem(DefId) -> &'tcx ty::Generics,
928 [] predicates_of: PredicatesOfItem(DefId) -> ty::GenericPredicates<'tcx>,
929
930 /// Maps from the def-id of a trait to the list of
931 /// super-predicates. This is a subset of the full list of
932 /// predicates. We store these in a separate map because we must
933 /// evaluate them even during type conversion, often before the
934 /// full predicates are available (note that supertraits have
935 /// additional acyclicity requirements).
936 [] super_predicates_of: SuperPredicatesOfItem(DefId) -> ty::GenericPredicates<'tcx>,
937
938 /// To avoid cycles within the predicates of a single item we compute
939 /// per-type-parameter predicates for resolving `T::AssocTy`.
940 [] type_param_predicates: type_param_predicates((DefId, DefId))
941 -> ty::GenericPredicates<'tcx>,
942
943 [] trait_def: TraitDefOfItem(DefId) -> &'tcx ty::TraitDef,
944 [] adt_def: AdtDefOfItem(DefId) -> &'tcx ty::AdtDef,
945 [] adt_destructor: AdtDestructor(DefId) -> Option<ty::Destructor>,
946 [] adt_sized_constraint: SizedConstraint(DefId) -> &'tcx [Ty<'tcx>],
947 [] adt_dtorck_constraint: DtorckConstraint(DefId) -> ty::DtorckConstraint<'tcx>,
948
949 /// True if this is a const fn
950 [] is_const_fn: IsConstFn(DefId) -> bool,
951
952 /// True if this is a foreign item (i.e., linked via `extern { ... }`).
953 [] is_foreign_item: IsForeignItem(DefId) -> bool,
954
955 /// True if this is a default impl (aka impl Foo for ..)
956 [] is_default_impl: IsDefaultImpl(DefId) -> bool,
957
958 /// Get a map with the variance of every item; use `item_variance`
959 /// instead.
960 [] crate_variances: crate_variances(CrateNum) -> Rc<ty::CrateVariancesMap>,
961
962 /// Maps from def-id of a type or region parameter to its
963 /// (inferred) variance.
964 [] variances_of: ItemVariances(DefId) -> Rc<Vec<ty::Variance>>,
965
966 /// Maps from an impl/trait def-id to a list of the def-ids of its items
967 [] associated_item_def_ids: AssociatedItemDefIds(DefId) -> Rc<Vec<DefId>>,
968
969 /// Maps from a trait item to the trait item "descriptor"
970 [] associated_item: AssociatedItems(DefId) -> ty::AssociatedItem,
971
972 [] impl_trait_ref: ImplTraitRef(DefId) -> Option<ty::TraitRef<'tcx>>,
973 [] impl_polarity: ImplPolarity(DefId) -> hir::ImplPolarity,
974
975 /// Maps a DefId of a type to a list of its inherent impls.
976 /// Contains implementations of methods that are inherent to a type.
977 /// Methods in these implementations don't need to be exported.
978 [] inherent_impls: InherentImpls(DefId) -> Rc<Vec<DefId>>,
979
980 /// Set of all the def-ids in this crate that have MIR associated with
981 /// them. This includes all the body owners, but also things like struct
982 /// constructors.
983 [] mir_keys: mir_keys(CrateNum) -> Rc<DefIdSet>,
984
985 /// Maps DefId's that have an associated Mir to the result
986 /// of the MIR qualify_consts pass. The actual meaning of
987 /// the value isn't known except to the pass itself.
988 [] mir_const_qualif: MirConstQualif(DefId) -> u8,
989
990 /// Fetch the MIR for a given def-id up till the point where it is
991 /// ready for const evaluation.
992 ///
993 /// See the README for the `mir` module for details.
994 [] mir_const: MirConst(DefId) -> &'tcx Steal<mir::Mir<'tcx>>,
995
996 [] mir_validated: MirValidated(DefId) -> &'tcx Steal<mir::Mir<'tcx>>,
997
998 /// MIR after our optimization passes have run. This is MIR that is ready
999 /// for trans. This is also the only query that can fetch non-local MIR, at present.
1000 [] optimized_mir: MirOptimized(DefId) -> &'tcx mir::Mir<'tcx>,
1001
1002 /// Type of each closure. The def ID is the ID of the
1003 /// expression defining the closure.
1004 [] closure_kind: ClosureKind(DefId) -> ty::ClosureKind,
1005
1006 /// The signature of functions and closures.
1007 [] fn_sig: FnSignature(DefId) -> ty::PolyFnSig<'tcx>,
1008
1009 /// Caches CoerceUnsized kinds for impls on custom types.
1010 [] coerce_unsized_info: CoerceUnsizedInfo(DefId)
1011 -> ty::adjustment::CoerceUnsizedInfo,
1012
1013 [] typeck_item_bodies: typeck_item_bodies_dep_node(CrateNum) -> CompileResult,
1014
1015 [] typeck_tables_of: TypeckTables(DefId) -> &'tcx ty::TypeckTables<'tcx>,
1016
1017 [] has_typeck_tables: HasTypeckTables(DefId) -> bool,
1018
1019 [] coherent_trait: coherent_trait_dep_node((CrateNum, DefId)) -> (),
1020
1021 [] borrowck: BorrowCheck(DefId) -> (),
1022 // FIXME: shouldn't this return a `Result<(), BorrowckErrors>` instead?
1023 [] mir_borrowck: MirBorrowCheck(DefId) -> (),
1024
1025 /// Gets a complete map from all types to their inherent impls.
1026 /// Not meant to be used directly outside of coherence.
1027 /// (Defined only for LOCAL_CRATE)
1028 [] crate_inherent_impls: crate_inherent_impls_dep_node(CrateNum) -> CrateInherentImpls,
1029
1030 /// Checks all types in the krate for overlap in their inherent impls. Reports errors.
1031 /// Not meant to be used directly outside of coherence.
1032 /// (Defined only for LOCAL_CRATE)
1033 [] crate_inherent_impls_overlap_check: inherent_impls_overlap_check_dep_node(CrateNum) -> (),
1034
1035 /// Results of evaluating const items or constants embedded in
1036 /// other items (such as enum variant explicit discriminants).
1037 [] const_eval: const_eval_dep_node(ty::ParamEnvAnd<'tcx, (DefId, &'tcx Substs<'tcx>)>)
1038 -> const_val::EvalResult<'tcx>,
1039
1040 /// Performs the privacy check and computes "access levels".
1041 [] privacy_access_levels: PrivacyAccessLevels(CrateNum) -> Rc<AccessLevels>,
1042
1043 [] reachable_set: reachability_dep_node(CrateNum) -> Rc<NodeSet>,
1044
1045 /// Per-function `RegionMaps`. The `DefId` should be the owner-def-id for the fn body;
1046 /// in the case of closures or "inline" expressions, this will be redirected to the enclosing
1047 /// fn item.
1048 [] region_maps: RegionMaps(DefId) -> Rc<RegionMaps>,
1049
1050 [] mir_shims: mir_shim_dep_node(ty::InstanceDef<'tcx>) -> &'tcx mir::Mir<'tcx>,
1051
1052 [] def_symbol_name: SymbolName(DefId) -> ty::SymbolName,
1053 [] symbol_name: symbol_name_dep_node(ty::Instance<'tcx>) -> ty::SymbolName,
1054
1055 [] describe_def: DescribeDef(DefId) -> Option<Def>,
1056 [] def_span: DefSpan(DefId) -> Span,
1057 [] stability: Stability(DefId) -> Option<attr::Stability>,
1058 [] deprecation: Deprecation(DefId) -> Option<attr::Deprecation>,
1059 [] item_attrs: ItemAttrs(DefId) -> Rc<[ast::Attribute]>,
1060 [] fn_arg_names: FnArgNames(DefId) -> Vec<ast::Name>,
1061 [] impl_parent: ImplParent(DefId) -> Option<DefId>,
1062 [] trait_of_item: TraitOfItem(DefId) -> Option<DefId>,
1063 [] is_exported_symbol: IsExportedSymbol(DefId) -> bool,
1064 [] item_body_nested_bodies: ItemBodyNestedBodies(DefId) -> Rc<BTreeMap<hir::BodyId, hir::Body>>,
1065 [] const_is_rvalue_promotable_to_static: ConstIsRvaluePromotableToStatic(DefId) -> bool,
1066 [] is_mir_available: IsMirAvailable(DefId) -> bool,
1067
1068 [] trait_impls_of: TraitImpls(DefId) -> Rc<ty::trait_def::TraitImpls>,
1069 [] specialization_graph_of: SpecializationGraph(DefId) -> Rc<specialization_graph::Graph>,
1070 [] is_object_safe: ObjectSafety(DefId) -> bool,
1071
1072 // Get the ParameterEnvironment for a given item; this environment
1073 // will be in "user-facing" mode, meaning that it is suitabe for
1074 // type-checking etc, and it does not normalize specializable
1075 // associated types. This is almost always what you want,
1076 // unless you are doing MIR optimizations, in which case you
1077 // might want to use `reveal_all()` method to change modes.
1078 [] param_env: ParamEnv(DefId) -> ty::ParamEnv<'tcx>,
1079
1080 // Trait selection queries. These are best used by invoking `ty.moves_by_default()`,
1081 // `ty.is_copy()`, etc, since that will prune the environment where possible.
1082 [] is_copy_raw: is_copy_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool,
1083 [] is_sized_raw: is_sized_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool,
1084 [] is_freeze_raw: is_freeze_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool,
1085 [] needs_drop_raw: needs_drop_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool,
1086 [] layout_raw: layout_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
1087 -> Result<&'tcx Layout, LayoutError<'tcx>>,
1088
1089 [] dylib_dependency_formats: DylibDepFormats(DefId)
1090 -> Rc<Vec<(CrateNum, LinkagePreference)>>,
1091
1092 [] is_allocator: IsAllocator(DefId) -> bool,
1093 [] is_panic_runtime: IsPanicRuntime(DefId) -> bool,
1094 [] is_compiler_builtins: IsCompilerBuiltins(DefId) -> bool,
1095 [] has_global_allocator: HasGlobalAllocator(DefId) -> bool,
1096
1097 [] extern_crate: ExternCrate(DefId) -> Rc<Option<ExternCrate>>,
1098
1099 [] lint_levels: lint_levels(CrateNum) -> Rc<lint::LintLevelMap>,
1100 }
1101
1102 fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {
1103 DepConstructor::TypeParamPredicates {
1104 item_id,
1105 param_id
1106 }
1107 }
1108
1109 fn coherent_trait_dep_node<'tcx>((_, def_id): (CrateNum, DefId)) -> DepConstructor<'tcx> {
1110 DepConstructor::CoherenceCheckTrait(def_id)
1111 }
1112
1113 fn crate_inherent_impls_dep_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
1114 DepConstructor::Coherence
1115 }
1116
1117 fn inherent_impls_overlap_check_dep_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
1118 DepConstructor::CoherenceInherentImplOverlapCheck
1119 }
1120
1121 fn reachability_dep_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
1122 DepConstructor::Reachability
1123 }
1124
1125 fn mir_shim_dep_node<'tcx>(instance_def: ty::InstanceDef<'tcx>) -> DepConstructor<'tcx> {
1126 DepConstructor::MirShim {
1127 instance_def
1128 }
1129 }
1130
1131 fn symbol_name_dep_node<'tcx>(instance: ty::Instance<'tcx>) -> DepConstructor<'tcx> {
1132 DepConstructor::InstanceSymbolName { instance }
1133 }
1134
1135 fn typeck_item_bodies_dep_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
1136 DepConstructor::TypeckBodiesKrate
1137 }
1138
1139 fn const_eval_dep_node<'tcx>(_: ty::ParamEnvAnd<'tcx, (DefId, &'tcx Substs<'tcx>)>)
1140 -> DepConstructor<'tcx> {
1141 DepConstructor::ConstEval
1142 }
1143
1144 fn mir_keys<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
1145 DepConstructor::MirKeys
1146 }
1147
1148 fn crate_variances<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
1149 DepConstructor::CrateVariances
1150 }
1151
1152 fn is_copy_dep_node<'tcx>(_: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> DepConstructor<'tcx> {
1153 DepConstructor::IsCopy
1154 }
1155
1156 fn is_sized_dep_node<'tcx>(_: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> DepConstructor<'tcx> {
1157 DepConstructor::IsSized
1158 }
1159
1160 fn is_freeze_dep_node<'tcx>(_: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> DepConstructor<'tcx> {
1161 DepConstructor::IsFreeze
1162 }
1163
1164 fn needs_drop_dep_node<'tcx>(_: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> DepConstructor<'tcx> {
1165 DepConstructor::NeedsDrop
1166 }
1167
1168 fn layout_dep_node<'tcx>(_: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> DepConstructor<'tcx> {
1169 DepConstructor::Layout
1170 }
1171
1172 fn lint_levels<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
1173 DepConstructor::LintLevels
1174 }