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