@@ -21,7 +21,7 @@ use rustc_middle::ty::{self, TyCtxt};
2121use rustc_middle:: util:: Providers ;
2222use rustc_session:: cstore:: { CrateStore , ExternCrate } ;
2323use rustc_session:: { Session , StableCrateId } ;
24- use rustc_span:: hygiene:: { ExpnHash , ExpnId } ;
24+ use rustc_span:: hygiene:: ExpnId ;
2525use rustc_span:: symbol:: { kw, Symbol } ;
2626use rustc_span:: Span ;
2727
@@ -378,6 +378,7 @@ provide! { tcx, def_id, other, cdata,
378378}
379379
380380pub ( in crate :: rmeta) fn provide ( providers : & mut Providers ) {
381+ provide_cstore_hooks ( providers) ;
381382 // FIXME(#44234) - almost all of these queries have no sub-queries and
382383 // therefore no actual inputs, they're just reading tables calculated in
383384 // resolve! Does this work? Unsure! That's what the issue is about
@@ -649,26 +650,41 @@ impl CrateStore for CStore {
649650 fn def_path_hash ( & self , def : DefId ) -> DefPathHash {
650651 self . get_crate_data ( def. krate ) . def_path_hash ( def. index )
651652 }
653+ }
652654
653- fn def_path_hash_to_def_id ( & self , cnum : CrateNum , hash : DefPathHash ) -> DefId {
654- let def_index = self . get_crate_data ( cnum) . def_path_hash_to_def_index ( hash) ;
655- DefId { krate : cnum, index : def_index }
656- }
657-
658- fn expn_hash_to_expn_id (
659- & self ,
660- sess : & Session ,
661- cnum : CrateNum ,
662- index_guess : u32 ,
663- hash : ExpnHash ,
664- ) -> ExpnId {
665- self . get_crate_data ( cnum) . expn_hash_to_expn_id ( sess, index_guess, hash)
666- }
655+ fn provide_cstore_hooks ( providers : & mut Providers ) {
656+ providers. hooks . def_path_hash_to_def_id = |tcx, hash, err_msg| {
657+ debug ! ( "def_path_hash_to_def_id({:?})" , hash) ;
658+
659+ let stable_crate_id = hash. stable_crate_id ( ) ;
660+
661+ // If this is a DefPathHash from the local crate, we can look up the
662+ // DefId in the tcx's `Definitions`.
663+ if stable_crate_id == tcx. stable_crate_id ( LOCAL_CRATE ) {
664+ tcx. untracked ( )
665+ . definitions
666+ . read ( )
667+ . local_def_path_hash_to_def_id ( hash, err_msg)
668+ . to_def_id ( )
669+ } else {
670+ // If this is a DefPathHash from an upstream crate, let the CrateStore map
671+ // it to a DefId.
672+ let cstore = CStore :: from_tcx ( tcx. tcx ) ;
673+ let cnum = cstore. stable_crate_id_to_crate_num ( stable_crate_id) ;
674+ let def_index = cstore. get_crate_data ( cnum) . def_path_hash_to_def_index ( hash) ;
675+ DefId { krate : cnum, index : def_index }
676+ }
677+ } ;
667678
668- fn import_source_files ( & self , sess : & Session , cnum : CrateNum ) {
669- let cdata = self . get_crate_data ( cnum) ;
679+ providers. hooks . expn_hash_to_expn_id = |tcx, cnum, index_guess, hash| {
680+ let cstore = CStore :: from_tcx ( tcx. tcx ) ;
681+ cstore. get_crate_data ( cnum) . expn_hash_to_expn_id ( tcx. sess , index_guess, hash)
682+ } ;
683+ providers. hooks . import_source_files = |tcx, cnum| {
684+ let cstore = CStore :: from_tcx ( tcx. tcx ) ;
685+ let cdata = cstore. get_crate_data ( cnum) ;
670686 for file_index in 0 ..cdata. root . source_map . size ( ) {
671- cdata. imported_source_file ( file_index as u32 , sess) ;
687+ cdata. imported_source_file ( file_index as u32 , tcx . sess ) ;
672688 }
673- }
689+ } ;
674690}
0 commit comments