1
Fork 0

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.
This commit is contained in:
bors 2022-08-16 02:32:47 +00:00
commit ef9810a3e2
5 changed files with 12 additions and 64 deletions

View file

@ -643,14 +643,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
) -> (Fingerprint, Fingerprint) { ) -> (Fingerprint, Fingerprint) {
self.tcx.with_stable_hashing_context(|mut hcx| { self.tcx.with_stable_hashing_context(|mut hcx| {
let mut stable_hasher = StableHasher::new(); let mut stable_hasher = StableHasher::new();
hcx.with_hir_bodies(true, node.def_id(), bodies, |hcx| { hcx.with_hir_bodies(node.def_id(), bodies, |hcx| {
node.hash_stable(hcx, &mut stable_hasher) node.hash_stable(hcx, &mut stable_hasher)
}); });
let hash_including_bodies = stable_hasher.finish(); let hash_including_bodies = stable_hasher.finish();
let mut stable_hasher = StableHasher::new(); let mut stable_hasher = StableHasher::new();
hcx.with_hir_bodies(false, node.def_id(), bodies, |hcx| { hcx.without_hir_bodies(|hcx| node.hash_stable(hcx, &mut stable_hasher));
node.hash_stable(hcx, &mut stable_hasher)
});
let hash_without_bodies = stable_hasher.finish(); let hash_without_bodies = stable_hasher.finish();
(hash_including_bodies, hash_without_bodies) (hash_including_bodies, hash_without_bodies)
}) })

View file

@ -1626,7 +1626,7 @@ pub struct AnonConst {
} }
/// An expression. /// An expression.
#[derive(Debug)] #[derive(Debug, HashStable_Generic)]
pub struct Expr<'hir> { pub struct Expr<'hir> {
pub hir_id: HirId, pub hir_id: HirId,
pub kind: ExprKind<'hir>, pub kind: ExprKind<'hir>,
@ -2380,7 +2380,7 @@ impl TypeBinding<'_> {
} }
} }
#[derive(Debug)] #[derive(Debug, HashStable_Generic)]
pub struct Ty<'hir> { pub struct Ty<'hir> {
pub hir_id: HirId, pub hir_id: HirId,
pub kind: TyKind<'hir>, pub kind: TyKind<'hir>,

View file

@ -1,8 +1,7 @@
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use crate::hir::{ use crate::hir::{
AttributeMap, BodyId, Crate, Expr, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId, AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
Ty,
}; };
use crate::hir_id::{HirId, ItemLocalId}; use crate::hir_id::{HirId, ItemLocalId};
use rustc_span::def_id::DefPathHash; use rustc_span::def_id::DefPathHash;
@ -14,8 +13,6 @@ pub trait HashStableContext:
rustc_ast::HashStableContext + rustc_target::HashStableContext rustc_ast::HashStableContext + rustc_target::HashStableContext
{ {
fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher); fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher);
fn hash_hir_expr(&mut self, _: &Expr<'_>, hasher: &mut StableHasher);
fn hash_hir_ty(&mut self, _: &Ty<'_>, hasher: &mut StableHasher);
} }
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId { impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
@ -96,18 +93,6 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
// want to pick up on a reference changing its target, so we hash the NodeIds // want to pick up on a reference changing its target, so we hash the NodeIds
// in "DefPath Mode". // in "DefPath Mode".
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Expr<'_> {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
hcx.hash_hir_expr(self, hasher)
}
}
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Ty<'_> {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
hcx.hash_hir_ty(self, hasher)
}
}
impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> { impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
// We ignore the `nodes` and `bodies` fields since these refer to information included in // We ignore the `nodes` and `bodies` fields since these refer to information included in

View file

@ -40,11 +40,8 @@ pub struct StableHashingContext<'a> {
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub(super) enum BodyResolver<'tcx> { pub(super) enum BodyResolver<'tcx> {
Forbidden, Forbidden,
Traverse { Ignore,
hash_bodies: bool, Traverse { owner: LocalDefId, bodies: &'tcx SortedMap<hir::ItemLocalId, &'tcx hir::Body<'tcx>> },
owner: LocalDefId,
bodies: &'tcx SortedMap<hir::ItemLocalId, &'tcx hir::Body<'tcx>>,
},
} }
impl<'a> StableHashingContext<'a> { impl<'a> StableHashingContext<'a> {
@ -98,32 +95,20 @@ impl<'a> StableHashingContext<'a> {
Self::new_with_or_without_spans(sess, definitions, cstore, source_span, always_ignore_spans) Self::new_with_or_without_spans(sess, definitions, cstore, source_span, always_ignore_spans)
} }
/// Allow hashing
#[inline] #[inline]
pub fn while_hashing_hir_bodies(&mut self, hb: bool, f: impl FnOnce(&mut Self)) { pub fn without_hir_bodies(&mut self, f: impl FnOnce(&mut StableHashingContext<'_>)) {
let prev = match &mut self.body_resolver { f(&mut StableHashingContext { body_resolver: BodyResolver::Ignore, ..self.clone() });
BodyResolver::Forbidden => panic!("Hashing HIR bodies is forbidden."),
BodyResolver::Traverse { ref mut hash_bodies, .. } => {
std::mem::replace(hash_bodies, hb)
}
};
f(self);
match &mut self.body_resolver {
BodyResolver::Forbidden => unreachable!(),
BodyResolver::Traverse { ref mut hash_bodies, .. } => *hash_bodies = prev,
}
} }
#[inline] #[inline]
pub fn with_hir_bodies( pub fn with_hir_bodies(
&mut self, &mut self,
hash_bodies: bool,
owner: LocalDefId, owner: LocalDefId,
bodies: &SortedMap<hir::ItemLocalId, &hir::Body<'_>>, bodies: &SortedMap<hir::ItemLocalId, &hir::Body<'_>>,
f: impl FnOnce(&mut StableHashingContext<'_>), f: impl FnOnce(&mut StableHashingContext<'_>),
) { ) {
f(&mut StableHashingContext { f(&mut StableHashingContext {
body_resolver: BodyResolver::Traverse { hash_bodies, owner, bodies }, body_resolver: BodyResolver::Traverse { owner, bodies },
..self.clone() ..self.clone()
}); });
} }

View file

@ -12,31 +12,11 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
let hcx = self; let hcx = self;
match hcx.body_resolver { match hcx.body_resolver {
BodyResolver::Forbidden => panic!("Hashing HIR bodies is forbidden."), BodyResolver::Forbidden => panic!("Hashing HIR bodies is forbidden."),
BodyResolver::Traverse { hash_bodies: false, .. } => {} BodyResolver::Ignore => {}
BodyResolver::Traverse { hash_bodies: true, owner, bodies } => { BodyResolver::Traverse { owner, bodies } => {
assert_eq!(id.hir_id.owner, owner); assert_eq!(id.hir_id.owner, owner);
bodies[&id.hir_id.local_id].hash_stable(hcx, hasher); bodies[&id.hir_id.local_id].hash_stable(hcx, hasher);
} }
} }
} }
fn hash_hir_expr(&mut self, expr: &hir::Expr<'_>, hasher: &mut StableHasher) {
self.while_hashing_hir_bodies(true, |hcx| {
let hir::Expr { hir_id, ref span, ref kind } = *expr;
hir_id.hash_stable(hcx, hasher);
span.hash_stable(hcx, hasher);
kind.hash_stable(hcx, hasher);
})
}
fn hash_hir_ty(&mut self, ty: &hir::Ty<'_>, hasher: &mut StableHasher) {
self.while_hashing_hir_bodies(true, |hcx| {
let hir::Ty { hir_id, ref kind, ref span } = *ty;
hir_id.hash_stable(hcx, hasher);
kind.hash_stable(hcx, hasher);
span.hash_stable(hcx, hasher);
})
}
} }