Skip to content

Commit ef9810a

Browse files
committed
Auto merge of #100237 - cjgillot:no-special-hash-hir, r=nagisa
Remove manual implementations of HashStable for hir::Expr and hir::Ty. We do not need to force hashing HIR bodies inside those nodes. The contents of bodies are not accessible from the `hir_owner` query which used `hash_without_bodies`. When the content of a body is required, the access is still done using `hir_owner_nodes`, which continues hashing HIR bodies.
2 parents 3694b7d + 5d75ca5 commit ef9810a

File tree

5 files changed

+12
-64
lines changed

5 files changed

+12
-64
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -643,14 +643,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
643643
) -> (Fingerprint, Fingerprint) {
644644
self.tcx.with_stable_hashing_context(|mut hcx| {
645645
let mut stable_hasher = StableHasher::new();
646-
hcx.with_hir_bodies(true, node.def_id(), bodies, |hcx| {
646+
hcx.with_hir_bodies(node.def_id(), bodies, |hcx| {
647647
node.hash_stable(hcx, &mut stable_hasher)
648648
});
649649
let hash_including_bodies = stable_hasher.finish();
650650
let mut stable_hasher = StableHasher::new();
651-
hcx.with_hir_bodies(false, node.def_id(), bodies, |hcx| {
652-
node.hash_stable(hcx, &mut stable_hasher)
653-
});
651+
hcx.without_hir_bodies(|hcx| node.hash_stable(hcx, &mut stable_hasher));
654652
let hash_without_bodies = stable_hasher.finish();
655653
(hash_including_bodies, hash_without_bodies)
656654
})

compiler/rustc_hir/src/hir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ pub struct AnonConst {
16261626
}
16271627

16281628
/// An expression.
1629-
#[derive(Debug)]
1629+
#[derive(Debug, HashStable_Generic)]
16301630
pub struct Expr<'hir> {
16311631
pub hir_id: HirId,
16321632
pub kind: ExprKind<'hir>,
@@ -2380,7 +2380,7 @@ impl TypeBinding<'_> {
23802380
}
23812381
}
23822382

2383-
#[derive(Debug)]
2383+
#[derive(Debug, HashStable_Generic)]
23842384
pub struct Ty<'hir> {
23852385
pub hir_id: HirId,
23862386
pub kind: TyKind<'hir>,

compiler/rustc_hir/src/stable_hash_impls.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
22

33
use crate::hir::{
4-
AttributeMap, BodyId, Crate, Expr, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
5-
Ty,
4+
AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
65
};
76
use crate::hir_id::{HirId, ItemLocalId};
87
use rustc_span::def_id::DefPathHash;
@@ -14,8 +13,6 @@ pub trait HashStableContext:
1413
rustc_ast::HashStableContext + rustc_target::HashStableContext
1514
{
1615
fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher);
17-
fn hash_hir_expr(&mut self, _: &Expr<'_>, hasher: &mut StableHasher);
18-
fn hash_hir_ty(&mut self, _: &Ty<'_>, hasher: &mut StableHasher);
1916
}
2017

2118
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
@@ -96,18 +93,6 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
9693
// want to pick up on a reference changing its target, so we hash the NodeIds
9794
// in "DefPath Mode".
9895

99-
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Expr<'_> {
100-
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
101-
hcx.hash_hir_expr(self, hasher)
102-
}
103-
}
104-
105-
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Ty<'_> {
106-
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
107-
hcx.hash_hir_ty(self, hasher)
108-
}
109-
}
110-
11196
impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> {
11297
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
11398
// We ignore the `nodes` and `bodies` fields since these refer to information included in

compiler/rustc_query_system/src/ich/hcx.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,8 @@ pub struct StableHashingContext<'a> {
4040
#[derive(Clone, Copy)]
4141
pub(super) enum BodyResolver<'tcx> {
4242
Forbidden,
43-
Traverse {
44-
hash_bodies: bool,
45-
owner: LocalDefId,
46-
bodies: &'tcx SortedMap<hir::ItemLocalId, &'tcx hir::Body<'tcx>>,
47-
},
43+
Ignore,
44+
Traverse { owner: LocalDefId, bodies: &'tcx SortedMap<hir::ItemLocalId, &'tcx hir::Body<'tcx>> },
4845
}
4946

5047
impl<'a> StableHashingContext<'a> {
@@ -98,32 +95,20 @@ impl<'a> StableHashingContext<'a> {
9895
Self::new_with_or_without_spans(sess, definitions, cstore, source_span, always_ignore_spans)
9996
}
10097

101-
/// Allow hashing
10298
#[inline]
103-
pub fn while_hashing_hir_bodies(&mut self, hb: bool, f: impl FnOnce(&mut Self)) {
104-
let prev = match &mut self.body_resolver {
105-
BodyResolver::Forbidden => panic!("Hashing HIR bodies is forbidden."),
106-
BodyResolver::Traverse { ref mut hash_bodies, .. } => {
107-
std::mem::replace(hash_bodies, hb)
108-
}
109-
};
110-
f(self);
111-
match &mut self.body_resolver {
112-
BodyResolver::Forbidden => unreachable!(),
113-
BodyResolver::Traverse { ref mut hash_bodies, .. } => *hash_bodies = prev,
114-
}
99+
pub fn without_hir_bodies(&mut self, f: impl FnOnce(&mut StableHashingContext<'_>)) {
100+
f(&mut StableHashingContext { body_resolver: BodyResolver::Ignore, ..self.clone() });
115101
}
116102

117103
#[inline]
118104
pub fn with_hir_bodies(
119105
&mut self,
120-
hash_bodies: bool,
121106
owner: LocalDefId,
122107
bodies: &SortedMap<hir::ItemLocalId, &hir::Body<'_>>,
123108
f: impl FnOnce(&mut StableHashingContext<'_>),
124109
) {
125110
f(&mut StableHashingContext {
126-
body_resolver: BodyResolver::Traverse { hash_bodies, owner, bodies },
111+
body_resolver: BodyResolver::Traverse { owner, bodies },
127112
..self.clone()
128113
});
129114
}

compiler/rustc_query_system/src/ich/impls_hir.rs

+2-22
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,11 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
1212
let hcx = self;
1313
match hcx.body_resolver {
1414
BodyResolver::Forbidden => panic!("Hashing HIR bodies is forbidden."),
15-
BodyResolver::Traverse { hash_bodies: false, .. } => {}
16-
BodyResolver::Traverse { hash_bodies: true, owner, bodies } => {
15+
BodyResolver::Ignore => {}
16+
BodyResolver::Traverse { owner, bodies } => {
1717
assert_eq!(id.hir_id.owner, owner);
1818
bodies[&id.hir_id.local_id].hash_stable(hcx, hasher);
1919
}
2020
}
2121
}
22-
23-
fn hash_hir_expr(&mut self, expr: &hir::Expr<'_>, hasher: &mut StableHasher) {
24-
self.while_hashing_hir_bodies(true, |hcx| {
25-
let hir::Expr { hir_id, ref span, ref kind } = *expr;
26-
27-
hir_id.hash_stable(hcx, hasher);
28-
span.hash_stable(hcx, hasher);
29-
kind.hash_stable(hcx, hasher);
30-
})
31-
}
32-
33-
fn hash_hir_ty(&mut self, ty: &hir::Ty<'_>, hasher: &mut StableHasher) {
34-
self.while_hashing_hir_bodies(true, |hcx| {
35-
let hir::Ty { hir_id, ref kind, ref span } = *ty;
36-
37-
hir_id.hash_stable(hcx, hasher);
38-
kind.hash_stable(hcx, hasher);
39-
span.hash_stable(hcx, hasher);
40-
})
41-
}
4222
}

0 commit comments

Comments
 (0)