Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6bff842

Browse files
committedMar 21, 2025
Auto merge of rust-lang#138774 - oli-obk:crate-hash-no-hir-crate, r=<try>
Avoid directly accessing the hir_crate query from crate_hash based on rust-lang#138772 cc rust-lang#95004 Move the crate hashing to the crate_hash query and make it work off hir_crate_items instead of hir_crate. r? `@ghost`
2 parents 5d85a71 + b10648a commit 6bff842

File tree

5 files changed

+35
-56
lines changed

5 files changed

+35
-56
lines changed
 

‎compiler/rustc_ast_lowering/src/lib.rs

+2-30
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,15 @@ use std::sync::Arc;
4747
use rustc_ast::node_id::NodeMap;
4848
use rustc_ast::{self as ast, *};
4949
use rustc_attr_parsing::{AttributeParser, OmitDoc};
50-
use rustc_data_structures::fingerprint::Fingerprint;
5150
use rustc_data_structures::sorted_map::SortedMap;
52-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5351
use rustc_data_structures::tagged_ptr::TaggedRef;
5452
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
5553
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5654
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5755
use rustc_hir::{
5856
self as hir, ConstArg, GenericArg, HirId, ItemLocalMap, LangItem, ParamName, TraitCandidate,
5957
};
60-
use rustc_index::{Idx, IndexSlice, IndexVec};
58+
use rustc_index::{Idx, IndexVec};
6159
use rustc_macros::extension;
6260
use rustc_middle::span_bug;
6361
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
@@ -408,29 +406,6 @@ fn index_crate<'a>(
408406
}
409407
}
410408

411-
/// Compute the hash for the HIR of the full crate.
412-
/// This hash will then be part of the crate_hash which is stored in the metadata.
413-
fn compute_hir_hash(
414-
tcx: TyCtxt<'_>,
415-
owners: &IndexSlice<LocalDefId, hir::MaybeOwner<'_>>,
416-
) -> Fingerprint {
417-
let mut hir_body_nodes: Vec<_> = owners
418-
.iter_enumerated()
419-
.filter_map(|(def_id, info)| {
420-
let info = info.as_owner()?;
421-
let def_path_hash = tcx.hir_def_path_hash(def_id);
422-
Some((def_path_hash, info))
423-
})
424-
.collect();
425-
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
426-
427-
tcx.with_stable_hashing_context(|mut hcx| {
428-
let mut stable_hasher = StableHasher::new();
429-
hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
430-
stable_hasher.finish()
431-
})
432-
}
433-
434409
pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
435410
let sess = tcx.sess;
436411
// Queries that borrow `resolver_for_lowering`.
@@ -460,10 +435,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
460435
drop(ast_index);
461436
sess.time("drop_ast", || drop(krate));
462437

463-
// Don't hash unless necessary, because it's expensive.
464-
let opt_hir_hash =
465-
if tcx.needs_crate_hash() { Some(compute_hir_hash(tcx, &owners)) } else { None };
466-
hir::Crate { owners, opt_hir_hash }
438+
hir::Crate { owners }
467439
}
468440

469441
#[derive(Copy, Clone, PartialEq, Debug)]

‎compiler/rustc_hir/src/hir.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1421,8 +1421,6 @@ impl<'tcx> MaybeOwner<'tcx> {
14211421
#[derive(Debug)]
14221422
pub struct Crate<'hir> {
14231423
pub owners: IndexVec<LocalDefId, MaybeOwner<'hir>>,
1424-
// Only present when incr. comp. is enabled.
1425-
pub opt_hir_hash: Option<Fingerprint>,
14261424
}
14271425

14281426
#[derive(Debug, Clone, Copy, HashStable_Generic)]

‎compiler/rustc_hir/src/stable_hash_impls.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_span::def_id::DefPathHash;
33

44
use crate::HashIgnoredAttrId;
55
use crate::hir::{
6-
AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
6+
AttributeMap, BodyId, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
77
};
88
use crate::hir_id::{HirId, ItemLocalId};
99

@@ -111,13 +111,6 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap
111111
}
112112
}
113113

114-
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
115-
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
116-
let Crate { owners: _, opt_hir_hash } = self;
117-
opt_hir_hash.unwrap().hash_stable(hcx, hasher)
118-
}
119-
}
120-
121114
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HashIgnoredAttrId {
122115
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
123116
hcx.hash_attr_id(self, hasher)

‎compiler/rustc_middle/src/hir/map.rs

+31-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_data_structures::svh::Svh;
66
use rustc_data_structures::sync::{DynSend, DynSync, par_for_each_in, try_par_for_each_in};
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId, LocalModDefId};
9-
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
9+
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash, Definitions};
1010
use rustc_hir::intravisit::Visitor;
1111
use rustc_hir::*;
1212
use rustc_hir_pretty as pprust_hir;
@@ -193,12 +193,6 @@ impl<'tcx> TyCtxt<'tcx> {
193193
self.definitions_untracked().def_path(def_id)
194194
}
195195

196-
#[inline]
197-
pub fn hir_def_path_hash(self, def_id: LocalDefId) -> DefPathHash {
198-
// Accessing the DefPathHash is ok, it is incr. comp. stable.
199-
self.definitions_untracked().def_path_hash(def_id)
200-
}
201-
202196
pub fn hir_get_if_local(self, id: DefId) -> Option<Node<'tcx>> {
203197
id.as_local().map(|id| self.hir_node_by_def_id(id))
204198
}
@@ -1130,9 +1124,32 @@ impl<'tcx> pprust_hir::PpAnn for TyCtxt<'tcx> {
11301124
}
11311125
}
11321126

1127+
/// Compute the hash for the HIR of the full crate.
1128+
/// This hash will then be part of the crate_hash which is stored in the metadata.
1129+
fn compute_hir_hash(tcx: TyCtxt<'_>, definitions: &Definitions) -> Fingerprint {
1130+
let mut hir_body_nodes: Vec<_> = tcx
1131+
.hir_crate_items(())
1132+
.owners()
1133+
.map(|owner_id| {
1134+
let def_path_hash = definitions.def_path_hash(owner_id.def_id);
1135+
let nodes = tcx.opt_hir_owner_nodes(owner_id).unwrap();
1136+
let attrs = tcx.hir_attr_map(owner_id);
1137+
let in_scope_traits_map = tcx.in_scope_traits_map(owner_id).unwrap();
1138+
(def_path_hash, nodes, attrs, in_scope_traits_map)
1139+
})
1140+
.collect();
1141+
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
1142+
1143+
tcx.with_stable_hashing_context(|mut hcx| {
1144+
let mut stable_hasher = StableHasher::new();
1145+
hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
1146+
stable_hasher.finish()
1147+
})
1148+
}
1149+
11331150
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");
1151+
let definitions = tcx.untracked().definitions.freeze();
1152+
let hir_body_hash = compute_hir_hash(tcx, definitions);
11361153

11371154
let upstream_crates = upstream_crates(tcx);
11381155

@@ -1175,16 +1192,14 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
11751192
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
11761193
debugger_visualizers.hash_stable(&mut hcx, &mut stable_hasher);
11771194
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)| {
1183-
let _ = info.as_owner()?;
1195+
let mut owner_spans: Vec<_> = tcx
1196+
.hir_crate_items(())
1197+
.definitions()
1198+
.map(|def_id| {
11841199
let def_path_hash = definitions.def_path_hash(def_id);
11851200
let span = tcx.source_span(def_id);
11861201
debug_assert_eq!(span.parent(), None);
1187-
Some((def_path_hash, span))
1202+
(def_path_hash, span)
11881203
})
11891204
.collect();
11901205
owner_spans.sort_unstable_by_key(|bn| bn.0);

‎compiler/rustc_middle/src/query/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ rustc_queries! {
151151
query hir_crate(key: ()) -> &'tcx Crate<'tcx> {
152152
arena_cache
153153
eval_always
154+
no_hash
154155
desc { "getting the crate HIR" }
155156
}
156157

0 commit comments

Comments
 (0)
Failed to load comments.