]> git.proxmox.com Git - rustc.git/blame - src/librustc/ich/impls_ty.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / src / librustc / ich / impls_ty.rs
CommitLineData
cc61c64b
XL
1//! This module contains `HashStable` implementations for various data types
2//! from rustc::ty in no particular order.
3
dfeec247
XL
4use crate::ich::{Fingerprint, NodeIdHashingMode, StableHashingContext};
5use crate::middle::region;
6use crate::mir;
7use crate::ty;
2c00a5a8 8use rustc_data_structures::fx::FxHashMap;
dfeec247 9use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
2c00a5a8 10use std::cell::RefCell;
cc61c64b 11use std::mem;
cc61c64b 12
dc9dc135
XL
13impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for &'tcx ty::List<T>
14where
15 T: HashStable<StableHashingContext<'a>>,
16{
e74abb32 17 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
2c00a5a8
XL
18 thread_local! {
19 static CACHE: RefCell<FxHashMap<(usize, usize), Fingerprint>> =
0bf4aa26 20 RefCell::new(Default::default());
2c00a5a8
XL
21 }
22
23 let hash = CACHE.with(|cache| {
24 let key = (self.as_ptr() as usize, self.len());
25 if let Some(&hash) = cache.borrow().get(&key) {
26 return hash;
27 }
28
29 let mut hasher = StableHasher::new();
30 (&self[..]).hash_stable(hcx, &mut hasher);
31
32 let hash: Fingerprint = hasher.finish();
33 cache.borrow_mut().insert(key, hash);
34 hash
35 });
36
37 hash.hash_stable(hcx, hasher);
cc61c64b
XL
38 }
39}
40
dc9dc135
XL
41impl<'a, 'tcx, T> ToStableHashKey<StableHashingContext<'a>> for &'tcx ty::List<T>
42where
43 T: HashStable<StableHashingContext<'a>>,
83c7162d
XL
44{
45 type KeyType = Fingerprint;
46
47 #[inline]
48 fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> Fingerprint {
49 let mut hasher = StableHasher::new();
50 let mut hcx: StableHashingContext<'a> = hcx.clone();
51 self.hash_stable(&mut hcx, &mut hasher);
52 hasher.finish()
53 }
54}
55
e74abb32
XL
56impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::subst::GenericArg<'tcx> {
57 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
0531ce1d
XL
58 self.unpack().hash_stable(hcx, hasher);
59 }
60}
61
dfeec247 62impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionKind {
e74abb32 63 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
cc61c64b
XL
64 mem::discriminant(self).hash_stable(hcx, hasher);
65 match *self {
74b04a01 66 ty::ReErased | ty::ReStatic => {
cc61c64b
XL
67 // No variant fields to hash for these ...
68 }
74b04a01
XL
69 ty::ReEmpty(universe) => {
70 universe.hash_stable(hcx, hasher);
71 }
cc61c64b 72 ty::ReLateBound(db, ty::BrAnon(i)) => {
94b46f34 73 db.hash_stable(hcx, hasher);
cc61c64b
XL
74 i.hash_stable(hcx, hasher);
75 }
3b2f2976 76 ty::ReLateBound(db, ty::BrNamed(def_id, name)) => {
94b46f34 77 db.hash_stable(hcx, hasher);
3b2f2976
XL
78 def_id.hash_stable(hcx, hasher);
79 name.hash_stable(hcx, hasher);
80 }
ea8adc8c 81 ty::ReLateBound(db, ty::BrEnv) => {
94b46f34 82 db.hash_stable(hcx, hasher);
ea8adc8c 83 }
7cac9316
XL
84 ty::ReEarlyBound(ty::EarlyBoundRegion { def_id, index, name }) => {
85 def_id.hash_stable(hcx, hasher);
cc61c64b
XL
86 index.hash_stable(hcx, hasher);
87 name.hash_stable(hcx, hasher);
88 }
ea8adc8c
XL
89 ty::ReScope(scope) => {
90 scope.hash_stable(hcx, hasher);
cc61c64b
XL
91 }
92 ty::ReFree(ref free_region) => {
93 free_region.hash_stable(hcx, hasher);
94 }
ff7c6d11
XL
95 ty::ReClosureBound(vid) => {
96 vid.hash_stable(hcx, hasher);
97 }
dfeec247 98 ty::ReVar(..) | ty::RePlaceholder(..) => {
94b46f34 99 bug!("StableHasher: unexpected region {:?}", *self)
cc61c64b
XL
100 }
101 }
102 }
103}
104
0531ce1d
XL
105impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionVid {
106 #[inline]
e74abb32 107 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
0531ce1d
XL
108 self.index().hash_stable(hcx, hasher);
109 }
110}
111
dc9dc135 112impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::ConstVid<'tcx> {
ff7c6d11 113 #[inline]
e74abb32 114 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
532ac7d7 115 self.index.hash_stable(hcx, hasher);
cc61c64b
XL
116 }
117}
118
dc9dc135 119impl<'tcx> HashStable<StableHashingContext<'tcx>> for ty::BoundVar {
532ac7d7 120 #[inline]
e74abb32 121 fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
532ac7d7 122 self.index().hash_stable(hcx, hasher);
2c00a5a8
XL
123 }
124}
125
dc9dc135
XL
126impl<'a, T> HashStable<StableHashingContext<'a>> for ty::Binder<T>
127where
128 T: HashStable<StableHashingContext<'a>>,
cc61c64b 129{
e74abb32 130 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
83c7162d 131 self.skip_binder().hash_stable(hcx, hasher);
cc61c64b
XL
132 }
133}
134
a1dfa0c6 135// AllocIds get resolved to whatever they point to (to be stable)
0531ce1d 136impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::AllocId {
e74abb32 137 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
0531ce1d
XL
138 ty::tls::with_opt(|tcx| {
139 trace!("hashing {:?}", *self);
140 let tcx = tcx.expect("can't hash AllocIds during hir lowering");
b7449926 141 let alloc_kind = tcx.alloc_map.lock().get(*self);
94b46f34 142 alloc_kind.hash_stable(hcx, hasher);
0531ce1d
XL
143 });
144 }
145}
146
e1599b0c 147// `Relocations` with default type parameters is a sorted map.
dfeec247 148impl<'a, Tag> HashStable<StableHashingContext<'a>> for mir::interpret::Relocations<Tag>
e1599b0c
XL
149where
150 Tag: HashStable<StableHashingContext<'a>>,
151{
e74abb32 152 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
e1599b0c
XL
153 self.len().hash_stable(hcx, hasher);
154 for reloc in self.iter() {
0531ce1d 155 reloc.hash_stable(hcx, hasher);
ea8adc8c
XL
156 }
157 }
158}
159
0531ce1d 160impl<'a> ToStableHashKey<StableHashingContext<'a>> for region::Scope {
ea8adc8c
XL
161 type KeyType = region::Scope;
162
163 #[inline]
0531ce1d 164 fn to_stable_hash_key(&self, _: &StableHashingContext<'a>) -> region::Scope {
ea8adc8c 165 *self
cc61c64b
XL
166 }
167}
168
dc9dc135 169impl<'a> HashStable<StableHashingContext<'a>> for ty::TyVid {
e74abb32 170 fn hash_stable(&self, _hcx: &mut StableHashingContext<'a>, _hasher: &mut StableHasher) {
e1599b0c 171 // `TyVid` values are confined to an inference context and hence
0531ce1d 172 // should not be hashed.
b7449926 173 bug!("ty::TyKind::hash_stable() - can't hash a TyVid {:?}.", *self)
0531ce1d
XL
174 }
175}
176
dc9dc135 177impl<'a> HashStable<StableHashingContext<'a>> for ty::IntVid {
e74abb32 178 fn hash_stable(&self, _hcx: &mut StableHashingContext<'a>, _hasher: &mut StableHasher) {
e1599b0c 179 // `IntVid` values are confined to an inference context and hence
0531ce1d 180 // should not be hashed.
b7449926 181 bug!("ty::TyKind::hash_stable() - can't hash an IntVid {:?}.", *self)
0531ce1d
XL
182 }
183}
184
dc9dc135 185impl<'a> HashStable<StableHashingContext<'a>> for ty::FloatVid {
e74abb32 186 fn hash_stable(&self, _hcx: &mut StableHashingContext<'a>, _hasher: &mut StableHasher) {
e1599b0c 187 // `FloatVid` values are confined to an inference context and hence
0531ce1d 188 // should not be hashed.
b7449926 189 bug!("ty::TyKind::hash_stable() - can't hash a FloatVid {:?}.", *self)
0531ce1d
XL
190 }
191}
192
dc9dc135
XL
193impl<'a, T> HashStable<StableHashingContext<'a>> for ty::steal::Steal<T>
194where
195 T: HashStable<StableHashingContext<'a>>,
ea8adc8c 196{
e74abb32 197 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
ea8adc8c
XL
198 self.borrow().hash_stable(hcx, hasher);
199 }
200}
201
dfeec247 202impl<'a> HashStable<StableHashingContext<'a>> for crate::middle::privacy::AccessLevels {
e74abb32 203 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
ea8adc8c 204 hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
dfeec247 205 let crate::middle::privacy::AccessLevels { ref map } = *self;
ea8adc8c
XL
206
207 map.hash_stable(hcx, hasher);
208 });
209 }
210}