2017-03-30 15:27:27 +02:00
|
|
|
//! This module contains `HashStable` implementations for various data types
|
2020-03-29 16:41:09 +02:00
|
|
|
//! from `rustc_middle::ty` in no particular order.
|
2017-03-30 15:27:27 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::middle::region;
|
|
|
|
use crate::mir;
|
|
|
|
use crate::ty;
|
2020-03-24 09:09:42 +01:00
|
|
|
use rustc_data_structures::fingerprint::Fingerprint;
|
2018-01-11 23:01:27 -05:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2021-12-24 16:36:33 -05:00
|
|
|
use rustc_data_structures::stable_hasher::HashingControls;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
|
2020-11-14 16:48:54 +01:00
|
|
|
use rustc_query_system::ich::StableHashingContext;
|
2018-01-11 23:01:27 -05:00
|
|
|
use std::cell::RefCell;
|
2017-03-30 15:27:27 +02:00
|
|
|
use std::mem;
|
|
|
|
|
2019-06-14 01:32:15 +03:00
|
|
|
impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for &'tcx ty::List<T>
|
|
|
|
where
|
|
|
|
T: HashStable<StableHashingContext<'a>>,
|
|
|
|
{
|
2019-09-26 18:54:39 -04:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2018-01-11 23:01:27 -05:00
|
|
|
thread_local! {
|
2021-12-24 16:36:33 -05:00
|
|
|
static CACHE: RefCell<FxHashMap<(usize, usize, HashingControls), Fingerprint>> =
|
2018-10-16 16:57:53 +02:00
|
|
|
RefCell::new(Default::default());
|
2018-01-11 23:01:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let hash = CACHE.with(|cache| {
|
2021-12-24 16:36:33 -05:00
|
|
|
let key = (self.as_ptr() as usize, self.len(), hcx.hashing_controls());
|
2018-01-11 23:01:27 -05:00
|
|
|
if let Some(&hash) = cache.borrow().get(&key) {
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut hasher = StableHasher::new();
|
|
|
|
(&self[..]).hash_stable(hcx, &mut hasher);
|
|
|
|
|
|
|
|
let hash: Fingerprint = hasher.finish();
|
|
|
|
cache.borrow_mut().insert(key, hash);
|
|
|
|
hash
|
|
|
|
});
|
|
|
|
|
|
|
|
hash.hash_stable(hcx, hasher);
|
2017-03-30 15:27:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx, T> ToStableHashKey<StableHashingContext<'a>> for &'tcx ty::List<T>
|
2019-06-14 01:32:15 +03:00
|
|
|
where
|
|
|
|
T: HashStable<StableHashingContext<'a>>,
|
2018-03-06 10:33:42 +01:00
|
|
|
{
|
|
|
|
type KeyType = Fingerprint;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> Fingerprint {
|
|
|
|
let mut hasher = StableHasher::new();
|
|
|
|
let mut hcx: StableHashingContext<'a> = hcx.clone();
|
|
|
|
self.hash_stable(&mut hcx, &mut hasher);
|
|
|
|
hasher.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 16:39:44 +01:00
|
|
|
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::subst::GenericArg<'tcx> {
|
2019-09-26 18:54:39 -04:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2018-02-23 01:13:54 +00:00
|
|
|
self.unpack().hash_stable(hcx, hasher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionKind {
|
2019-09-26 18:54:39 -04:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2017-03-30 15:27:27 +02:00
|
|
|
mem::discriminant(self).hash_stable(hcx, hasher);
|
|
|
|
match *self {
|
2019-10-08 19:26:57 -04:00
|
|
|
ty::ReErased | ty::ReStatic => {
|
2017-03-30 15:27:27 +02:00
|
|
|
// No variant fields to hash for these ...
|
|
|
|
}
|
2019-10-08 19:26:57 -04:00
|
|
|
ty::ReEmpty(universe) => {
|
|
|
|
universe.hash_stable(hcx, hasher);
|
|
|
|
}
|
2020-10-26 14:18:31 -04:00
|
|
|
ty::ReLateBound(db, ty::BoundRegion { kind: ty::BrAnon(i), .. }) => {
|
2018-05-28 09:52:53 -04:00
|
|
|
db.hash_stable(hcx, hasher);
|
2017-03-30 15:27:27 +02:00
|
|
|
i.hash_stable(hcx, hasher);
|
|
|
|
}
|
2020-10-26 14:18:31 -04:00
|
|
|
ty::ReLateBound(db, ty::BoundRegion { kind: ty::BrNamed(def_id, name), .. }) => {
|
2018-05-28 09:52:53 -04:00
|
|
|
db.hash_stable(hcx, hasher);
|
2017-08-08 17:56:28 +02:00
|
|
|
def_id.hash_stable(hcx, hasher);
|
|
|
|
name.hash_stable(hcx, hasher);
|
|
|
|
}
|
2020-10-26 14:18:31 -04:00
|
|
|
ty::ReLateBound(db, ty::BoundRegion { kind: ty::BrEnv, .. }) => {
|
2018-05-28 09:52:53 -04:00
|
|
|
db.hash_stable(hcx, hasher);
|
2017-10-10 17:08:29 +02:00
|
|
|
}
|
2017-05-11 15:05:00 +03:00
|
|
|
ty::ReEarlyBound(ty::EarlyBoundRegion { def_id, index, name }) => {
|
|
|
|
def_id.hash_stable(hcx, hasher);
|
2017-03-30 15:27:27 +02:00
|
|
|
index.hash_stable(hcx, hasher);
|
|
|
|
name.hash_stable(hcx, hasher);
|
|
|
|
}
|
2017-04-05 13:00:17 +02:00
|
|
|
ty::ReFree(ref free_region) => {
|
|
|
|
free_region.hash_stable(hcx, hasher);
|
|
|
|
}
|
2021-09-21 09:31:59 +01:00
|
|
|
ty::RePlaceholder(p) => {
|
|
|
|
p.hash_stable(hcx, hasher);
|
|
|
|
}
|
|
|
|
ty::ReVar(..) => {
|
2018-05-08 07:52:01 -04:00
|
|
|
bug!("StableHasher: unexpected region {:?}", *self)
|
2017-03-30 15:27:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 10:16:38 +01:00
|
|
|
impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionVid {
|
2017-11-22 17:38:51 -05:00
|
|
|
#[inline]
|
2019-09-26 18:54:39 -04:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2017-11-22 17:38:51 -05:00
|
|
|
self.index().hash_stable(hcx, hasher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::ConstVid<'tcx> {
|
2019-02-20 01:13:49 +00:00
|
|
|
#[inline]
|
2019-09-26 18:54:39 -04:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2019-02-20 01:13:49 +00:00
|
|
|
self.index.hash_stable(hcx, hasher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> HashStable<StableHashingContext<'tcx>> for ty::BoundVar {
|
2018-02-09 10:39:36 -05:00
|
|
|
#[inline]
|
2019-09-26 18:54:39 -04:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
|
2018-02-09 10:39:36 -05:00
|
|
|
self.index().hash_stable(hcx, hasher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 16:51:33 -04:00
|
|
|
impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for ty::Binder<'tcx, T>
|
2019-06-11 13:20:33 +03:00
|
|
|
where
|
|
|
|
T: HashStable<StableHashingContext<'a>>,
|
2017-03-30 15:27:27 +02:00
|
|
|
{
|
2019-09-26 18:54:39 -04:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2020-06-24 23:40:33 +02:00
|
|
|
self.as_ref().skip_binder().hash_stable(hcx, hasher);
|
2020-10-05 20:41:46 -04:00
|
|
|
self.bound_vars().hash_stable(hcx, hasher);
|
2017-03-30 15:27:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 16:00:01 +01:00
|
|
|
// AllocIds get resolved to whatever they point to (to be stable)
|
2018-01-16 10:16:38 +01:00
|
|
|
impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::AllocId {
|
2019-09-26 18:54:39 -04:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2018-01-16 10:16:38 +01:00
|
|
|
ty::tls::with_opt(|tcx| {
|
2018-04-10 09:58:46 +02:00
|
|
|
trace!("hashing {:?}", *self);
|
2018-01-16 10:16:38 +01:00
|
|
|
let tcx = tcx.expect("can't hash AllocIds during hir lowering");
|
2020-04-24 12:53:18 +02:00
|
|
|
tcx.get_global_alloc(*self).hash_stable(hcx, hasher);
|
2018-01-16 10:16:38 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 19:49:51 +02:00
|
|
|
// `Relocations` with default type parameters is a sorted map.
|
2019-12-22 17:42:04 -05:00
|
|
|
impl<'a, Tag> HashStable<StableHashingContext<'a>> for mir::interpret::Relocations<Tag>
|
2019-08-17 19:49:51 +02:00
|
|
|
where
|
|
|
|
Tag: HashStable<StableHashingContext<'a>>,
|
|
|
|
{
|
2019-09-26 18:54:39 -04:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2019-08-17 19:49:51 +02:00
|
|
|
self.len().hash_stable(hcx, hasher);
|
|
|
|
for reloc in self.iter() {
|
2018-01-16 10:16:38 +01:00
|
|
|
reloc.hash_stable(hcx, hasher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ToStableHashKey<StableHashingContext<'a>> for region::Scope {
|
2017-09-13 18:20:27 +02:00
|
|
|
type KeyType = region::Scope;
|
|
|
|
|
|
|
|
#[inline]
|
2018-01-16 10:16:38 +01:00
|
|
|
fn to_stable_hash_key(&self, _: &StableHashingContext<'a>) -> region::Scope {
|
2017-09-13 18:20:27 +02:00
|
|
|
*self
|
2017-04-05 13:00:17 +02:00
|
|
|
}
|
|
|
|
}
|