1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This module contains `HashStable` implementations for various data types
12 //! from rustc::ty in no particular order.
14 use ich::{self, StableHashingContext, NodeIdHashingMode};
15 use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
17 use std::hash as std_hash;
19 use syntax_pos::symbol::InternedString;
22 impl_stable_hash_for!(struct ty::ItemSubsts<'tcx> { substs });
24 impl<'a, 'tcx, T> HashStable<StableHashingContext<'a, 'tcx>> for &'tcx ty::Slice<T>
25 where T: HashStable<StableHashingContext<'a, 'tcx>> {
26 fn hash_stable<W: StableHasherResult>(&self,
27 hcx: &mut StableHashingContext<'a, 'tcx>,
28 hasher: &mut StableHasher<W>) {
29 (&self[..]).hash_stable(hcx, hasher);
33 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::subst::Kind<'tcx> {
34 fn hash_stable<W: StableHasherResult>(&self,
35 hcx: &mut StableHashingContext<'a, 'tcx>,
36 hasher: &mut StableHasher<W>) {
37 self.as_type().hash_stable(hcx, hasher);
38 self.as_region().hash_stable(hcx, hasher);
42 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::RegionKind<'tcx> {
43 fn hash_stable<W: StableHasherResult>(&self,
44 hcx: &mut StableHashingContext<'a, 'tcx>,
45 hasher: &mut StableHasher<W>) {
46 mem::discriminant(self).hash_stable(hcx, hasher);
51 // No variant fields to hash for these ...
53 ty::ReLateBound(db, ty::BrAnon(i)) => {
54 db.depth.hash_stable(hcx, hasher);
55 i.hash_stable(hcx, hasher);
57 ty::ReEarlyBound(ty::EarlyBoundRegion { index, name }) => {
58 index.hash_stable(hcx, hasher);
59 name.hash_stable(hcx, hasher);
61 ty::ReScope(code_extent) => {
62 code_extent.hash_stable(hcx, hasher);
64 ty::ReFree(ref free_region) => {
65 free_region.hash_stable(hcx, hasher);
69 ty::ReSkolemized(..) => {
70 bug!("TypeIdHasher: unexpected region {:?}", *self)
76 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::adjustment::AutoBorrow<'tcx> {
77 fn hash_stable<W: StableHasherResult>(&self,
78 hcx: &mut StableHashingContext<'a, 'tcx>,
79 hasher: &mut StableHasher<W>) {
80 mem::discriminant(self).hash_stable(hcx, hasher);
82 ty::adjustment::AutoBorrow::Ref(ref region, mutability) => {
83 region.hash_stable(hcx, hasher);
84 mutability.hash_stable(hcx, hasher);
86 ty::adjustment::AutoBorrow::RawPtr(mutability) => {
87 mutability.hash_stable(hcx, hasher);
93 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::adjustment::Adjust<'tcx> {
94 fn hash_stable<W: StableHasherResult>(&self,
95 hcx: &mut StableHashingContext<'a, 'tcx>,
96 hasher: &mut StableHasher<W>) {
97 mem::discriminant(self).hash_stable(hcx, hasher);
99 ty::adjustment::Adjust::NeverToAny |
100 ty::adjustment::Adjust::ReifyFnPointer |
101 ty::adjustment::Adjust::UnsafeFnPointer |
102 ty::adjustment::Adjust::ClosureFnPointer |
103 ty::adjustment::Adjust::MutToConstPointer => {}
104 ty::adjustment::Adjust::DerefRef { autoderefs, ref autoref, unsize } => {
105 autoderefs.hash_stable(hcx, hasher);
106 autoref.hash_stable(hcx, hasher);
107 unsize.hash_stable(hcx, hasher);
113 impl_stable_hash_for!(struct ty::adjustment::Adjustment<'tcx> { kind, target });
114 impl_stable_hash_for!(struct ty::MethodCall { expr_id, autoderef });
115 impl_stable_hash_for!(struct ty::MethodCallee<'tcx> { def_id, ty, substs });
116 impl_stable_hash_for!(struct ty::UpvarId { var_id, closure_expr_id });
117 impl_stable_hash_for!(struct ty::UpvarBorrow<'tcx> { kind, region });
119 impl_stable_hash_for!(enum ty::BorrowKind {
125 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::UpvarCapture<'tcx> {
126 fn hash_stable<W: StableHasherResult>(&self,
127 hcx: &mut StableHashingContext<'a, 'tcx>,
128 hasher: &mut StableHasher<W>) {
129 mem::discriminant(self).hash_stable(hcx, hasher);
131 ty::UpvarCapture::ByValue => {}
132 ty::UpvarCapture::ByRef(ref up_var_borrow) => {
133 up_var_borrow.hash_stable(hcx, hasher);
139 impl_stable_hash_for!(struct ty::FnSig<'tcx> {
146 impl<'a, 'tcx, T> HashStable<StableHashingContext<'a, 'tcx>> for ty::Binder<T>
147 where T: HashStable<StableHashingContext<'a, 'tcx>> + ty::fold::TypeFoldable<'tcx>
149 fn hash_stable<W: StableHasherResult>(&self,
150 hcx: &mut StableHashingContext<'a, 'tcx>,
151 hasher: &mut StableHasher<W>) {
152 hcx.tcx().anonymize_late_bound_regions(self).0.hash_stable(hcx, hasher);
156 impl_stable_hash_for!(enum ty::ClosureKind { Fn, FnMut, FnOnce });
158 impl_stable_hash_for!(enum ty::Visibility {
164 impl_stable_hash_for!(struct ty::TraitRef<'tcx> { def_id, substs });
165 impl_stable_hash_for!(struct ty::TraitPredicate<'tcx> { trait_ref });
166 impl_stable_hash_for!(tuple_struct ty::EquatePredicate<'tcx> { t1, t2 });
167 impl_stable_hash_for!(struct ty::SubtypePredicate<'tcx> { a_is_expected, a, b });
169 impl<'a, 'tcx, A, B> HashStable<StableHashingContext<'a, 'tcx>> for ty::OutlivesPredicate<A, B>
170 where A: HashStable<StableHashingContext<'a, 'tcx>>,
171 B: HashStable<StableHashingContext<'a, 'tcx>>,
173 fn hash_stable<W: StableHasherResult>(&self,
174 hcx: &mut StableHashingContext<'a, 'tcx>,
175 hasher: &mut StableHasher<W>) {
176 let ty::OutlivesPredicate(ref a, ref b) = *self;
177 a.hash_stable(hcx, hasher);
178 b.hash_stable(hcx, hasher);
182 impl_stable_hash_for!(struct ty::ProjectionPredicate<'tcx> { projection_ty, ty });
183 impl_stable_hash_for!(struct ty::ProjectionTy<'tcx> { trait_ref, item_name });
186 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::Predicate<'tcx> {
187 fn hash_stable<W: StableHasherResult>(&self,
188 hcx: &mut StableHashingContext<'a, 'tcx>,
189 hasher: &mut StableHasher<W>) {
190 mem::discriminant(self).hash_stable(hcx, hasher);
192 ty::Predicate::Trait(ref pred) => {
193 pred.hash_stable(hcx, hasher);
195 ty::Predicate::Equate(ref pred) => {
196 pred.hash_stable(hcx, hasher);
198 ty::Predicate::Subtype(ref pred) => {
199 pred.hash_stable(hcx, hasher);
201 ty::Predicate::RegionOutlives(ref pred) => {
202 pred.hash_stable(hcx, hasher);
204 ty::Predicate::TypeOutlives(ref pred) => {
205 pred.hash_stable(hcx, hasher);
207 ty::Predicate::Projection(ref pred) => {
208 pred.hash_stable(hcx, hasher);
210 ty::Predicate::WellFormed(ty) => {
211 ty.hash_stable(hcx, hasher);
213 ty::Predicate::ObjectSafe(def_id) => {
214 def_id.hash_stable(hcx, hasher);
216 ty::Predicate::ClosureKind(def_id, closure_kind) => {
217 def_id.hash_stable(hcx, hasher);
218 closure_kind.hash_stable(hcx, hasher);
224 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::AdtFlags {
225 fn hash_stable<W: StableHasherResult>(&self,
226 _: &mut StableHashingContext<'a, 'tcx>,
227 hasher: &mut StableHasher<W>) {
228 std_hash::Hash::hash(self, hasher);
232 impl_stable_hash_for!(struct ty::VariantDef {
240 impl_stable_hash_for!(enum ty::VariantDiscr {
245 impl_stable_hash_for!(struct ty::FieldDef {
251 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>>
252 for ::middle::const_val::ConstVal<'tcx> {
253 fn hash_stable<W: StableHasherResult>(&self,
254 hcx: &mut StableHashingContext<'a, 'tcx>,
255 hasher: &mut StableHasher<W>) {
256 use middle::const_val::ConstVal;
258 mem::discriminant(self).hash_stable(hcx, hasher);
261 ConstVal::Float(ref value) => {
262 value.hash_stable(hcx, hasher);
264 ConstVal::Integral(ref value) => {
265 value.hash_stable(hcx, hasher);
267 ConstVal::Str(ref value) => {
268 value.hash_stable(hcx, hasher);
270 ConstVal::ByteStr(ref value) => {
271 value.hash_stable(hcx, hasher);
273 ConstVal::Bool(value) => {
274 value.hash_stable(hcx, hasher);
276 ConstVal::Char(value) => {
277 value.hash_stable(hcx, hasher);
279 ConstVal::Variant(def_id) => {
280 def_id.hash_stable(hcx, hasher);
282 ConstVal::Function(def_id, substs) => {
283 def_id.hash_stable(hcx, hasher);
284 substs.hash_stable(hcx, hasher);
286 ConstVal::Struct(ref name_value_map) => {
287 let mut values: Vec<(InternedString, &ConstVal)> =
288 name_value_map.iter()
289 .map(|(name, val)| (name.as_str(), val))
292 values.sort_unstable_by_key(|&(ref name, _)| name.clone());
293 values.hash_stable(hcx, hasher);
295 ConstVal::Tuple(ref value) => {
296 value.hash_stable(hcx, hasher);
298 ConstVal::Array(ref value) => {
299 value.hash_stable(hcx, hasher);
301 ConstVal::Repeat(ref value, times) => {
302 value.hash_stable(hcx, hasher);
303 times.hash_stable(hcx, hasher);
309 impl_stable_hash_for!(struct ty::ClosureSubsts<'tcx> { substs });
311 impl_stable_hash_for!(struct ty::GenericPredicates<'tcx> {
316 impl_stable_hash_for!(enum ty::Variance {
323 impl_stable_hash_for!(enum ty::adjustment::CustomCoerceUnsized {
327 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::Generics {
328 fn hash_stable<W: StableHasherResult>(&self,
329 hcx: &mut StableHashingContext<'a, 'tcx>,
330 hasher: &mut StableHasher<W>) {
338 // Reverse map to each `TypeParameterDef`'s `index` field, from
339 // `def_id.index` (`def_id.krate` is the same as the item's).
340 type_param_to_index: _, // Don't hash this
344 parent.hash_stable(hcx, hasher);
345 parent_regions.hash_stable(hcx, hasher);
346 parent_types.hash_stable(hcx, hasher);
347 regions.hash_stable(hcx, hasher);
348 types.hash_stable(hcx, hasher);
349 has_self.hash_stable(hcx, hasher);
353 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::RegionParameterDef {
354 fn hash_stable<W: StableHasherResult>(&self,
355 hcx: &mut StableHashingContext<'a, 'tcx>,
356 hasher: &mut StableHasher<W>) {
357 let ty::RegionParameterDef {
365 name.hash_stable(hcx, hasher);
366 def_id.hash_stable(hcx, hasher);
367 index.hash_stable(hcx, hasher);
368 pure_wrt_drop.hash_stable(hcx, hasher);
372 impl_stable_hash_for!(struct ty::TypeParameterDef {
377 object_lifetime_default,
382 impl<'a, 'tcx, T> HashStable<StableHashingContext<'a, 'tcx>>
383 for ::middle::resolve_lifetime::Set1<T>
384 where T: HashStable<StableHashingContext<'a, 'tcx>>
386 fn hash_stable<W: StableHasherResult>(&self,
387 hcx: &mut StableHashingContext<'a, 'tcx>,
388 hasher: &mut StableHasher<W>) {
389 use middle::resolve_lifetime::Set1;
391 mem::discriminant(self).hash_stable(hcx, hasher);
397 Set1::One(ref value) => {
398 value.hash_stable(hcx, hasher);
404 impl_stable_hash_for!(enum ::middle::resolve_lifetime::Region {
406 EarlyBound(index, decl),
407 LateBound(db_index, decl),
408 LateBoundAnon(db_index, anon_index),
409 Free(call_site_scope_data, decl)
412 impl_stable_hash_for!(struct ::middle::region::CallSiteScopeData {
417 impl_stable_hash_for!(struct ty::DebruijnIndex {
421 impl_stable_hash_for!(enum ty::cast::CastKind {
435 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ::middle::region::CodeExtentData
437 fn hash_stable<W: StableHasherResult>(&self,
438 hcx: &mut StableHashingContext<'a, 'tcx>,
439 hasher: &mut StableHasher<W>) {
440 use middle::region::CodeExtentData;
442 mem::discriminant(self).hash_stable(hcx, hasher);
444 CodeExtentData::Misc(node_id) |
445 CodeExtentData::DestructionScope(node_id) => {
446 node_id.hash_stable(hcx, hasher);
448 CodeExtentData::CallSiteScope { fn_id, body_id } |
449 CodeExtentData::ParameterScope { fn_id, body_id } => {
450 fn_id.hash_stable(hcx, hasher);
451 body_id.hash_stable(hcx, hasher);
453 CodeExtentData::Remainder(block_remainder) => {
454 block_remainder.hash_stable(hcx, hasher);
460 impl_stable_hash_for!(struct ::middle::region::BlockRemainder {
462 first_statement_index
465 impl_stable_hash_for!(struct ty::adjustment::CoerceUnsizedInfo {
469 impl_stable_hash_for!(struct ty::FreeRegion<'tcx> {
474 impl_stable_hash_for!(enum ty::BoundRegion {
476 BrNamed(def_id, name),
481 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::TypeVariants<'tcx>
483 fn hash_stable<W: StableHasherResult>(&self,
484 hcx: &mut StableHashingContext<'a, 'tcx>,
485 hasher: &mut StableHasher<W>) {
486 use ty::TypeVariants::*;
488 mem::discriminant(self).hash_stable(hcx, hasher);
494 // Nothing more to hash.
497 int_ty.hash_stable(hcx, hasher);
500 uint_ty.hash_stable(hcx, hasher);
502 TyFloat(float_ty) => {
503 float_ty.hash_stable(hcx, hasher);
505 TyAdt(adt_def, substs) => {
506 adt_def.hash_stable(hcx, hasher);
507 substs.hash_stable(hcx, hasher);
509 TyArray(inner_ty, len) => {
510 inner_ty.hash_stable(hcx, hasher);
511 len.hash_stable(hcx, hasher);
513 TySlice(inner_ty) => {
514 inner_ty.hash_stable(hcx, hasher);
516 TyRawPtr(pointee_ty) => {
517 pointee_ty.hash_stable(hcx, hasher);
519 TyRef(region, pointee_ty) => {
520 region.hash_stable(hcx, hasher);
521 pointee_ty.hash_stable(hcx, hasher);
523 TyFnDef(def_id, substs, ref sig) => {
524 def_id.hash_stable(hcx, hasher);
525 substs.hash_stable(hcx, hasher);
526 sig.hash_stable(hcx, hasher);
528 TyFnPtr(ref sig) => {
529 sig.hash_stable(hcx, hasher);
531 TyDynamic(ref existential_predicates, region) => {
532 existential_predicates.hash_stable(hcx, hasher);
533 region.hash_stable(hcx, hasher);
535 TyClosure(def_id, closure_substs) => {
536 def_id.hash_stable(hcx, hasher);
537 closure_substs.hash_stable(hcx, hasher);
539 TyTuple(inner_tys, from_diverging_type_var) => {
540 inner_tys.hash_stable(hcx, hasher);
541 from_diverging_type_var.hash_stable(hcx, hasher);
543 TyProjection(ref projection_ty) => {
544 projection_ty.hash_stable(hcx, hasher);
546 TyAnon(def_id, substs) => {
547 def_id.hash_stable(hcx, hasher);
548 substs.hash_stable(hcx, hasher);
550 TyParam(param_ty) => {
551 param_ty.hash_stable(hcx, hasher);
556 bug!("ty::TypeVariants::hash_stable() - Unexpected variant.")
562 impl_stable_hash_for!(struct ty::ParamTy {
567 impl_stable_hash_for!(struct ty::TypeAndMut<'tcx> {
572 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::ExistentialPredicate<'tcx>
574 fn hash_stable<W: StableHasherResult>(&self,
575 hcx: &mut StableHashingContext<'a, 'tcx>,
576 hasher: &mut StableHasher<W>) {
577 mem::discriminant(self).hash_stable(hcx, hasher);
579 ty::ExistentialPredicate::Trait(ref trait_ref) => {
580 trait_ref.hash_stable(hcx, hasher);
582 ty::ExistentialPredicate::Projection(ref projection) => {
583 projection.hash_stable(hcx, hasher);
585 ty::ExistentialPredicate::AutoTrait(def_id) => {
586 def_id.hash_stable(hcx, hasher);
592 impl_stable_hash_for!(struct ty::ExistentialTraitRef<'tcx> {
597 impl_stable_hash_for!(struct ty::ExistentialProjection<'tcx> {
604 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::TypeckTables<'tcx> {
605 fn hash_stable<W: StableHasherResult>(&self,
606 hcx: &mut StableHashingContext<'a, 'tcx>,
607 hasher: &mut StableHasher<W>) {
608 let ty::TypeckTables {
609 ref type_relative_path_defs,
614 ref upvar_capture_map,
617 ref liberated_fn_sigs,
622 // FIXME(#41184): This is still ignored at the moment.
624 ref used_trait_imports,
629 hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
630 ich::hash_stable_nodemap(hcx, hasher, type_relative_path_defs);
631 ich::hash_stable_nodemap(hcx, hasher, node_types);
632 ich::hash_stable_nodemap(hcx, hasher, item_substs);
633 ich::hash_stable_nodemap(hcx, hasher, adjustments);
635 ich::hash_stable_hashmap(hcx, hasher, method_map, |hcx, method_call| {
641 let def_id = hcx.tcx().hir.local_def_id(expr_id);
642 (hcx.def_path_hash(def_id), autoderef)
645 ich::hash_stable_hashmap(hcx, hasher, upvar_capture_map, |hcx, up_var_id| {
651 let var_def_id = hcx.tcx().hir.local_def_id(var_id);
652 let closure_def_id = hcx.tcx().hir.local_def_id(closure_expr_id);
653 (hcx.def_path_hash(var_def_id), hcx.def_path_hash(closure_def_id))
656 ich::hash_stable_nodemap(hcx, hasher, closure_tys);
657 ich::hash_stable_nodemap(hcx, hasher, closure_kinds);
658 ich::hash_stable_nodemap(hcx, hasher, liberated_fn_sigs);
659 ich::hash_stable_nodemap(hcx, hasher, fru_field_types);
660 ich::hash_stable_nodemap(hcx, hasher, cast_kinds);
662 ich::hash_stable_hashset(hcx, hasher, used_trait_imports, |hcx, def_id| {
663 hcx.def_path_hash(*def_id)
666 tainted_by_errors.hash_stable(hcx, hasher);
667 free_region_map.hash_stable(hcx, hasher);