use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use crate::hir::{
AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
};
use crate::hir_id::{HirId, ItemLocalId};
use rustc_span::def_id::DefPathHash;
pub trait HashStableContext:
rustc_ast::HashStableContext + rustc_target::HashStableContext
{
fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher);
}
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
type KeyType = (DefPathHash, ItemLocalId);
#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
let def_path_hash = self.owner.def_id.to_stable_hash_key(hcx);
(def_path_hash, self.local_id)
}
}
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)
}
}
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
self.owner_id.def_id.to_stable_hash_key(hcx)
}
}
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
self.owner_id.def_id.to_stable_hash_key(hcx)
}
}
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
self.owner_id.def_id.to_stable_hash_key(hcx)
}
}
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
self.owner_id.def_id.to_stable_hash_key(hcx)
}
}
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
hcx.hash_body_id(*self, hasher)
}
}
impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _ } = *self;
opt_hash_including_bodies.unwrap().hash_stable(hcx, hasher);
}
}
impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx> {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
let AttributeMap { opt_hash, map: _ } = *self;
opt_hash.unwrap().hash_stable(hcx, hasher);
}
}
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
let Crate { owners: _, opt_hir_hash } = self;
opt_hir_hash.unwrap().hash_stable(hcx, hasher)
}
}