5555//!
5656//! [dependency graph]: https://rustc-dev-guide.rust-lang.org/query.html
5757
58- use crate :: ty:: query:: QueryCtxt ;
5958use crate :: ty:: TyCtxt ;
6059
6160use rustc_data_structures:: fingerprint:: Fingerprint ;
6261use rustc_hir:: def_id:: { CrateNum , DefId , LocalDefId , CRATE_DEF_INDEX } ;
6362use rustc_hir:: definitions:: DefPathHash ;
6463use rustc_hir:: HirId ;
6564use rustc_span:: symbol:: Symbol ;
66- use rustc_span:: DUMMY_SP ;
6765use std:: hash:: Hash ;
6866
6967pub use rustc_query_system:: dep_graph:: { DepContext , DepNodeParams } ;
@@ -92,53 +90,6 @@ pub struct DepKindStruct {
9290 // FIXME: Make this a simple boolean once DepNodeParams::can_reconstruct_query_key
9391 // can be made a specialized associated const.
9492 can_reconstruct_query_key : fn ( ) -> bool ,
95-
96- /// The red/green evaluation system will try to mark a specific DepNode in the
97- /// dependency graph as green by recursively trying to mark the dependencies of
98- /// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode`
99- /// where we don't know if it is red or green and we therefore actually have
100- /// to recompute its value in order to find out. Since the only piece of
101- /// information that we have at that point is the `DepNode` we are trying to
102- /// re-evaluate, we need some way to re-run a query from just that. This is what
103- /// `force_from_dep_node()` implements.
104- ///
105- /// In the general case, a `DepNode` consists of a `DepKind` and an opaque
106- /// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
107- /// is usually constructed by computing a stable hash of the query-key that the
108- /// `DepNode` corresponds to. Consequently, it is not in general possible to go
109- /// back from hash to query-key (since hash functions are not reversible). For
110- /// this reason `force_from_dep_node()` is expected to fail from time to time
111- /// because we just cannot find out, from the `DepNode` alone, what the
112- /// corresponding query-key is and therefore cannot re-run the query.
113- ///
114- /// The system deals with this case letting `try_mark_green` fail which forces
115- /// the root query to be re-evaluated.
116- ///
117- /// Now, if `force_from_dep_node()` would always fail, it would be pretty useless.
118- /// Fortunately, we can use some contextual information that will allow us to
119- /// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we
120- /// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a
121- /// valid `DefPathHash`. Since we also always build a huge table that maps every
122- /// `DefPathHash` in the current codebase to the corresponding `DefId`, we have
123- /// everything we need to re-run the query.
124- ///
125- /// Take the `mir_promoted` query as an example. Like many other queries, it
126- /// just has a single parameter: the `DefId` of the item it will compute the
127- /// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode`
128- /// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
129- /// is actually a `DefPathHash`, and can therefore just look up the corresponding
130- /// `DefId` in `tcx.def_path_hash_to_def_id`.
131- ///
132- /// When you implement a new query, it will likely have a corresponding new
133- /// `DepKind`, and you'll have to support it here in `force_from_dep_node()`. As
134- /// a rule of thumb, if your query takes a `DefId` or `LocalDefId` as sole parameter,
135- /// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
136- /// add it to the "We don't have enough information to reconstruct..." group in
137- /// the match below.
138- pub ( crate ) force_from_dep_node : fn ( tcx : QueryCtxt < ' _ > , dep_node : & DepNode ) -> bool ,
139-
140- /// Invoke a query to put the on-disk cached value in memory.
141- pub ( crate ) try_load_from_on_disk_cache : fn ( QueryCtxt < ' _ > , & DepNode ) ,
14293}
14394
14495impl std:: ops:: Deref for DepKind {
@@ -197,8 +148,7 @@ macro_rules! contains_eval_always_attr {
197148#[ allow( non_upper_case_globals) ]
198149pub mod dep_kind {
199150 use super :: * ;
200- use crate :: ty:: query:: { queries, query_keys} ;
201- use rustc_query_system:: query:: { force_query, QueryDescription } ;
151+ use crate :: ty:: query:: query_keys;
202152
203153 // We use this for most things when incr. comp. is turned off.
204154 pub const Null : DepKindStruct = DepKindStruct {
@@ -207,8 +157,6 @@ pub mod dep_kind {
207157 is_eval_always : false ,
208158
209159 can_reconstruct_query_key : || true ,
210- force_from_dep_node : |_, dep_node| bug ! ( "force_from_dep_node: encountered {:?}" , dep_node) ,
211- try_load_from_on_disk_cache : |_, _| { } ,
212160 } ;
213161
214162 pub const TraitSelect : DepKindStruct = DepKindStruct {
@@ -217,8 +165,6 @@ pub mod dep_kind {
217165 is_eval_always : false ,
218166
219167 can_reconstruct_query_key : || true ,
220- force_from_dep_node : |_, _| false ,
221- try_load_from_on_disk_cache : |_, _| { } ,
222168 } ;
223169
224170 pub const CompileCodegenUnit : DepKindStruct = DepKindStruct {
@@ -227,8 +173,6 @@ pub mod dep_kind {
227173 is_eval_always : false ,
228174
229175 can_reconstruct_query_key : || false ,
230- force_from_dep_node : |_, _| false ,
231- try_load_from_on_disk_cache : |_, _| { } ,
232176 } ;
233177
234178 macro_rules! define_query_dep_kinds {
@@ -247,54 +191,11 @@ pub mod dep_kind {
247191 :: can_reconstruct_query_key( )
248192 }
249193
250- fn recover<' tcx>( tcx: TyCtxt <' tcx>, dep_node: & DepNode ) -> Option <query_keys:: $variant<' tcx>> {
251- <query_keys:: $variant<' _> as DepNodeParams <TyCtxt <' _>>>:: recover( tcx, dep_node)
252- }
253-
254- fn force_from_dep_node( tcx: QueryCtxt <' _>, dep_node: & DepNode ) -> bool {
255- if is_anon {
256- return false ;
257- }
258-
259- if !can_reconstruct_query_key( ) {
260- return false ;
261- }
262-
263- if let Some ( key) = recover( * tcx, dep_node) {
264- force_query:: <queries:: $variant<' _>, _>( tcx, key, DUMMY_SP , * dep_node) ;
265- return true ;
266- }
267-
268- false
269- }
270-
271- fn try_load_from_on_disk_cache( tcx: QueryCtxt <' _>, dep_node: & DepNode ) {
272- if is_anon {
273- return
274- }
275-
276- if !can_reconstruct_query_key( ) {
277- return
278- }
279-
280- debug_assert!( tcx. dep_graph
281- . node_color( dep_node)
282- . map( |c| c. is_green( ) )
283- . unwrap_or( false ) ) ;
284-
285- let key = recover( * tcx, dep_node) . unwrap_or_else( || panic!( "Failed to recover key for {:?} with hash {}" , dep_node, dep_node. hash) ) ;
286- if queries:: $variant:: cache_on_disk( tcx, & key, None ) {
287- let _ = tcx. $variant( key) ;
288- }
289- }
290-
291194 DepKindStruct {
292195 has_params,
293196 is_anon,
294197 is_eval_always,
295198 can_reconstruct_query_key,
296- force_from_dep_node,
297- try_load_from_on_disk_cache,
298199 }
299200 } ; ) *
300201 ) ;
@@ -310,7 +211,12 @@ macro_rules! define_dep_nodes {
310211 $variant: ident $( ( $tuple_arg_ty: ty $( , ) ? ) ) *
311212 , ) *
312213 ) => (
313- static DEP_KINDS : & [ DepKindStruct ] = & [ $( dep_kind:: $variant) ,* ] ;
214+ #[ macro_export]
215+ macro_rules! make_dep_kind_array {
216+ ( $mod: ident) => { [ $( ( $mod:: $variant) , ) * ] } ;
217+ }
218+
219+ static DEP_KINDS : & [ DepKindStruct ] = & make_dep_kind_array!( dep_kind) ;
314220
315221 /// This enum serves as an index into the `DEP_KINDS` array.
316222 #[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash , Encodable , Decodable ) ]
0 commit comments