5050//! fingerprint for a given set of node parameters.
5151
5252use crate :: hir:: map:: DefPathHash ;
53- use crate :: ich:: { Fingerprint , StableHashingContext } ;
53+ use crate :: ich:: Fingerprint ;
5454use crate :: mir;
5555use crate :: mir:: interpret:: { GlobalId , LitToConstInput } ;
5656use crate :: traits;
@@ -62,13 +62,13 @@ use crate::traits::query::{
6262use crate :: ty:: subst:: SubstsRef ;
6363use crate :: ty:: { self , ParamEnvAnd , Ty , TyCtxt } ;
6464
65- use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
6665use rustc_hir:: def_id:: { CrateNum , DefId , LocalDefId , CRATE_DEF_INDEX } ;
6766use rustc_hir:: HirId ;
6867use rustc_span:: symbol:: Symbol ;
69- use std:: fmt;
7068use std:: hash:: Hash ;
7169
70+ pub use rustc_query_system:: dep_graph:: { DepContext , DepNodeParams } ;
71+
7272// erase!() just makes tokens go away. It's used to specify which macro argument
7373// is repeated (i.e., which sub-expression of the macro we are in) but don't need
7474// to actually use any of the arguments.
@@ -128,7 +128,7 @@ macro_rules! define_dep_nodes {
128128
129129 // tuple args
130130 $( {
131- return <$tuple_arg_ty as DepNodeParams >
131+ return <$tuple_arg_ty as DepNodeParams < TyCtxt < ' _>> >
132132 :: CAN_RECONSTRUCT_QUERY_KEY ;
133133 } ) *
134134
@@ -212,38 +212,34 @@ macro_rules! define_dep_nodes {
212212 ) *
213213 }
214214
215- #[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ,
216- RustcEncodable , RustcDecodable ) ]
217- pub struct DepNode {
218- pub kind: DepKind ,
219- pub hash: Fingerprint ,
215+ pub type DepNode = rustc_query_system:: dep_graph:: DepNode <DepKind >;
216+
217+ pub trait DepNodeExt : Sized {
218+ /// Construct a DepNode from the given DepKind and DefPathHash. This
219+ /// method will assert that the given DepKind actually requires a
220+ /// single DefId/DefPathHash parameter.
221+ fn from_def_path_hash( def_path_hash: DefPathHash , kind: DepKind ) -> Self ;
222+
223+ /// Used in testing
224+ fn from_label_string( label: & str , def_path_hash: DefPathHash )
225+ -> Result <Self , ( ) >;
226+
227+ /// Used in testing
228+ fn has_label_string( label: & str ) -> bool ;
220229 }
221230
222- impl DepNode {
231+ impl DepNodeExt for DepNode {
223232 /// Construct a DepNode from the given DepKind and DefPathHash. This
224233 /// method will assert that the given DepKind actually requires a
225234 /// single DefId/DefPathHash parameter.
226- pub fn from_def_path_hash( def_path_hash: DefPathHash ,
227- kind: DepKind )
228- -> DepNode {
235+ fn from_def_path_hash( def_path_hash: DefPathHash , kind: DepKind ) -> DepNode {
229236 debug_assert!( kind. can_reconstruct_query_key( ) && kind. has_params( ) ) ;
230237 DepNode {
231238 kind,
232239 hash: def_path_hash. 0 ,
233240 }
234241 }
235242
236- /// Creates a new, parameterless DepNode. This method will assert
237- /// that the DepNode corresponding to the given DepKind actually
238- /// does not require any parameters.
239- pub fn new_no_params( kind: DepKind ) -> DepNode {
240- debug_assert!( !kind. has_params( ) ) ;
241- DepNode {
242- kind,
243- hash: Fingerprint :: ZERO ,
244- }
245- }
246-
247243 /// Extracts the DefId corresponding to this DepNode. This will work
248244 /// if two conditions are met:
249245 ///
@@ -254,20 +250,8 @@ macro_rules! define_dep_nodes {
254250 /// DepNode. Condition (2) might not be fulfilled if a DepNode
255251 /// refers to something from the previous compilation session that
256252 /// has been removed.
257- pub fn extract_def_id( & self , tcx: TyCtxt <' _>) -> Option <DefId > {
258- if self . kind. can_reconstruct_query_key( ) {
259- let def_path_hash = DefPathHash ( self . hash) ;
260- tcx. def_path_hash_to_def_id. as_ref( ) ?
261- . get( & def_path_hash) . cloned( )
262- } else {
263- None
264- }
265- }
266-
267253 /// Used in testing
268- pub fn from_label_string( label: & str ,
269- def_path_hash: DefPathHash )
270- -> Result <DepNode , ( ) > {
254+ fn from_label_string( label: & str , def_path_hash: DefPathHash ) -> Result <DepNode , ( ) > {
271255 let kind = match label {
272256 $(
273257 stringify!( $variant) => DepKind :: $variant,
@@ -287,7 +271,7 @@ macro_rules! define_dep_nodes {
287271 }
288272
289273 /// Used in testing
290- pub fn has_label_string( label: & str ) -> bool {
274+ fn has_label_string( label: & str ) -> bool {
291275 match label {
292276 $(
293277 stringify!( $variant) => true ,
@@ -308,35 +292,6 @@ macro_rules! define_dep_nodes {
308292 ) ;
309293}
310294
311- impl fmt:: Debug for DepNode {
312- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
313- write ! ( f, "{:?}" , self . kind) ?;
314-
315- if !self . kind . has_params ( ) && !self . kind . is_anon ( ) {
316- return Ok ( ( ) ) ;
317- }
318-
319- write ! ( f, "(" ) ?;
320-
321- crate :: ty:: tls:: with_opt ( |opt_tcx| {
322- if let Some ( tcx) = opt_tcx {
323- if let Some ( def_id) = self . extract_def_id ( tcx) {
324- write ! ( f, "{}" , tcx. def_path_debug_str( def_id) ) ?;
325- } else if let Some ( ref s) = tcx. dep_graph . dep_node_debug_str ( * self ) {
326- write ! ( f, "{}" , s) ?;
327- } else {
328- write ! ( f, "{}" , self . hash) ?;
329- }
330- } else {
331- write ! ( f, "{}" , self . hash) ?;
332- }
333- Ok ( ( ) )
334- } ) ?;
335-
336- write ! ( f, ")" )
337- }
338- }
339-
340295rustc_dep_node_append ! ( [ define_dep_nodes!] [ <' tcx>
341296 // We use this for most things when incr. comp. is turned off.
342297 [ ] Null ,
@@ -349,58 +304,10 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
349304 [ ] CompileCodegenUnit ( Symbol ) ,
350305] ) ;
351306
352- pub ( crate ) trait DepNodeParams < ' tcx > : fmt:: Debug + Sized {
353- const CAN_RECONSTRUCT_QUERY_KEY : bool ;
354-
355- /// This method turns the parameters of a DepNodeConstructor into an opaque
356- /// Fingerprint to be used in DepNode.
357- /// Not all DepNodeParams support being turned into a Fingerprint (they
358- /// don't need to if the corresponding DepNode is anonymous).
359- fn to_fingerprint ( & self , _: TyCtxt < ' tcx > ) -> Fingerprint {
360- panic ! ( "Not implemented. Accidentally called on anonymous node?" )
361- }
362-
363- fn to_debug_str ( & self , _: TyCtxt < ' tcx > ) -> String {
364- format ! ( "{:?}" , self )
365- }
366-
367- /// This method tries to recover the query key from the given `DepNode`,
368- /// something which is needed when forcing `DepNode`s during red-green
369- /// evaluation. The query system will only call this method if
370- /// `CAN_RECONSTRUCT_QUERY_KEY` is `true`.
371- /// It is always valid to return `None` here, in which case incremental
372- /// compilation will treat the query as having changed instead of forcing it.
373- fn recover ( tcx : TyCtxt < ' tcx > , dep_node : & DepNode ) -> Option < Self > ;
374- }
375-
376- impl < ' tcx , T > DepNodeParams < ' tcx > for T
377- where
378- T : HashStable < StableHashingContext < ' tcx > > + fmt:: Debug ,
379- {
380- default const CAN_RECONSTRUCT_QUERY_KEY : bool = false;
381-
382- default fn to_fingerprint ( & self , tcx : TyCtxt < ' tcx > ) -> Fingerprint {
383- let mut hcx = tcx. create_stable_hashing_context ( ) ;
384- let mut hasher = StableHasher :: new ( ) ;
385-
386- self . hash_stable ( & mut hcx, & mut hasher) ;
387-
388- hasher. finish ( )
389- }
390-
391- default fn to_debug_str ( & self , _: TyCtxt < ' tcx > ) -> String {
392- format ! ( "{:?}" , * self )
393- }
394-
395- default fn recover ( _: TyCtxt < ' tcx > , _: & DepNode ) -> Option < Self > {
396- None
397- }
398- }
399-
400- impl < ' tcx > DepNodeParams < ' tcx > for DefId {
307+ impl < ' tcx > DepNodeParams < TyCtxt < ' tcx > > for DefId {
401308 const CAN_RECONSTRUCT_QUERY_KEY : bool = true ;
402309
403- fn to_fingerprint ( & self , tcx : TyCtxt < ' _ > ) -> Fingerprint {
310+ fn to_fingerprint ( & self , tcx : TyCtxt < ' tcx > ) -> Fingerprint {
404311 tcx. def_path_hash ( * self ) . 0
405312 }
406313
@@ -409,14 +316,14 @@ impl<'tcx> DepNodeParams<'tcx> for DefId {
409316 }
410317
411318 fn recover ( tcx : TyCtxt < ' tcx > , dep_node : & DepNode ) -> Option < Self > {
412- dep_node . extract_def_id ( tcx )
319+ tcx . extract_def_id ( dep_node )
413320 }
414321}
415322
416- impl < ' tcx > DepNodeParams < ' tcx > for LocalDefId {
323+ impl < ' tcx > DepNodeParams < TyCtxt < ' tcx > > for LocalDefId {
417324 const CAN_RECONSTRUCT_QUERY_KEY : bool = true ;
418325
419- fn to_fingerprint ( & self , tcx : TyCtxt < ' _ > ) -> Fingerprint {
326+ fn to_fingerprint ( & self , tcx : TyCtxt < ' tcx > ) -> Fingerprint {
420327 self . to_def_id ( ) . to_fingerprint ( tcx)
421328 }
422329
@@ -425,14 +332,14 @@ impl<'tcx> DepNodeParams<'tcx> for LocalDefId {
425332 }
426333
427334 fn recover ( tcx : TyCtxt < ' tcx > , dep_node : & DepNode ) -> Option < Self > {
428- dep_node . extract_def_id ( tcx ) . map ( |id| id. expect_local ( ) )
335+ tcx . extract_def_id ( dep_node ) . map ( |id| id. expect_local ( ) )
429336 }
430337}
431338
432- impl < ' tcx > DepNodeParams < ' tcx > for CrateNum {
339+ impl < ' tcx > DepNodeParams < TyCtxt < ' tcx > > for CrateNum {
433340 const CAN_RECONSTRUCT_QUERY_KEY : bool = true ;
434341
435- fn to_fingerprint ( & self , tcx : TyCtxt < ' _ > ) -> Fingerprint {
342+ fn to_fingerprint ( & self , tcx : TyCtxt < ' tcx > ) -> Fingerprint {
436343 let def_id = DefId { krate : * self , index : CRATE_DEF_INDEX } ;
437344 tcx. def_path_hash ( def_id) . 0
438345 }
@@ -442,17 +349,17 @@ impl<'tcx> DepNodeParams<'tcx> for CrateNum {
442349 }
443350
444351 fn recover ( tcx : TyCtxt < ' tcx > , dep_node : & DepNode ) -> Option < Self > {
445- dep_node . extract_def_id ( tcx ) . map ( |id| id. krate )
352+ tcx . extract_def_id ( dep_node ) . map ( |id| id. krate )
446353 }
447354}
448355
449- impl < ' tcx > DepNodeParams < ' tcx > for ( DefId , DefId ) {
356+ impl < ' tcx > DepNodeParams < TyCtxt < ' tcx > > for ( DefId , DefId ) {
450357 const CAN_RECONSTRUCT_QUERY_KEY : bool = false ;
451358
452359 // We actually would not need to specialize the implementation of this
453360 // method but it's faster to combine the hashes than to instantiate a full
454361 // hashing context and stable-hashing state.
455- fn to_fingerprint ( & self , tcx : TyCtxt < ' _ > ) -> Fingerprint {
362+ fn to_fingerprint ( & self , tcx : TyCtxt < ' tcx > ) -> Fingerprint {
456363 let ( def_id_0, def_id_1) = * self ;
457364
458365 let def_path_hash_0 = tcx. def_path_hash ( def_id_0) ;
@@ -468,13 +375,13 @@ impl<'tcx> DepNodeParams<'tcx> for (DefId, DefId) {
468375 }
469376}
470377
471- impl < ' tcx > DepNodeParams < ' tcx > for HirId {
378+ impl < ' tcx > DepNodeParams < TyCtxt < ' tcx > > for HirId {
472379 const CAN_RECONSTRUCT_QUERY_KEY : bool = false ;
473380
474381 // We actually would not need to specialize the implementation of this
475382 // method but it's faster to combine the hashes than to instantiate a full
476383 // hashing context and stable-hashing state.
477- fn to_fingerprint ( & self , tcx : TyCtxt < ' _ > ) -> Fingerprint {
384+ fn to_fingerprint ( & self , tcx : TyCtxt < ' tcx > ) -> Fingerprint {
478385 let HirId { owner, local_id } = * self ;
479386
480387 let def_path_hash = tcx. def_path_hash ( owner. to_def_id ( ) ) ;
@@ -483,27 +390,3 @@ impl<'tcx> DepNodeParams<'tcx> for HirId {
483390 def_path_hash. 0 . combine ( local_id)
484391 }
485392}
486-
487- /// A "work product" corresponds to a `.o` (or other) file that we
488- /// save in between runs. These IDs do not have a `DefId` but rather
489- /// some independent path or string that persists between runs without
490- /// the need to be mapped or unmapped. (This ensures we can serialize
491- /// them even in the absence of a tcx.)
492- #[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord , Hash , RustcEncodable , RustcDecodable ) ]
493- #[ derive( HashStable ) ]
494- pub struct WorkProductId {
495- hash : Fingerprint ,
496- }
497-
498- impl WorkProductId {
499- pub fn from_cgu_name ( cgu_name : & str ) -> WorkProductId {
500- let mut hasher = StableHasher :: new ( ) ;
501- cgu_name. len ( ) . hash ( & mut hasher) ;
502- cgu_name. hash ( & mut hasher) ;
503- WorkProductId { hash : hasher. finish ( ) }
504- }
505-
506- pub fn from_fingerprint ( fingerprint : Fingerprint ) -> WorkProductId {
507- WorkProductId { hash : fingerprint }
508- }
509- }
0 commit comments