@@ -13,6 +13,7 @@ use rustc::hir::def::{self, Res, DefKind, CtorOf, CtorKind};
13
13
use rustc:: hir:: def_id:: { CrateNum , DefId , DefIndex , LocalDefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
14
14
use rustc_data_structures:: fingerprint:: Fingerprint ;
15
15
use rustc_data_structures:: fx:: FxHashMap ;
16
+ use rustc:: dep_graph:: { DepNodeIndex , DepKind } ;
16
17
use rustc:: middle:: lang_items;
17
18
use rustc:: mir:: { self , interpret} ;
18
19
use rustc:: mir:: interpret:: AllocDecodingSession ;
@@ -1365,6 +1366,30 @@ impl<'a, 'tcx> CrateMetadata {
1365
1366
// This shouldn't borrow twice, but there is no way to downgrade RefMut to Ref.
1366
1367
self . source_map_import_info . borrow ( )
1367
1368
}
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
+ }
1368
1393
}
1369
1394
1370
1395
// Cannot be implemented on 'ProcMacro', as libproc_macro
0 commit comments