@@ -5,8 +5,8 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5
5
use rustc_data_structures:: svh:: Svh ;
6
6
use rustc_data_structures:: sync:: { DynSend , DynSync , par_for_each_in, try_par_for_each_in} ;
7
7
use 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 } ;
10
10
use rustc_hir:: intravisit:: Visitor ;
11
11
use rustc_hir:: * ;
12
12
use rustc_hir_pretty as pprust_hir;
@@ -405,9 +405,8 @@ impl<'tcx> TyCtxt<'tcx> {
405
405
where
406
406
V : Visitor < ' tcx > ,
407
407
{
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) {
411
410
for attrs in info. attrs . map . values ( ) {
412
411
walk_list ! ( visitor, visit_attribute, * attrs) ;
413
412
}
@@ -1130,9 +1129,32 @@ impl<'tcx> pprust_hir::PpAnn for TyCtxt<'tcx> {
1130
1129
}
1131
1130
}
1132
1131
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
+
1133
1155
pub ( 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 ) ;
1136
1158
1137
1159
let upstream_crates = upstream_crates ( tcx) ;
1138
1160
@@ -1175,11 +1197,12 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
1175
1197
source_file_names. hash_stable ( & mut hcx, & mut stable_hasher) ;
1176
1198
debugger_visualizers. hash_stable ( & mut hcx, & mut stable_hasher) ;
1177
1199
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) ;
1183
1206
let _ = info. as_owner ( ) ?;
1184
1207
let def_path_hash = definitions. def_path_hash ( def_id) ;
1185
1208
let span = tcx. source_span ( def_id) ;
0 commit comments