]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/query/mod.rs
c75e0d95e8fdfa01b6b484e5b4efc3ae880376d2
[rust.git] / src / librustc / ty / query / mod.rs
1 use crate::dep_graph::{self, DepConstructor, DepNode, DepNodeParams};
2 use crate::hir::exports::Export;
3 use crate::hir::map;
4 use crate::infer::canonical::{self, Canonical};
5 use crate::lint::LintLevelMap;
6 use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
7 use crate::middle::cstore::{CrateSource, DepKind, NativeLibraryKind};
8 use crate::middle::cstore::{ExternCrate, ForeignModule, LinkagePreference, NativeLibrary};
9 use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
10 use crate::middle::lang_items::{LangItem, LanguageItems};
11 use crate::middle::lib_features::LibFeatures;
12 use crate::middle::privacy::AccessLevels;
13 use crate::middle::region;
14 use crate::middle::resolve_lifetime::{ObjectLifetimeDefault, Region, ResolveLifetimes};
15 use crate::middle::stability::{self, DeprecationEntry};
16 use crate::mir;
17 use crate::mir::interpret::GlobalId;
18 use crate::mir::interpret::{ConstEvalRawResult, ConstEvalResult, ConstValue};
19 use crate::mir::interpret::{LitToConstError, LitToConstInput};
20 use crate::mir::mono::CodegenUnit;
21 use crate::traits::query::{
22     CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal,
23     CanonicalTypeOpAscribeUserTypeGoal, CanonicalTypeOpEqGoal, CanonicalTypeOpNormalizeGoal,
24     CanonicalTypeOpProvePredicateGoal, CanonicalTypeOpSubtypeGoal, NoSolution,
25 };
26 use crate::traits::query::{
27     DropckOutlivesResult, DtorckConstraint, MethodAutoderefStepsResult, NormalizationResult,
28     OutlivesBound,
29 };
30 use crate::traits::specialization_graph;
31 use crate::traits::Clauses;
32 use crate::traits::{self, Vtable};
33 use crate::ty::steal::Steal;
34 use crate::ty::subst::{GenericArg, SubstsRef};
35 use crate::ty::util::AlwaysRequiresDrop;
36 use crate::ty::{self, AdtSizedConstraint, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
37 use crate::util::common::ErrorReported;
38 use rustc_data_structures::fingerprint::Fingerprint;
39 use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
40 use rustc_data_structures::profiling::ProfileCategory::*;
41 use rustc_data_structures::stable_hasher::StableVec;
42 use rustc_data_structures::svh::Svh;
43 use rustc_data_structures::sync::Lrc;
44 use rustc_hir as hir;
45 use rustc_hir::def::DefKind;
46 use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId};
47 use rustc_hir::{Crate, HirIdSet, ItemLocalId, TraitCandidate};
48 use rustc_index::vec::IndexVec;
49 use rustc_session::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion};
50 use rustc_session::CrateDisambiguator;
51 use rustc_target::spec::PanicStrategy;
52
53 use rustc_ast::ast;
54 use rustc_attr as attr;
55 use rustc_span::symbol::Symbol;
56 use rustc_span::{Span, DUMMY_SP};
57 use std::borrow::Cow;
58 use std::collections::BTreeMap;
59 use std::ops::Deref;
60 use std::sync::Arc;
61
62 #[macro_use]
63 mod plumbing;
64 pub(crate) use self::plumbing::CycleError;
65 use self::plumbing::*;
66
67 mod stats;
68 pub use self::stats::print_stats;
69
70 mod job;
71 #[cfg(parallel_compiler)]
72 pub use self::job::handle_deadlock;
73 use self::job::QueryJobInfo;
74 pub use self::job::{QueryInfo, QueryJob, QueryJobId};
75
76 mod keys;
77 use self::keys::Key;
78
79 mod values;
80 use self::values::Value;
81
82 mod caches;
83 use self::caches::CacheSelector;
84
85 mod config;
86 use self::config::QueryAccessors;
87 pub use self::config::QueryConfig;
88 pub(crate) use self::config::QueryDescription;
89
90 mod on_disk_cache;
91 pub use self::on_disk_cache::OnDiskCache;
92
93 mod profiling_support;
94 pub use self::profiling_support::{IntoSelfProfilingString, QueryKeyStringBuilder};
95
96 // Each of these queries corresponds to a function pointer field in the
97 // `Providers` struct for requesting a value of that type, and a method
98 // on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way
99 // which memoizes and does dep-graph tracking, wrapping around the actual
100 // `Providers` that the driver creates (using several `rustc_*` crates).
101 //
102 // The result type of each query must implement `Clone`, and additionally
103 // `ty::query::values::Value`, which produces an appropriate placeholder
104 // (error) value if the query resulted in a query cycle.
105 // Queries marked with `fatal_cycle` do not need the latter implementation,
106 // as they will raise an fatal error on query cycles instead.
107
108 rustc_query_append! { [define_queries!][<'tcx>] }
109
110 /// The red/green evaluation system will try to mark a specific DepNode in the
111 /// dependency graph as green by recursively trying to mark the dependencies of
112 /// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode`
113 /// where we don't know if it is red or green and we therefore actually have
114 /// to recompute its value in order to find out. Since the only piece of
115 /// information that we have at that point is the `DepNode` we are trying to
116 /// re-evaluate, we need some way to re-run a query from just that. This is what
117 /// `force_from_dep_node()` implements.
118 ///
119 /// In the general case, a `DepNode` consists of a `DepKind` and an opaque
120 /// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
121 /// is usually constructed by computing a stable hash of the query-key that the
122 /// `DepNode` corresponds to. Consequently, it is not in general possible to go
123 /// back from hash to query-key (since hash functions are not reversible). For
124 /// this reason `force_from_dep_node()` is expected to fail from time to time
125 /// because we just cannot find out, from the `DepNode` alone, what the
126 /// corresponding query-key is and therefore cannot re-run the query.
127 ///
128 /// The system deals with this case letting `try_mark_green` fail which forces
129 /// the root query to be re-evaluated.
130 ///
131 /// Now, if `force_from_dep_node()` would always fail, it would be pretty useless.
132 /// Fortunately, we can use some contextual information that will allow us to
133 /// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we
134 /// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a
135 /// valid `DefPathHash`. Since we also always build a huge table that maps every
136 /// `DefPathHash` in the current codebase to the corresponding `DefId`, we have
137 /// everything we need to re-run the query.
138 ///
139 /// Take the `mir_validated` query as an example. Like many other queries, it
140 /// just has a single parameter: the `DefId` of the item it will compute the
141 /// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode`
142 /// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
143 /// is actually a `DefPathHash`, and can therefore just look up the corresponding
144 /// `DefId` in `tcx.def_path_hash_to_def_id`.
145 ///
146 /// When you implement a new query, it will likely have a corresponding new
147 /// `DepKind`, and you'll have to support it here in `force_from_dep_node()`. As
148 /// a rule of thumb, if your query takes a `DefId` or `LocalDefId` as sole parameter,
149 /// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
150 /// add it to the "We don't have enough information to reconstruct..." group in
151 /// the match below.
152 pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool {
153     // We must avoid ever having to call `force_from_dep_node()` for a
154     // `DepNode::codegen_unit`:
155     // Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
156     // would always end up having to evaluate the first caller of the
157     // `codegen_unit` query that *is* reconstructible. This might very well be
158     // the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
159     // to re-trigger calling the `codegen_unit` query with the right key. At
160     // that point we would already have re-done all the work we are trying to
161     // avoid doing in the first place.
162     // The solution is simple: Just explicitly call the `codegen_unit` query for
163     // each CGU, right after partitioning. This way `try_mark_green` will always
164     // hit the cache instead of having to go through `force_from_dep_node`.
165     // This assertion makes sure, we actually keep applying the solution above.
166     debug_assert!(
167         dep_node.kind != crate::dep_graph::DepKind::codegen_unit,
168         "calling force_from_dep_node() on DepKind::codegen_unit"
169     );
170
171     if !dep_node.kind.can_reconstruct_query_key() {
172         return false;
173     }
174
175     rustc_dep_node_force!([dep_node, tcx]
176         // These are inputs that are expected to be pre-allocated and that
177         // should therefore always be red or green already.
178         crate::dep_graph::DepKind::CrateMetadata |
179
180         // These are anonymous nodes.
181         crate::dep_graph::DepKind::TraitSelect |
182
183         // We don't have enough information to reconstruct the query key of
184         // these.
185         crate::dep_graph::DepKind::CompileCodegenUnit => {
186             bug!("force_from_dep_node: encountered {:?}", dep_node)
187         }
188     );
189
190     false
191 }
192
193 pub(crate) fn try_load_from_on_disk_cache<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) {
194     rustc_dep_node_try_load_from_on_disk_cache!(dep_node, tcx)
195 }