@@ -18,7 +18,7 @@ use hir::TraitMap;
1818use hir:: def:: { Def , ExportMap } ;
1919use hir:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
2020use hir:: map as hir_map;
21- use hir:: map:: DisambiguatedDefPathData ;
21+ use hir:: map:: { DisambiguatedDefPathData , DefPathHash } ;
2222use middle:: free_region:: FreeRegionMap ;
2323use middle:: lang_items;
2424use middle:: resolve_lifetime;
@@ -448,6 +448,10 @@ pub struct GlobalCtxt<'tcx> {
448448
449449 pub hir : hir_map:: Map < ' tcx > ,
450450
451+ /// A map from DefPathHash -> DefId. Includes DefIds from the local crate
452+ /// as well as all upstream crates. Only populated in incremental mode.
453+ pub def_path_hash_to_def_id : Option < FxHashMap < DefPathHash , DefId > > ,
454+
451455 pub maps : maps:: Maps < ' tcx > ,
452456
453457 pub mir_passes : Rc < Passes > ,
@@ -676,6 +680,40 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
676680 let max_cnum = s. cstore . crates ( ) . iter ( ) . map ( |c| c. as_usize ( ) ) . max ( ) . unwrap_or ( 0 ) ;
677681 let mut providers = IndexVec :: from_elem_n ( extern_providers, max_cnum + 1 ) ;
678682 providers[ LOCAL_CRATE ] = local_providers;
683+
684+ let def_path_hash_to_def_id = if s. opts . build_dep_graph ( ) {
685+ let upstream_def_path_tables: Vec < ( CrateNum , Rc < _ > ) > = s
686+ . cstore
687+ . crates ( )
688+ . iter ( )
689+ . map ( |& cnum| ( cnum, s. cstore . def_path_table ( cnum) ) )
690+ . collect ( ) ;
691+
692+ let def_path_tables = || {
693+ upstream_def_path_tables
694+ . iter ( )
695+ . map ( |& ( cnum, ref rc) | ( cnum, & * * rc) )
696+ . chain ( iter:: once ( ( LOCAL_CRATE , hir. definitions ( ) . def_path_table ( ) ) ) )
697+ } ;
698+
699+ // Precompute the capacity of the hashmap so we don't have to
700+ // re-allocate when populating it.
701+ let capacity = def_path_tables ( ) . map ( |( _, t) | t. size ( ) ) . sum :: < usize > ( ) ;
702+
703+ let mut map: FxHashMap < _ , _ > = FxHashMap :: with_capacity_and_hasher (
704+ capacity,
705+ :: std:: default:: Default :: default ( )
706+ ) ;
707+
708+ for ( cnum, def_path_table) in def_path_tables ( ) {
709+ def_path_table. add_def_path_hashes_to ( cnum, & mut map) ;
710+ }
711+
712+ Some ( map)
713+ } else {
714+ None
715+ } ;
716+
679717 tls:: enter_global ( GlobalCtxt {
680718 sess : s,
681719 trans_trait_caches : traits:: trans:: TransTraitCaches :: new ( dep_graph. clone ( ) ) ,
@@ -689,6 +727,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
689727 export_map : resolutions. export_map ,
690728 fulfilled_predicates : RefCell :: new ( fulfilled_predicates) ,
691729 hir : hir,
730+ def_path_hash_to_def_id : def_path_hash_to_def_id,
692731 maps : maps:: Maps :: new ( providers) ,
693732 mir_passes,
694733 freevars : RefCell :: new ( resolutions. freevars ) ,
0 commit comments