rust/compiler/rustc_hir/src/stable_hash_impls.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

144 lines
5.0 KiB
Rust
Raw Normal View History

2020-02-07 17:25:36 +00:00
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
2020-02-07 10:14:47 +00:00
use crate::hir::{
2022-04-04 20:19:25 +00:00
AttributeMap, BodyId, Crate, Expr, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
Ty,
2020-02-07 10:14:47 +00:00
};
2020-02-07 17:25:36 +00:00
use crate::hir_id::{HirId, ItemLocalId};
2021-04-19 20:27:49 +00:00
use rustc_span::def_id::DefPathHash;
/// Requirements for a `StableHashingContext` to be used in this crate.
/// This is a hack to allow using the `HashStable_Generic` derive macro
/// instead of implementing everything in `rustc_middle`.
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);
2020-02-07 17:25:36 +00:00
}
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
type KeyType = (DefPathHash, ItemLocalId);
#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
2021-04-19 20:27:49 +00:00
let def_path_hash = self.owner.to_stable_hash_key(hcx);
2020-02-07 17:25:36 +00:00
(def_path_hash, self.local_id)
}
}
2021-09-19 21:03:21 +00:00
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemLocalId {
type KeyType = ItemLocalId;
#[inline]
fn to_stable_hash_key(&self, _: &HirCtx) -> ItemLocalId {
*self
}
}
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for BodyId {
type KeyType = (DefPathHash, ItemLocalId);
#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
let BodyId { hir_id } = *self;
hir_id.to_stable_hash_key(hcx)
}
}
2021-01-30 11:06:04 +00:00
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
type KeyType = DefPathHash;
2021-01-30 11:06:04 +00:00
#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
2021-04-19 20:27:49 +00:00
self.def_id.to_stable_hash_key(hcx)
2021-01-30 11:06:04 +00:00
}
}
2020-02-07 17:25:36 +00:00
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
type KeyType = DefPathHash;
2020-02-07 17:25:36 +00:00
#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
2021-04-19 20:27:49 +00:00
self.def_id.to_stable_hash_key(hcx)
2020-02-07 17:25:36 +00:00
}
}
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
type KeyType = DefPathHash;
2020-02-07 17:25:36 +00:00
#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
2021-04-19 20:27:49 +00:00
self.def_id.to_stable_hash_key(hcx)
2020-02-07 17:25:36 +00:00
}
}
2020-11-11 20:57:54 +00:00
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
type KeyType = DefPathHash;
2020-11-11 20:57:54 +00:00
#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
2021-04-19 20:27:49 +00:00
self.def_id.to_stable_hash_key(hcx)
2020-11-11 20:57:54 +00:00
}
}
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
hcx.hash_body_id(*self, hasher)
}
}
2020-02-05 10:29:07 +00:00
// The following implementations of HashStable for `ItemId`, `TraitItemId`, and
// `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
// the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
// are used when another item in the HIR is *referenced* and we certainly
// 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
// `hash` which is hashed in the collector and used for the crate hash.
// `local_id_to_def_id` is also ignored because is dependent on the body, then just hashing
// the body satisfies the condition of two nodes being different have different
// `hash_stable` results.
2021-11-21 18:04:47 +00:00
let OwnerNodes {
hash_including_bodies,
hash_without_bodies: _,
nodes: _,
bodies: _,
local_id_to_def_id: _,
} = *self;
2021-10-11 20:36:37 +00:00
hash_including_bodies.hash_stable(hcx, hasher);
}
}
impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx> {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
// We ignore the `map` since it refers to information included in `hash` which is hashed in
// the collector and used for the crate hash.
let AttributeMap { hash, map: _ } = *self;
hash.hash_stable(hcx, hasher);
}
}
2021-09-19 20:07:12 +00:00
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
let Crate { owners: _, hir_hash } = self;
hir_hash.hash_stable(hcx, hasher)
}
}