2020-02-07 18:25:36 +01:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
|
2020-01-02 05:18:45 +01:00
|
|
|
|
2020-02-07 11:14:47 +01:00
|
|
|
use crate::hir::{
|
2022-07-31 16:13:25 +02:00
|
|
|
AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
|
2020-02-07 11:14:47 +01:00
|
|
|
};
|
2020-02-07 18:25:36 +01:00
|
|
|
use crate::hir_id::{HirId, ItemLocalId};
|
2021-04-19 22:27:49 +02:00
|
|
|
use rustc_span::def_id::DefPathHash;
|
2020-01-02 05:18:45 +01:00
|
|
|
|
|
|
|
/// Requirements for a `StableHashingContext` to be used in this crate.
|
|
|
|
/// This is a hack to allow using the `HashStable_Generic` derive macro
|
2021-04-07 14:47:01 -05:00
|
|
|
/// instead of implementing everything in `rustc_middle`.
|
2020-02-29 20:37:32 +03:00
|
|
|
pub trait HashStableContext:
|
|
|
|
rustc_ast::HashStableContext + rustc_target::HashStableContext
|
|
|
|
{
|
2020-01-02 05:18:45 +01:00
|
|
|
fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher);
|
2020-02-07 18:25:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
|
|
|
|
type KeyType = (DefPathHash, ItemLocalId);
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
|
2022-09-20 14:11:23 +09:00
|
|
|
let def_path_hash = self.owner.def_id.to_stable_hash_key(hcx);
|
2020-02-07 18:25:36 +01:00
|
|
|
(def_path_hash, self.local_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-19 23:03:21 +02: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 12:06:04 +01:00
|
|
|
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
|
2021-01-30 17:47:51 +01:00
|
|
|
type KeyType = DefPathHash;
|
2021-01-30 12:06:04 +01:00
|
|
|
|
|
|
|
#[inline]
|
2021-01-30 17:47:51 +01:00
|
|
|
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
|
2022-10-27 14:02:18 +11:00
|
|
|
self.owner_id.def_id.to_stable_hash_key(hcx)
|
2021-01-30 12:06:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 18:25:36 +01:00
|
|
|
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
|
2021-01-30 20:46:50 +01:00
|
|
|
type KeyType = DefPathHash;
|
2020-02-07 18:25:36 +01:00
|
|
|
|
|
|
|
#[inline]
|
2021-01-30 20:46:50 +01:00
|
|
|
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
|
2022-10-27 14:02:18 +11:00
|
|
|
self.owner_id.def_id.to_stable_hash_key(hcx)
|
2020-02-07 18:25:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
|
2021-01-30 23:25:03 +01:00
|
|
|
type KeyType = DefPathHash;
|
2020-02-07 18:25:36 +01:00
|
|
|
|
|
|
|
#[inline]
|
2021-01-30 23:25:03 +01:00
|
|
|
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
|
2022-10-27 14:02:18 +11:00
|
|
|
self.owner_id.def_id.to_stable_hash_key(hcx)
|
2020-02-07 18:25:36 +01:00
|
|
|
}
|
2020-01-02 05:18:45 +01:00
|
|
|
}
|
|
|
|
|
2020-11-11 21:57:54 +01:00
|
|
|
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
|
2021-02-01 00:33:38 +01:00
|
|
|
type KeyType = DefPathHash;
|
2020-11-11 21:57:54 +01:00
|
|
|
|
|
|
|
#[inline]
|
2021-02-01 00:33:38 +01:00
|
|
|
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
|
2022-10-27 14:02:18 +11:00
|
|
|
self.owner_id.def_id.to_stable_hash_key(hcx)
|
2020-11-11 21:57:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 05:18:45 +01: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 11:29:07 +01: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".
|
|
|
|
|
2021-12-13 21:33:53 -07:00
|
|
|
impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> {
|
2021-09-12 03:19:18 +02:00
|
|
|
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.
|
2022-01-28 18:06:53 -03:00
|
|
|
// `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.
|
2023-03-03 17:02:11 +11:00
|
|
|
let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _ } = *self;
|
|
|
|
opt_hash_including_bodies.unwrap().hash_stable(hcx, hasher);
|
2021-09-12 03:19:18 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-12 11:41:35 +02:00
|
|
|
|
2021-12-13 21:33:53 -07:00
|
|
|
impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx> {
|
2021-09-12 11:41:35 +02:00
|
|
|
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
|
2023-03-03 17:02:11 +11:00
|
|
|
// We ignore the `map` since it refers to information included in `opt_hash` which is
|
|
|
|
// hashed in the collector and used for the crate hash.
|
|
|
|
let AttributeMap { opt_hash, map: _ } = *self;
|
|
|
|
opt_hash.unwrap().hash_stable(hcx, hasher);
|
2021-09-12 11:41:35 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-19 22:07:12 +02:00
|
|
|
|
|
|
|
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
|
|
|
|
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
|
2023-03-03 17:02:11 +11:00
|
|
|
let Crate { owners: _, opt_hir_hash } = self;
|
|
|
|
opt_hir_hash.unwrap().hash_stable(hcx, hasher)
|
2021-09-19 22:07:12 +02:00
|
|
|
}
|
|
|
|
}
|