@@ -13,6 +13,7 @@ use rustc::hir::def::{self, Res, DefKind, CtorOf, CtorKind};
1313use rustc:: hir:: def_id:: { CrateNum , DefId , DefIndex , LocalDefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
1414use rustc_data_structures:: fingerprint:: Fingerprint ;
1515use rustc_data_structures:: fx:: FxHashMap ;
16+ use rustc:: dep_graph:: { DepNodeIndex , DepKind } ;
1617use rustc:: middle:: lang_items;
1718use rustc:: mir:: { self , interpret} ;
1819use rustc:: mir:: interpret:: AllocDecodingSession ;
@@ -1365,6 +1366,30 @@ impl<'a, 'tcx> CrateMetadata {
13651366 // This shouldn't borrow twice, but there is no way to downgrade RefMut to Ref.
13661367 self . source_map_import_info . borrow ( )
13671368 }
1369+
1370+ /// Get the `DepNodeIndex` corresponding this crate. The result of this
1371+ /// method is cached in the `dep_node_index` field.
1372+ pub ( super ) fn get_crate_dep_node_index ( & self , tcx : TyCtxt < ' tcx > ) -> DepNodeIndex {
1373+ let mut dep_node_index = self . dep_node_index . load ( ) ;
1374+
1375+ if dep_node_index == DepNodeIndex :: INVALID {
1376+ // We have not cached the DepNodeIndex for this upstream crate yet,
1377+ // so use the dep-graph to find it out and cache it.
1378+ // Note that multiple threads can enter this block concurrently.
1379+ // That is fine because the DepNodeIndex remains constant
1380+ // throughout the whole compilation session, and multiple stores
1381+ // would always write the same value.
1382+
1383+ let def_path_hash = self . def_path_hash ( CRATE_DEF_INDEX ) ;
1384+ let dep_node = def_path_hash. to_dep_node ( DepKind :: CrateMetadata ) ;
1385+
1386+ dep_node_index = tcx. dep_graph . dep_node_index_of ( & dep_node) ;
1387+ assert ! ( dep_node_index != DepNodeIndex :: INVALID ) ;
1388+ self . dep_node_index . store ( dep_node_index) ;
1389+ }
1390+
1391+ dep_node_index
1392+ }
13681393}
13691394
13701395// Cannot be implemented on 'ProcMacro', as libproc_macro
0 commit comments