diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 056f9ca08f8..8dc82e2deee 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -643,14 +643,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ) -> (Fingerprint, Fingerprint) { self.tcx.with_stable_hashing_context(|mut hcx| { 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) }); let hash_including_bodies = stable_hasher.finish(); let mut stable_hasher = StableHasher::new(); - hcx.with_hir_bodies(false, node.def_id(), bodies, |hcx| { - node.hash_stable(hcx, &mut stable_hasher) - }); + hcx.without_hir_bodies(|hcx| node.hash_stable(hcx, &mut stable_hasher)); let hash_without_bodies = stable_hasher.finish(); (hash_including_bodies, hash_without_bodies) }) diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 2610d0b92d8..584e86d51d9 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1626,7 +1626,7 @@ pub struct AnonConst { } /// An expression. -#[derive(Debug)] +#[derive(Debug, HashStable_Generic)] pub struct Expr<'hir> { pub hir_id: HirId, pub kind: ExprKind<'hir>, @@ -2380,7 +2380,7 @@ impl TypeBinding<'_> { } } -#[derive(Debug)] +#[derive(Debug, HashStable_Generic)] pub struct Ty<'hir> { pub hir_id: HirId, pub kind: TyKind<'hir>, diff --git a/compiler/rustc_hir/src/stable_hash_impls.rs b/compiler/rustc_hir/src/stable_hash_impls.rs index 8ccd59e8e37..5b9c42686c3 100644 --- a/compiler/rustc_hir/src/stable_hash_impls.rs +++ b/compiler/rustc_hir/src/stable_hash_impls.rs @@ -1,8 +1,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; use crate::hir::{ - AttributeMap, BodyId, Crate, Expr, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId, - Ty, + AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId, }; use crate::hir_id::{HirId, ItemLocalId}; use rustc_span::def_id::DefPathHash; @@ -14,8 +13,6 @@ pub trait HashStableContext: rustc_ast::HashStableContext + rustc_target::HashStableContext { 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 ToStableHashKey for HirId { @@ -96,18 +93,6 @@ impl HashStable for BodyId { // want to pick up on a reference changing its target, so we hash the NodeIds // in "DefPath Mode". -impl HashStable for Expr<'_> { - fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { - hcx.hash_hir_expr(self, hasher) - } -} - -impl HashStable for Ty<'_> { - fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { - hcx.hash_hir_ty(self, hasher) - } -} - impl<'tcx, HirCtx: crate::HashStableContext> HashStable for OwnerNodes<'tcx> { fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { // We ignore the `nodes` and `bodies` fields since these refer to information included in diff --git a/compiler/rustc_query_system/src/ich/hcx.rs b/compiler/rustc_query_system/src/ich/hcx.rs index 217fac341ed..a09b8ca30e1 100644 --- a/compiler/rustc_query_system/src/ich/hcx.rs +++ b/compiler/rustc_query_system/src/ich/hcx.rs @@ -40,11 +40,8 @@ pub struct StableHashingContext<'a> { #[derive(Clone, Copy)] pub(super) enum BodyResolver<'tcx> { Forbidden, - Traverse { - hash_bodies: bool, - owner: LocalDefId, - bodies: &'tcx SortedMap>, - }, + Ignore, + Traverse { owner: LocalDefId, bodies: &'tcx SortedMap> }, } 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) } - /// Allow hashing #[inline] - pub fn while_hashing_hir_bodies(&mut self, hb: bool, f: impl FnOnce(&mut Self)) { - let prev = match &mut self.body_resolver { - 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, - } + pub fn without_hir_bodies(&mut self, f: impl FnOnce(&mut StableHashingContext<'_>)) { + f(&mut StableHashingContext { body_resolver: BodyResolver::Ignore, ..self.clone() }); } #[inline] pub fn with_hir_bodies( &mut self, - hash_bodies: bool, owner: LocalDefId, bodies: &SortedMap>, f: impl FnOnce(&mut StableHashingContext<'_>), ) { f(&mut StableHashingContext { - body_resolver: BodyResolver::Traverse { hash_bodies, owner, bodies }, + body_resolver: BodyResolver::Traverse { owner, bodies }, ..self.clone() }); } diff --git a/compiler/rustc_query_system/src/ich/impls_hir.rs b/compiler/rustc_query_system/src/ich/impls_hir.rs index 3390ed9eb42..aa008d404c3 100644 --- a/compiler/rustc_query_system/src/ich/impls_hir.rs +++ b/compiler/rustc_query_system/src/ich/impls_hir.rs @@ -12,31 +12,11 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> { let hcx = self; match hcx.body_resolver { BodyResolver::Forbidden => panic!("Hashing HIR bodies is forbidden."), - BodyResolver::Traverse { hash_bodies: false, .. } => {} - BodyResolver::Traverse { hash_bodies: true, owner, bodies } => { + BodyResolver::Ignore => {} + BodyResolver::Traverse { owner, bodies } => { assert_eq!(id.hir_id.owner, owner); 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); - }) - } }