about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-08-16 02:32:47 +0000
committerbors <bors@rust-lang.org>2022-08-16 02:32:47 +0000
commitef9810a3e2a9a16e79176cb0d3466ea82d239942 (patch)
tree9f7e70742142d41b540f1d9f2c3e106046767759
parent3694b7d307b7516757651952b30bb97b6ba5c049 (diff)
parent5d75ca5ef4e947d3316e3aef7b5bd4b58028b943 (diff)
downloadrust-ef9810a3e2a9a16e79176cb0d3466ea82d239942.tar.gz
rust-ef9810a3e2a9a16e79176cb0d3466ea82d239942.zip
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.
-rw-r--r--compiler/rustc_ast_lowering/src/lib.rs6
-rw-r--r--compiler/rustc_hir/src/hir.rs4
-rw-r--r--compiler/rustc_hir/src/stable_hash_impls.rs17
-rw-r--r--compiler/rustc_query_system/src/ich/hcx.rs25
-rw-r--r--compiler/rustc_query_system/src/ich/impls_hir.rs24
5 files changed, 12 insertions, 64 deletions
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<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
 // 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> {
     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<hir::ItemLocalId, &'tcx hir::Body<'tcx>>,
-    },
+    Ignore,
+    Traverse { owner: LocalDefId, bodies: &'tcx SortedMap<hir::ItemLocalId, &'tcx hir::Body<'tcx>> },
 }
 
 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<hir::ItemLocalId, &hir::Body<'_>>,
         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);
-        })
-    }
 }