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.
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.
11 use dep_graph
::{DepConstructor, DepNode, DepNodeIndex}
;
12 use errors
::{Diagnostic, DiagnosticBuilder}
;
13 use hir
::def_id
::{CrateNum, DefId, LOCAL_CRATE}
;
17 use middle
::const_val
;
18 use middle
::cstore
::{ExternCrate, LinkagePreference}
;
19 use middle
::privacy
::AccessLevels
;
20 use middle
::region
::RegionMaps
;
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}
;
29 use ty
::subst
::Substs
;
30 use ty
::fast_reject
::SimplifiedType
;
31 use util
::nodemap
::{DefIdSet, NodeSet}
;
32 use util
::common
::{profq_msg, ProfileQueriesMsg}
;
34 use rustc_data_structures
::indexed_vec
::IndexVec
;
35 use rustc_data_structures
::fx
::FxHashMap
;
36 use std
::cell
::{RefCell, RefMut, Cell}
;
39 use std
::marker
::PhantomData
;
41 use std
::collections
::BTreeMap
;
44 use syntax_pos
::{Span, DUMMY_SP}
;
47 use syntax
::symbol
::Symbol
;
49 pub trait Key
: Clone
+ Hash
+ Eq
+ Debug
{
50 fn map_crate(&self) -> CrateNum
;
51 fn default_span(&self, tcx
: TyCtxt
) -> Span
;
54 impl<'tcx
> Key
for ty
::InstanceDef
<'tcx
> {
55 fn map_crate(&self) -> CrateNum
{
59 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
60 tcx
.def_span(self.def_id())
64 impl<'tcx
> Key
for ty
::Instance
<'tcx
> {
65 fn map_crate(&self) -> CrateNum
{
69 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
70 tcx
.def_span(self.def_id())
74 impl Key
for CrateNum
{
75 fn map_crate(&self) -> CrateNum
{
78 fn default_span(&self, _
: TyCtxt
) -> Span
{
84 fn map_crate(&self) -> CrateNum
{
87 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
92 impl Key
for (DefId
, DefId
) {
93 fn map_crate(&self) -> CrateNum
{
96 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
97 self.1.default_span(tcx
)
101 impl Key
for (CrateNum
, DefId
) {
102 fn map_crate(&self) -> CrateNum
{
105 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
106 self.1.default_span(tcx
)
110 impl Key
for (DefId
, SimplifiedType
) {
111 fn map_crate(&self) -> CrateNum
{
114 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
115 self.0.default_span(tcx
)
119 impl<'tcx
> Key
for (DefId
, &'tcx Substs
<'tcx
>) {
120 fn map_crate(&self) -> CrateNum
{
123 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
124 self.0.default_span(tcx
)
128 impl Key
for (MirSuite
, DefId
) {
129 fn map_crate(&self) -> CrateNum
{
132 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
133 self.1.default_span(tcx
)
137 impl Key
for (MirSuite
, MirPassIndex
, DefId
) {
138 fn map_crate(&self) -> CrateNum
{
141 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
142 self.2.default_span(tcx
)
146 impl<'tcx
, T
: Clone
+ Hash
+ Eq
+ Debug
> Key
for ty
::ParamEnvAnd
<'tcx
, T
> {
147 fn map_crate(&self) -> CrateNum
{
150 fn default_span(&self, _
: TyCtxt
) -> Span
{
155 trait Value
<'tcx
>: Sized
{
156 fn from_cycle_error
<'a
>(tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>) -> Self;
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");
166 impl<'tcx
, T
: Default
> Value
<'tcx
> for T
{
167 default fn from_cycle_error
<'a
>(_
: TyCtxt
<'a
, 'tcx
, 'tcx
>) -> T
{
172 impl<'tcx
> Value
<'tcx
> for Ty
<'tcx
> {
173 fn from_cycle_error
<'a
>(tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>) -> Ty
<'tcx
> {
178 impl<'tcx
> Value
<'tcx
> for ty
::DtorckConstraint
<'tcx
> {
179 fn from_cycle_error
<'a
>(_
: TyCtxt
<'a
, 'tcx
, 'tcx
>) -> Self {
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() }
190 struct QueryMap
<D
: QueryDescription
> {
191 phantom
: PhantomData
<D
>,
192 map
: FxHashMap
<D
::Key
, QueryValue
<D
::Value
>>,
195 struct QueryValue
<T
> {
198 diagnostics
: Option
<Box
<QueryDiagnostics
>>,
201 struct QueryDiagnostics
{
202 diagnostics
: Vec
<Diagnostic
>,
203 emitted_diagnostics
: Cell
<bool
>,
206 impl<M
: QueryDescription
> QueryMap
<M
> {
207 fn new() -> QueryMap
<M
> {
209 phantom
: PhantomData
,
215 struct CycleError
<'a
, 'tcx
: 'a
> {
217 cycle
: RefMut
<'a
, [(Span
, Query
<'tcx
>)]>,
220 impl<'a
, 'gcx
, 'tcx
> TyCtxt
<'a
, 'gcx
, 'tcx
> {
221 fn report_cycle(self, CycleError { span, cycle }
: CycleError
)
222 -> DiagnosticBuilder
<'a
>
224 // Subtle: release the refcell lock before invoking `describe()`
225 // below by dropping `cycle`.
226 let stack
= cycle
.to_vec();
229 assert
!(!stack
.is_empty());
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(|| {
237 struct_span_err
!(self.sess
, span
, E0391
,
238 "unsupported cyclic reference between types/traits detected");
239 err
.span_label(span
, "cyclic reference");
241 err
.span_note(stack
[0].0, &format
!("the cycle begins when {}...",
242 stack
[0].1.describe(self)));
244 for &(span
, ref query
) in &stack
[1..] {
245 err
.span_note(span
, &format
!("...which then requires {}...",
246 query
.describe(self)));
249 err
.note(&format
!("...which then again requires {}, completing the cycle.",
250 stack
[0].1.describe(self)));
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
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
{
266 cycle
: RefMut
::map(stack
, |stack
| &mut stack
[i
..])
269 stack
.push((span
, query
));
272 let result
= compute();
274 self.maps
.query_stack
.borrow_mut().pop();
280 pub trait QueryConfig
{
281 type Key
: Eq
+ Hash
+ Clone
;
285 trait QueryDescription
: QueryConfig
{
286 fn describe(tcx
: TyCtxt
, key
: Self::Key
) -> String
;
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
))
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
)
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
)
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
)
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
)
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
)
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
))
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
))
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
))
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
)
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")
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")
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()))
372 impl<'tcx
> QueryDescription
for queries
::privacy_access_levels
<'tcx
> {
373 fn describe(_
: TyCtxt
, _
: CrateNum
) -> String
{
374 format
!("privacy access levels")
378 impl<'tcx
> QueryDescription
for queries
::typeck_item_bodies
<'tcx
> {
379 fn describe(_
: TyCtxt
, _
: CrateNum
) -> String
{
380 format
!("type-checking all item bodies")
384 impl<'tcx
> QueryDescription
for queries
::reachable_set
<'tcx
> {
385 fn describe(_
: TyCtxt
, _
: CrateNum
) -> String
{
386 format
!("reachability")
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))
396 impl<'tcx
> QueryDescription
for queries
::mir_keys
<'tcx
> {
397 fn describe(_
: TyCtxt
, _
: CrateNum
) -> String
{
398 format
!("getting a list of all mir_keys")
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
)
408 impl<'tcx
> QueryDescription
for queries
::describe_def
<'tcx
> {
409 fn describe(_
: TyCtxt
, _
: DefId
) -> String
{
414 impl<'tcx
> QueryDescription
for queries
::def_span
<'tcx
> {
415 fn describe(_
: TyCtxt
, _
: DefId
) -> String
{
421 impl<'tcx
> QueryDescription
for queries
::stability
<'tcx
> {
422 fn describe(_
: TyCtxt
, _
: DefId
) -> String
{
427 impl<'tcx
> QueryDescription
for queries
::deprecation
<'tcx
> {
428 fn describe(_
: TyCtxt
, _
: DefId
) -> String
{
433 impl<'tcx
> QueryDescription
for queries
::item_attrs
<'tcx
> {
434 fn describe(_
: TyCtxt
, _
: DefId
) -> String
{
439 impl<'tcx
> QueryDescription
for queries
::is_exported_symbol
<'tcx
> {
440 fn describe(_
: TyCtxt
, _
: DefId
) -> String
{
441 bug
!("is_exported_symbol")
445 impl<'tcx
> QueryDescription
for queries
::fn_arg_names
<'tcx
> {
446 fn describe(_
: TyCtxt
, _
: DefId
) -> String
{
451 impl<'tcx
> QueryDescription
for queries
::impl_parent
<'tcx
> {
452 fn describe(_
: TyCtxt
, _
: DefId
) -> String
{
457 impl<'tcx
> QueryDescription
for queries
::trait_of_item
<'tcx
> {
458 fn describe(_
: TyCtxt
, _
: DefId
) -> String
{
459 bug
!("trait_of_item")
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
))
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
))
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
))
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
))
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
))
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
))
501 impl<'tcx
> QueryDescription
for queries
::dylib_dependency_formats
<'tcx
> {
502 fn describe(_
: TyCtxt
, _
: DefId
) -> String
{
503 "dylib dependency formats of crate".to_string()
507 impl<'tcx
> QueryDescription
for queries
::is_allocator
<'tcx
> {
508 fn describe(_
: TyCtxt
, _
: DefId
) -> String
{
509 "checking if the crate is_allocator".to_string()
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()
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()
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()
531 impl<'tcx
> QueryDescription
for queries
::extern_crate
<'tcx
> {
532 fn describe(_
: TyCtxt
, _
: DefId
) -> String
{
533 "getting crate's ExternCrateData".to_string()
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")
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() {
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
))
566 macro_rules
! define_maps
{
569 [$
($modifiers
:tt
)*] $name
:ident
: $node
:ident($K
:ty
) -> $V
:ty
,)*) => {
572 input
: ($
(([$
($modifiers
)*] [$
($attr
)*] [$name
]))*)
575 impl<$tcx
> Maps
<$tcx
> {
576 pub fn new(providers
: IndexVec
<CrateNum
, Providers
<$tcx
>>)
580 query_stack
: RefCell
::new(vec
![]),
581 $
($name
: RefCell
::new(QueryMap
::new())),*
587 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
588 pub enum Query
<$tcx
> {
589 $
($
(#[$attr])* $name($K)),*
593 #[derive(Clone, Debug, PartialEq, Eq)]
595 $
($
name(Option
<String
>)),*
598 impl<$tcx
> Query
<$tcx
> {
599 pub fn describe(&self, tcx
: TyCtxt
) -> String
{
601 $
(Query
::$
name(key
) => queries
::$name
::describe(tcx
, key
)),*
607 use std
::marker
::PhantomData
;
609 $
(#[allow(bad_style)]
610 pub struct $name
<$tcx
> {
611 data
: PhantomData
<&$
tcx ()>
615 $
(impl<$tcx
> QueryConfig
for queries
::$name
<$tcx
> {
620 impl<'a
, $tcx
, 'lcx
> queries
::$name
<$tcx
> {
622 fn to_dep_node(tcx
: TyCtxt
<'a
, $tcx
, 'lcx
>, key
: &$K
) -> DepNode
{
623 use dep_graph
::DepConstructor
::*;
625 DepNode
::new(tcx
, $
node(*key
))
628 fn try_get_with
<F
, R
>(tcx
: TyCtxt
<'a
, $tcx
, 'lcx
>,
632 -> Result
<R
, CycleError
<'a
, $tcx
>>
633 where F
: FnOnce(&$V
) -> R
635 debug
!("ty::queries::{}::try_get_with(key={:?}, span={:?})",
641 ProfileQueriesMsg
::QueryBegin(
643 QueryMsg
::$
name(profq_key
!(tcx
, key
))
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())
658 profq_msg
!(tcx
, ProfileQueriesMsg
::CacheHit
);
659 tcx
.dep_graph
.read_index(value
.index
);
660 return Ok(f(&value
.value
));
662 // else, we are going to run the provider:
663 profq_msg
!(tcx
, ProfileQueriesMsg
::ProviderBegin
);
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
)
672 let res
= tcx
.cycle_check(span
, Query
::$
name(key
), || {
673 let dep_node
= Self::to_dep_node(tcx
, &key
);
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
)
682 fn run_provider
<'a
, 'tcx
, 'lcx
>(tcx
: TyCtxt
<'a
, 'tcx
, 'lcx
>,
685 let provider
= tcx
.maps
.providers
[key
.map_crate()].$name
;
686 provider(tcx
.global_tcx(), key
)
689 tcx
.dep_graph
.with_task(dep_node
, tcx
, key
, run_provider
)
693 profq_msg
!(tcx
, ProfileQueriesMsg
::ProviderEnd
);
694 let ((result
, dep_node_index
), diagnostics
) = res
;
696 tcx
.dep_graph
.read_index(dep_node_index
);
698 let value
= QueryValue
{
700 index
: dep_node_index
,
701 diagnostics
: if diagnostics
.len() == 0 {
704 Some(Box
::new(QueryDiagnostics
{
706 emitted_diagnostics
: Cell
::new(true),
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
) {
724 Err(e
) => Err(tcx
.report_cycle(e
)),
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();
732 match Self::try_get_with(tcx
, span
, key
, |_
| ()) {
734 Err(e
) => tcx
.report_cycle(e
).emit(),
739 #[derive(Copy, Clone)]
740 pub struct TyCtxtAt
<'a
, 'gcx
: 'a
+'tcx
, 'tcx
: 'a
> {
741 pub tcx
: TyCtxt
<'a
, 'gcx
, 'tcx
>,
745 impl<'a
, 'gcx
, 'tcx
> Deref
for TyCtxtAt
<'a
, 'gcx
, 'tcx
> {
746 type Target
= TyCtxt
<'a
, 'gcx
, 'tcx
>;
747 fn deref(&self) -> &Self::Target
{
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
> {
763 pub fn $
name(self, key
: $K
) -> $V
{
764 self.at(DUMMY_SP
).$
name(key
)
768 impl<'a
, $tcx
, 'lcx
> TyCtxtAt
<'a
, $tcx
, 'lcx
> {
770 pub fn $
name(self, key
: $K
) -> $V
{
771 queries
::$name
::try_get(self.tcx
, self.span
, key
).unwrap_or_else(|mut e
| {
773 Value
::from_cycle_error(self.global_tcx())
778 define_provider_struct
! {
780 input
: ($
(([$
($modifiers
)*] [$name
] [$K
] [$V
]))*),
784 impl<$tcx
> Copy
for Providers
<$tcx
> {}
785 impl<$tcx
> Clone
for Providers
<$tcx
> {
786 fn clone(&self) -> Self { *self }
791 macro_rules
! define_map_struct
{
794 input
: $input
:tt
) => {
805 output
: ($
($output
:tt
)*)) => {
806 pub struct Maps
<$tcx
> {
807 providers
: IndexVec
<CrateNum
, Providers
<$tcx
>>,
808 query_stack
: RefCell
<Vec
<(Span
, Query
<$tcx
>)>>,
813 // Field recognized and ready to shift into the output
815 ready
: ([$
($
pub:tt
)*] [$
($attr
:tt
)*] [$name
:ident
]),
817 output
: ($
($output
:tt
)*)) => {
822 $
(#[$attr])* $($pub)* $name: RefCell<QueryMap<queries::$name<$tcx>>>,)
826 // No modifiers left? This is a private item.
828 input
: (([] $attrs
:tt $name
:tt
) $
($input
:tt
)*),
829 output
: $output
:tt
) => {
832 ready
: ([] $attrs $name
),
838 // Skip other modifiers
840 input
: (([$other_modifier
:tt $
($modifiers
:tt
)*] $
($fields
:tt
)*) $
($input
:tt
)*),
841 output
: $output
:tt
) => {
844 input
: (([$
($modifiers
)*] $
($fields
)*) $
($input
)*),
850 macro_rules
! define_provider_struct
{
852 (tcx
: $tcx
:tt
, input
: $input
:tt
) => {
853 define_provider_struct
! {
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
,)*
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
);
874 Providers { $($name),* }
879 // Something ready to shift:
881 ready
: ($name
:tt $K
:tt $V
:tt
),
883 output
: ($
($output
:tt
)*)) => {
884 define_provider_struct
! {
887 output
: ($
($output
)* ($name $K $V
))
891 // Regular queries produce a `V` only.
893 input
: (([] $name
:tt $K
:tt $V
:tt
) $
($input
:tt
)*),
894 output
: $output
:tt
) => {
895 define_provider_struct
! {
897 ready
: ($name $K $V
),
905 input
: (([$other_modifier
:tt $
($modifiers
:tt
)*] $
($fields
:tt
)*) $
($input
:tt
)*),
906 output
: $output
:tt
) => {
907 define_provider_struct
! {
909 input
: (([$
($modifiers
)*] $
($fields
)*) $
($input
)*),
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
>,
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
>,
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
>,
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
>,
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
>,
949 /// True if this is a const fn
950 [] is_const_fn
: IsConstFn(DefId
) -> bool
,
952 /// True if this is a foreign item (i.e., linked via `extern { ... }`).
953 [] is_foreign_item
: IsForeignItem(DefId
) -> bool
,
955 /// True if this is a default impl (aka impl Foo for ..)
956 [] is_default_impl
: IsDefaultImpl(DefId
) -> bool
,
958 /// Get a map with the variance of every item; use `item_variance`
960 [] crate_variances
: crate_variances(CrateNum
) -> Rc
<ty
::CrateVariancesMap
>,
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
>>,
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
>>,
969 /// Maps from a trait item to the trait item "descriptor"
970 [] associated_item
: AssociatedItems(DefId
) -> ty
::AssociatedItem
,
972 [] impl_trait_ref
: ImplTraitRef(DefId
) -> Option
<ty
::TraitRef
<'tcx
>>,
973 [] impl_polarity
: ImplPolarity(DefId
) -> hir
::ImplPolarity
,
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
>>,
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
983 [] mir_keys
: mir_keys(CrateNum
) -> Rc
<DefIdSet
>,
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,
990 /// Fetch the MIR for a given def-id up till the point where it is
991 /// ready for const evaluation.
993 /// See the README for the `mir` module for details.
994 [] mir_const
: MirConst(DefId
) -> &'tcx Steal
<mir
::Mir
<'tcx
>>,
996 [] mir_validated
: MirValidated(DefId
) -> &'tcx Steal
<mir
::Mir
<'tcx
>>,
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
>,
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
,
1006 /// The signature of functions and closures.
1007 [] fn_sig
: FnSignature(DefId
) -> ty
::PolyFnSig
<'tcx
>,
1009 /// Caches CoerceUnsized kinds for impls on custom types.
1010 [] coerce_unsized_info
: CoerceUnsizedInfo(DefId
)
1011 -> ty
::adjustment
::CoerceUnsizedInfo
,
1013 [] typeck_item_bodies
: typeck_item_bodies_dep_node(CrateNum
) -> CompileResult
,
1015 [] typeck_tables_of
: TypeckTables(DefId
) -> &'tcx ty
::TypeckTables
<'tcx
>,
1017 [] has_typeck_tables
: HasTypeckTables(DefId
) -> bool
,
1019 [] coherent_trait
: coherent_trait_dep_node((CrateNum
, DefId
)) -> (),
1021 [] borrowck
: BorrowCheck(DefId
) -> (),
1022 // FIXME: shouldn't this return a `Result<(), BorrowckErrors>` instead?
1023 [] mir_borrowck
: MirBorrowCheck(DefId
) -> (),
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
,
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
) -> (),
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
>,
1040 /// Performs the privacy check and computes "access levels".
1041 [] privacy_access_levels
: PrivacyAccessLevels(CrateNum
) -> Rc
<AccessLevels
>,
1043 [] reachable_set
: reachability_dep_node(CrateNum
) -> Rc
<NodeSet
>,
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
1048 [] region_maps
: RegionMaps(DefId
) -> Rc
<RegionMaps
>,
1050 [] mir_shims
: mir_shim_dep_node(ty
::InstanceDef
<'tcx
>) -> &'tcx mir
::Mir
<'tcx
>,
1052 [] def_symbol_name
: SymbolName(DefId
) -> ty
::SymbolName
,
1053 [] symbol_name
: symbol_name_dep_node(ty
::Instance
<'tcx
>) -> ty
::SymbolName
,
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
,
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
,
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
>,
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
>>,
1089 [] dylib_dependency_formats
: DylibDepFormats(DefId
)
1090 -> Rc
<Vec
<(CrateNum
, LinkagePreference
)>>,
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
,
1097 [] extern_crate
: ExternCrate(DefId
) -> Rc
<Option
<ExternCrate
>>,
1099 [] lint_levels
: lint_levels(CrateNum
) -> Rc
<lint
::LintLevelMap
>,
1102 fn type_param_predicates
<'tcx
>((item_id
, param_id
): (DefId
, DefId
)) -> DepConstructor
<'tcx
> {
1103 DepConstructor
::TypeParamPredicates
{
1109 fn coherent_trait_dep_node
<'tcx
>((_
, def_id
): (CrateNum
, DefId
)) -> DepConstructor
<'tcx
> {
1110 DepConstructor
::CoherenceCheckTrait(def_id
)
1113 fn crate_inherent_impls_dep_node
<'tcx
>(_
: CrateNum
) -> DepConstructor
<'tcx
> {
1114 DepConstructor
::Coherence
1117 fn inherent_impls_overlap_check_dep_node
<'tcx
>(_
: CrateNum
) -> DepConstructor
<'tcx
> {
1118 DepConstructor
::CoherenceInherentImplOverlapCheck
1121 fn reachability_dep_node
<'tcx
>(_
: CrateNum
) -> DepConstructor
<'tcx
> {
1122 DepConstructor
::Reachability
1125 fn mir_shim_dep_node
<'tcx
>(instance_def
: ty
::InstanceDef
<'tcx
>) -> DepConstructor
<'tcx
> {
1126 DepConstructor
::MirShim
{
1131 fn symbol_name_dep_node
<'tcx
>(instance
: ty
::Instance
<'tcx
>) -> DepConstructor
<'tcx
> {
1132 DepConstructor
::InstanceSymbolName { instance }
1135 fn typeck_item_bodies_dep_node
<'tcx
>(_
: CrateNum
) -> DepConstructor
<'tcx
> {
1136 DepConstructor
::TypeckBodiesKrate
1139 fn const_eval_dep_node
<'tcx
>(_
: ty
::ParamEnvAnd
<'tcx
, (DefId
, &'tcx Substs
<'tcx
>)>)
1140 -> DepConstructor
<'tcx
> {
1141 DepConstructor
::ConstEval
1144 fn mir_keys
<'tcx
>(_
: CrateNum
) -> DepConstructor
<'tcx
> {
1145 DepConstructor
::MirKeys
1148 fn crate_variances
<'tcx
>(_
: CrateNum
) -> DepConstructor
<'tcx
> {
1149 DepConstructor
::CrateVariances
1152 fn is_copy_dep_node
<'tcx
>(_
: ty
::ParamEnvAnd
<'tcx
, Ty
<'tcx
>>) -> DepConstructor
<'tcx
> {
1153 DepConstructor
::IsCopy
1156 fn is_sized_dep_node
<'tcx
>(_
: ty
::ParamEnvAnd
<'tcx
, Ty
<'tcx
>>) -> DepConstructor
<'tcx
> {
1157 DepConstructor
::IsSized
1160 fn is_freeze_dep_node
<'tcx
>(_
: ty
::ParamEnvAnd
<'tcx
, Ty
<'tcx
>>) -> DepConstructor
<'tcx
> {
1161 DepConstructor
::IsFreeze
1164 fn needs_drop_dep_node
<'tcx
>(_
: ty
::ParamEnvAnd
<'tcx
, Ty
<'tcx
>>) -> DepConstructor
<'tcx
> {
1165 DepConstructor
::NeedsDrop
1168 fn layout_dep_node
<'tcx
>(_
: ty
::ParamEnvAnd
<'tcx
, Ty
<'tcx
>>) -> DepConstructor
<'tcx
> {
1169 DepConstructor
::Layout
1172 fn lint_levels
<'tcx
>(_
: CrateNum
) -> DepConstructor
<'tcx
> {
1173 DepConstructor
::LintLevels