@@ -5,8 +5,8 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
55use rustc_data_structures:: svh:: Svh ;
66use rustc_data_structures:: sync:: { DynSend , DynSync , par_for_each_in, try_par_for_each_in} ;
77use rustc_hir:: def:: { DefKind , Res } ;
8- use rustc_hir:: def_id:: { DefId , LOCAL_CRATE , LocalDefId , LocalModDefId } ;
9- use rustc_hir:: definitions:: { DefKey , DefPath , DefPathHash } ;
8+ use rustc_hir:: def_id:: { CRATE_DEF_ID , DefId , LOCAL_CRATE , LocalDefId , LocalModDefId } ;
9+ use rustc_hir:: definitions:: { DefKey , DefPath , DefPathHash , Definitions } ;
1010use rustc_hir:: intravisit:: Visitor ;
1111use rustc_hir:: * ;
1212use rustc_hir_pretty as pprust_hir;
@@ -405,9 +405,8 @@ impl<'tcx> TyCtxt<'tcx> {
405405 where
406406 V : Visitor < ' tcx > ,
407407 {
408- let krate = self . hir_crate ( ( ) ) ;
409- for info in krate. owners . iter ( ) {
410- if let MaybeOwner :: Owner ( info) = info {
408+ for def_id in self . hir_crate_items ( ( ) ) . definitions ( ) . chain ( [ CRATE_DEF_ID ] ) {
409+ if let MaybeOwner :: Owner ( info) = self . hir_owner ( def_id) {
411410 for attrs in info. attrs . map . values ( ) {
412411 walk_list ! ( visitor, visit_attribute, * attrs) ;
413412 }
@@ -1130,9 +1129,32 @@ impl<'tcx> pprust_hir::PpAnn for TyCtxt<'tcx> {
11301129 }
11311130}
11321131
1132+ /// Compute the hash for the HIR of the full crate.
1133+ /// This hash will then be part of the crate_hash which is stored in the metadata.
1134+ fn compute_hir_hash ( tcx : TyCtxt < ' _ > , definitions : & Definitions ) -> Fingerprint {
1135+ let mut hir_body_nodes: Vec < _ > = definitions
1136+ . def_path_table ( )
1137+ . def_keys ( )
1138+ . filter_map ( |local_def_index| {
1139+ let def_id = LocalDefId { local_def_index } ;
1140+ let info = tcx. hir_owner ( def_id) ;
1141+ let info = info. as_owner ( ) ?;
1142+ let def_path_hash = tcx. hir_def_path_hash ( def_id) ;
1143+ Some ( ( def_path_hash, info) )
1144+ } )
1145+ . collect ( ) ;
1146+ hir_body_nodes. sort_unstable_by_key ( |bn| bn. 0 ) ;
1147+
1148+ tcx. with_stable_hashing_context ( |mut hcx| {
1149+ let mut stable_hasher = StableHasher :: new ( ) ;
1150+ hir_body_nodes. hash_stable ( & mut hcx, & mut stable_hasher) ;
1151+ stable_hasher. finish ( )
1152+ } )
1153+ }
1154+
11331155pub ( super ) fn crate_hash ( tcx : TyCtxt < ' _ > , _: LocalCrate ) -> Svh {
1134- let krate = tcx. hir_crate ( ( ) ) ;
1135- let hir_body_hash = krate . opt_hir_hash . expect ( "HIR hash missing while computing crate hash" ) ;
1156+ let definitions = tcx. untracked ( ) . definitions . freeze ( ) ;
1157+ let hir_body_hash = compute_hir_hash ( tcx , definitions ) ;
11361158
11371159 let upstream_crates = upstream_crates ( tcx) ;
11381160
@@ -1175,11 +1197,12 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
11751197 source_file_names. hash_stable ( & mut hcx, & mut stable_hasher) ;
11761198 debugger_visualizers. hash_stable ( & mut hcx, & mut stable_hasher) ;
11771199 if tcx. sess . opts . incremental . is_some ( ) {
1178- let definitions = tcx. untracked ( ) . definitions . freeze ( ) ;
1179- let mut owner_spans: Vec < _ > = krate
1180- . owners
1181- . iter_enumerated ( )
1182- . filter_map ( |( def_id, info) | {
1200+ let mut owner_spans: Vec < _ > = definitions
1201+ . def_path_table ( )
1202+ . def_keys ( )
1203+ . filter_map ( |local_def_index| {
1204+ let def_id = LocalDefId { local_def_index } ;
1205+ let info = tcx. hir_owner ( def_id) ;
11831206 let _ = info. as_owner ( ) ?;
11841207 let def_path_hash = definitions. def_path_hash ( def_id) ;
11851208 let span = tcx. source_span ( def_id) ;
0 commit comments