]> git.lizzy.rs Git - rust.git/blob - src/librustc/ich/impls_ty.rs
Make the rustc driver and interface demand driven
[rust.git] / src / librustc / ich / impls_ty.rs
1 //! This module contains `HashStable` implementations for various data types
2 //! from rustc::ty in no particular order.
3
4 use crate::ich::{Fingerprint, StableHashingContext, NodeIdHashingMode};
5 use rustc_data_structures::fx::FxHashMap;
6 use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
7                                            StableHasher, StableHasherResult};
8 use std::cell::RefCell;
9 use std::hash as std_hash;
10 use std::mem;
11 use crate::middle::region;
12 use crate::infer;
13 use crate::traits;
14 use crate::ty;
15 use crate::mir;
16
17 impl<'a, 'gcx, T> HashStable<StableHashingContext<'a>>
18 for &'gcx ty::List<T>
19     where T: HashStable<StableHashingContext<'a>> {
20     fn hash_stable<W: StableHasherResult>(&self,
21                                           hcx: &mut StableHashingContext<'a>,
22                                           hasher: &mut StableHasher<W>) {
23         thread_local! {
24             static CACHE: RefCell<FxHashMap<(usize, usize), Fingerprint>> =
25                 RefCell::new(Default::default());
26         }
27
28         let hash = CACHE.with(|cache| {
29             let key = (self.as_ptr() as usize, self.len());
30             if let Some(&hash) = cache.borrow().get(&key) {
31                 return hash;
32             }
33
34             let mut hasher = StableHasher::new();
35             (&self[..]).hash_stable(hcx, &mut hasher);
36
37             let hash: Fingerprint = hasher.finish();
38             cache.borrow_mut().insert(key, hash);
39             hash
40         });
41
42         hash.hash_stable(hcx, hasher);
43     }
44 }
45
46 impl<'a, 'gcx, T> ToStableHashKey<StableHashingContext<'a>> for &'gcx ty::List<T>
47     where T: HashStable<StableHashingContext<'a>>
48 {
49     type KeyType = Fingerprint;
50
51     #[inline]
52     fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> Fingerprint {
53         let mut hasher = StableHasher::new();
54         let mut hcx: StableHashingContext<'a> = hcx.clone();
55         self.hash_stable(&mut hcx, &mut hasher);
56         hasher.finish()
57     }
58 }
59
60 impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::subst::Kind<'gcx> {
61     fn hash_stable<W: StableHasherResult>(&self,
62                                           hcx: &mut StableHashingContext<'a>,
63                                           hasher: &mut StableHasher<W>) {
64         self.unpack().hash_stable(hcx, hasher);
65     }
66 }
67
68 impl<'a, 'gcx> HashStable<StableHashingContext<'a>>
69 for ty::subst::UnpackedKind<'gcx> {
70     fn hash_stable<W: StableHasherResult>(&self,
71                                           hcx: &mut StableHashingContext<'a>,
72                                           hasher: &mut StableHasher<W>) {
73         mem::discriminant(self).hash_stable(hcx, hasher);
74         match self {
75             ty::subst::UnpackedKind::Lifetime(lt) => lt.hash_stable(hcx, hasher),
76             ty::subst::UnpackedKind::Type(ty) => ty.hash_stable(hcx, hasher),
77             ty::subst::UnpackedKind::Const(ct) => ct.hash_stable(hcx, hasher),
78         }
79     }
80 }
81
82 impl<'a> HashStable<StableHashingContext<'a>>
83 for ty::RegionKind {
84     fn hash_stable<W: StableHasherResult>(&self,
85                                           hcx: &mut StableHashingContext<'a>,
86                                           hasher: &mut StableHasher<W>) {
87         mem::discriminant(self).hash_stable(hcx, hasher);
88         match *self {
89             ty::ReErased |
90             ty::ReStatic |
91             ty::ReEmpty => {
92                 // No variant fields to hash for these ...
93             }
94             ty::ReLateBound(db, ty::BrAnon(i)) => {
95                 db.hash_stable(hcx, hasher);
96                 i.hash_stable(hcx, hasher);
97             }
98             ty::ReLateBound(db, ty::BrNamed(def_id, name)) => {
99                 db.hash_stable(hcx, hasher);
100                 def_id.hash_stable(hcx, hasher);
101                 name.hash_stable(hcx, hasher);
102             }
103             ty::ReLateBound(db, ty::BrEnv) => {
104                 db.hash_stable(hcx, hasher);
105             }
106             ty::ReEarlyBound(ty::EarlyBoundRegion { def_id, index, name }) => {
107                 def_id.hash_stable(hcx, hasher);
108                 index.hash_stable(hcx, hasher);
109                 name.hash_stable(hcx, hasher);
110             }
111             ty::ReScope(scope) => {
112                 scope.hash_stable(hcx, hasher);
113             }
114             ty::ReFree(ref free_region) => {
115                 free_region.hash_stable(hcx, hasher);
116             }
117             ty::ReClosureBound(vid) => {
118                 vid.hash_stable(hcx, hasher);
119             }
120             ty::ReLateBound(..) |
121             ty::ReVar(..) |
122             ty::RePlaceholder(..) => {
123                 bug!("StableHasher: unexpected region {:?}", *self)
124             }
125         }
126     }
127 }
128
129 impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionVid {
130     #[inline]
131     fn hash_stable<W: StableHasherResult>(&self,
132                                           hcx: &mut StableHashingContext<'a>,
133                                           hasher: &mut StableHasher<W>) {
134         self.index().hash_stable(hcx, hasher);
135     }
136 }
137
138 impl<'gcx, 'tcx> HashStable<StableHashingContext<'gcx>> for ty::ConstVid<'tcx> {
139     #[inline]
140     fn hash_stable<W: StableHasherResult>(&self,
141                                           hcx: &mut StableHashingContext<'gcx>,
142                                           hasher: &mut StableHasher<W>) {
143         self.index.hash_stable(hcx, hasher);
144     }
145 }
146
147 impl<'gcx> HashStable<StableHashingContext<'gcx>> for ty::BoundVar {
148     #[inline]
149     fn hash_stable<W: StableHasherResult>(&self,
150                                           hcx: &mut StableHashingContext<'gcx>,
151                                           hasher: &mut StableHasher<W>) {
152         self.index().hash_stable(hcx, hasher);
153     }
154 }
155
156 impl<'a, 'gcx> HashStable<StableHashingContext<'a>>
157 for ty::adjustment::AutoBorrow<'gcx> {
158     fn hash_stable<W: StableHasherResult>(&self,
159                                           hcx: &mut StableHashingContext<'a>,
160                                           hasher: &mut StableHasher<W>) {
161         mem::discriminant(self).hash_stable(hcx, hasher);
162         match *self {
163             ty::adjustment::AutoBorrow::Ref(ref region, mutability) => {
164                 region.hash_stable(hcx, hasher);
165                 mutability.hash_stable(hcx, hasher);
166             }
167             ty::adjustment::AutoBorrow::RawPtr(mutability) => {
168                 mutability.hash_stable(hcx, hasher);
169             }
170         }
171     }
172 }
173
174 impl<'a, 'gcx> HashStable<StableHashingContext<'a>>
175 for ty::adjustment::Adjust<'gcx> {
176     fn hash_stable<W: StableHasherResult>(&self,
177                                           hcx: &mut StableHashingContext<'a>,
178                                           hasher: &mut StableHasher<W>) {
179         mem::discriminant(self).hash_stable(hcx, hasher);
180         match *self {
181             ty::adjustment::Adjust::NeverToAny |
182             ty::adjustment::Adjust::ReifyFnPointer |
183             ty::adjustment::Adjust::UnsafeFnPointer |
184             ty::adjustment::Adjust::ClosureFnPointer |
185             ty::adjustment::Adjust::MutToConstPointer |
186             ty::adjustment::Adjust::Unsize => {}
187             ty::adjustment::Adjust::Deref(ref overloaded) => {
188                 overloaded.hash_stable(hcx, hasher);
189             }
190             ty::adjustment::Adjust::Borrow(ref autoref) => {
191                 autoref.hash_stable(hcx, hasher);
192             }
193         }
194     }
195 }
196
197 impl_stable_hash_for!(struct ty::adjustment::Adjustment<'tcx> { kind, target });
198 impl_stable_hash_for!(struct ty::adjustment::OverloadedDeref<'tcx> { region, mutbl });
199 impl_stable_hash_for!(struct ty::UpvarBorrow<'tcx> { kind, region });
200 impl_stable_hash_for!(enum ty::adjustment::AllowTwoPhase {
201     Yes,
202     No
203 });
204
205 impl<'gcx> HashStable<StableHashingContext<'gcx>> for ty::adjustment::AutoBorrowMutability {
206     fn hash_stable<W: StableHasherResult>(&self,
207                                           hcx: &mut StableHashingContext<'gcx>,
208                                           hasher: &mut StableHasher<W>) {
209         mem::discriminant(self).hash_stable(hcx, hasher);
210         match *self {
211             ty::adjustment::AutoBorrowMutability::Mutable { ref allow_two_phase_borrow } => {
212                 allow_two_phase_borrow.hash_stable(hcx, hasher);
213             }
214             ty::adjustment::AutoBorrowMutability::Immutable => {}
215         }
216     }
217 }
218
219 impl_stable_hash_for!(tuple_struct ty::util::NeedsDrop { value });
220
221 impl_stable_hash_for!(tuple_struct ty::AdtSizedConstraint<'tcx> { list });
222
223 impl_stable_hash_for!(struct ty::UpvarPath { hir_id });
224
225 impl_stable_hash_for!(struct ty::UpvarId { var_path, closure_expr_id });
226
227 impl_stable_hash_for!(enum ty::BorrowKind {
228     ImmBorrow,
229     UniqueImmBorrow,
230     MutBorrow
231 });
232
233 impl_stable_hash_for!(impl<'gcx> for enum ty::UpvarCapture<'gcx> [ ty::UpvarCapture ] {
234     ByValue,
235     ByRef(up_var_borrow),
236 });
237
238 impl_stable_hash_for!(struct ty::GenSig<'tcx> {
239     yield_ty,
240     return_ty
241 });
242
243 impl_stable_hash_for!(struct ty::FnSig<'tcx> {
244     inputs_and_output,
245     c_variadic,
246     unsafety,
247     abi
248 });
249
250 impl_stable_hash_for!(struct ty::ResolvedOpaqueTy<'tcx> {
251     concrete_type,
252     substs
253 });
254
255 impl<'a, 'gcx, T> HashStable<StableHashingContext<'a>> for ty::Binder<T>
256     where T: HashStable<StableHashingContext<'a>>
257 {
258     fn hash_stable<W: StableHasherResult>(&self,
259                                           hcx: &mut StableHashingContext<'a>,
260                                           hasher: &mut StableHasher<W>) {
261         self.skip_binder().hash_stable(hcx, hasher);
262     }
263 }
264
265 impl_stable_hash_for!(enum ty::ClosureKind { Fn, FnMut, FnOnce });
266
267 impl_stable_hash_for!(enum ty::Visibility {
268     Public,
269     Restricted(def_id),
270     Invisible
271 });
272
273 impl_stable_hash_for!(struct ty::TraitRef<'tcx> { def_id, substs });
274 impl_stable_hash_for!(struct ty::TraitPredicate<'tcx> { trait_ref });
275 impl_stable_hash_for!(struct ty::SubtypePredicate<'tcx> { a_is_expected, a, b });
276 impl_stable_hash_for!(impl<A, B> for tuple_struct ty::OutlivesPredicate<A, B> { a, b });
277 impl_stable_hash_for!(struct ty::ProjectionPredicate<'tcx> { projection_ty, ty });
278 impl_stable_hash_for!(struct ty::ProjectionTy<'tcx> { substs, item_def_id });
279
280 impl_stable_hash_for!(
281     impl<'tcx> for enum ty::Predicate<'tcx> [ ty::Predicate ] {
282         Trait(pred),
283         Subtype(pred),
284         RegionOutlives(pred),
285         TypeOutlives(pred),
286         Projection(pred),
287         WellFormed(ty),
288         ObjectSafe(def_id),
289         ClosureKind(def_id, closure_substs, closure_kind),
290         ConstEvaluatable(def_id, substs),
291     }
292 );
293
294 impl<'a> HashStable<StableHashingContext<'a>> for ty::AdtFlags {
295     fn hash_stable<W: StableHasherResult>(&self,
296                                           _: &mut StableHashingContext<'a>,
297                                           hasher: &mut StableHasher<W>) {
298         std_hash::Hash::hash(self, hasher);
299     }
300 }
301
302 impl<'a> HashStable<StableHashingContext<'a>> for ty::VariantFlags {
303     fn hash_stable<W: StableHasherResult>(&self,
304                                           _: &mut StableHashingContext<'a>,
305                                           hasher: &mut StableHasher<W>) {
306         std_hash::Hash::hash(self, hasher);
307     }
308 }
309
310 impl_stable_hash_for!(
311     impl<'tcx> for enum ty::InferConst<'tcx> [ ty::InferConst ] {
312         Var(vid),
313         Fresh(i),
314         Canonical(debruijn, var),
315     }
316 );
317
318 impl_stable_hash_for!(enum ty::VariantDiscr {
319     Explicit(def_id),
320     Relative(distance)
321 });
322
323 impl_stable_hash_for!(struct ty::FieldDef {
324     did,
325     ident -> (ident.name),
326     vis,
327 });
328
329 impl_stable_hash_for!(
330     impl<'tcx> for enum mir::interpret::ConstValue<'tcx> [ mir::interpret::ConstValue ] {
331         Param(param),
332         Infer(infer),
333         Scalar(val),
334         Slice(a, b),
335         ByRef(ptr, alloc),
336     }
337 );
338
339 impl_stable_hash_for!(struct crate::mir::interpret::RawConst<'tcx> {
340     alloc_id,
341     ty,
342 });
343
344 impl_stable_hash_for! {
345     impl<Tag> for struct mir::interpret::Pointer<Tag> {
346         alloc_id,
347         offset,
348         tag,
349     }
350 }
351
352 impl_stable_hash_for!(
353     impl<Tag> for enum mir::interpret::Scalar<Tag> [ mir::interpret::Scalar ] {
354         Bits { bits, size },
355         Ptr(ptr),
356     }
357 );
358
359 impl_stable_hash_for!(
360     impl<'tcx> for enum mir::interpret::AllocKind<'tcx> [ mir::interpret::AllocKind ] {
361         Function(instance),
362         Static(def_id),
363         Memory(mem),
364     }
365 );
366
367 // AllocIds get resolved to whatever they point to (to be stable)
368 impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::AllocId {
369     fn hash_stable<W: StableHasherResult>(
370         &self,
371         hcx: &mut StableHashingContext<'a>,
372         hasher: &mut StableHasher<W>,
373     ) {
374         ty::tls::with_opt(|tcx| {
375             trace!("hashing {:?}", *self);
376             let tcx = tcx.expect("can't hash AllocIds during hir lowering");
377             let alloc_kind = tcx.alloc_map.lock().get(*self);
378             alloc_kind.hash_stable(hcx, hasher);
379         });
380     }
381 }
382
383 // Allocations treat their relocations specially
384 impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::Allocation {
385     fn hash_stable<W: StableHasherResult>(
386         &self,
387         hcx: &mut StableHashingContext<'a>,
388         hasher: &mut StableHasher<W>,
389     ) {
390         self.bytes.hash_stable(hcx, hasher);
391         for reloc in self.relocations.iter() {
392             reloc.hash_stable(hcx, hasher);
393         }
394         self.undef_mask.hash_stable(hcx, hasher);
395         self.align.hash_stable(hcx, hasher);
396         self.mutability.hash_stable(hcx, hasher);
397     }
398 }
399
400 impl_stable_hash_for!(enum ::syntax::ast::Mutability {
401     Immutable,
402     Mutable
403 });
404
405 impl_stable_hash_for!(struct ty::Const<'tcx> {
406     ty,
407     val
408 });
409
410 impl_stable_hash_for!(impl<'tcx> for enum ty::LazyConst<'tcx> [ty::LazyConst] {
411     Unevaluated(did, substs),
412     Evaluated(c)
413 });
414
415 impl_stable_hash_for!(enum mir::interpret::ErrorHandled {
416     Reported,
417     TooGeneric
418 });
419
420 impl_stable_hash_for!(struct mir::interpret::FrameInfo<'tcx> {
421     call_site,
422     lint_root,
423     instance
424 });
425
426 impl_stable_hash_for!(struct ty::ClosureSubsts<'tcx> { substs });
427 impl_stable_hash_for!(struct ty::GeneratorSubsts<'tcx> { substs });
428
429 impl_stable_hash_for!(struct ty::GenericPredicates<'tcx> {
430     parent,
431     predicates
432 });
433
434 impl_stable_hash_for!(
435     impl<'tcx, O> for enum mir::interpret::EvalErrorKind<'tcx, O>
436         [ mir::interpret::EvalErrorKind ]
437     {
438         FunctionArgCountMismatch,
439         DanglingPointerDeref,
440         DoubleFree,
441         InvalidMemoryAccess,
442         InvalidFunctionPointer,
443         InvalidBool,
444         InvalidNullPointerUsage,
445         ReadPointerAsBytes,
446         ReadBytesAsPointer,
447         ReadForeignStatic,
448         InvalidPointerMath,
449         DeadLocal,
450         StackFrameLimitReached,
451         OutOfTls,
452         TlsOutOfBounds,
453         CalledClosureAsFunction,
454         VtableForArgumentlessMethod,
455         ModifiedConstantMemory,
456         ModifiedStatic,
457         AssumptionNotHeld,
458         InlineAsm,
459         ReallocateNonBasePtr,
460         DeallocateNonBasePtr,
461         HeapAllocZeroBytes,
462         Unreachable,
463         ReadFromReturnPointer,
464         UnimplementedTraitSelection,
465         TypeckError,
466         TooGeneric,
467         DerefFunctionPointer,
468         ExecuteMemory,
469         OverflowNeg,
470         RemainderByZero,
471         DivisionByZero,
472         GeneratorResumedAfterReturn,
473         GeneratorResumedAfterPanic,
474         ReferencedConstant,
475         InfiniteLoop,
476         ReadUndefBytes(offset),
477         InvalidDiscriminant(val),
478         Panic { msg, file, line, col },
479         MachineError(err),
480         FunctionAbiMismatch(a, b),
481         FunctionArgMismatch(a, b),
482         FunctionRetMismatch(a, b),
483         NoMirFor(s),
484         UnterminatedCString(ptr),
485         PointerOutOfBounds { ptr, check, allocation_size },
486         InvalidBoolOp(bop),
487         Unimplemented(s),
488         BoundsCheck { len, index },
489         Intrinsic(s),
490         InvalidChar(c),
491         AbiViolation(s),
492         AlignmentCheckFailed { required, has },
493         ValidationFailure(s),
494         TypeNotPrimitive(ty),
495         ReallocatedWrongMemoryKind(a, b),
496         DeallocatedWrongMemoryKind(a, b),
497         IncorrectAllocationInformation(a, b, c, d),
498         Layout(lay),
499         HeapAllocNonPowerOfTwoAlignment(n),
500         PathNotFound(v),
501         Overflow(op),
502     }
503 );
504
505 impl_stable_hash_for!(enum mir::interpret::InboundsCheck {
506     Live,
507     MaybeDead
508 });
509
510 impl_stable_hash_for!(enum ty::Variance {
511     Covariant,
512     Invariant,
513     Contravariant,
514     Bivariant
515 });
516
517 impl_stable_hash_for!(enum ty::adjustment::CustomCoerceUnsized {
518     Struct(index)
519 });
520
521 impl_stable_hash_for!(struct ty::Generics {
522     parent,
523     parent_count,
524     params,
525     // Reverse map to each param's `index` field, from its `def_id`.
526     param_def_id_to_index -> _, // Don't hash this
527     has_self,
528     has_late_bound_regions,
529 });
530
531 impl_stable_hash_for!(struct ty::GenericParamDef {
532     name,
533     def_id,
534     index,
535     pure_wrt_drop,
536     kind
537 });
538
539 impl_stable_hash_for!(enum ty::GenericParamDefKind {
540     Lifetime,
541     Type { has_default, object_lifetime_default, synthetic },
542     Const,
543 });
544
545 impl_stable_hash_for!(
546     impl<T> for enum crate::middle::resolve_lifetime::Set1<T>
547         [ crate::middle::resolve_lifetime::Set1 ]
548     {
549         Empty,
550         Many,
551         One(value),
552     }
553 );
554
555 impl_stable_hash_for!(enum crate::middle::resolve_lifetime::LifetimeDefOrigin {
556     ExplicitOrElided,
557     InBand,
558     Error,
559 });
560
561 impl_stable_hash_for!(enum crate::middle::resolve_lifetime::Region {
562     Static,
563     EarlyBound(index, decl, is_in_band),
564     LateBound(db_index, decl, is_in_band),
565     LateBoundAnon(db_index, anon_index),
566     Free(call_site_scope_data, decl)
567 });
568
569 impl_stable_hash_for!(enum ty::cast::CastKind {
570     CoercionCast,
571     PtrPtrCast,
572     PtrAddrCast,
573     AddrPtrCast,
574     NumericCast,
575     EnumCast,
576     PrimIntCast,
577     U8CharCast,
578     ArrayPtrCast,
579     FnPtrPtrCast,
580     FnPtrAddrCast
581 });
582
583 impl_stable_hash_for!(struct crate::middle::region::Scope { id, data });
584
585 impl_stable_hash_for!(enum crate::middle::region::ScopeData {
586     Node,
587     CallSite,
588     Arguments,
589     Destruction,
590     Remainder(first_statement_index)
591 });
592
593 impl<'a> ToStableHashKey<StableHashingContext<'a>> for region::Scope {
594     type KeyType = region::Scope;
595
596     #[inline]
597     fn to_stable_hash_key(&self, _: &StableHashingContext<'a>) -> region::Scope {
598         *self
599     }
600 }
601
602 impl_stable_hash_for!(struct ty::adjustment::CoerceUnsizedInfo {
603     custom_kind
604 });
605
606 impl_stable_hash_for!(struct ty::FreeRegion {
607     scope,
608     bound_region
609 });
610
611 impl_stable_hash_for!(enum ty::BoundRegion {
612     BrAnon(index),
613     BrNamed(def_id, name),
614     BrFresh(index),
615     BrEnv
616 });
617
618 impl<'a, 'gcx> HashStable<StableHashingContext<'a>>
619 for ty::TyKind<'gcx>
620 {
621     fn hash_stable<W: StableHasherResult>(&self,
622                                           hcx: &mut StableHashingContext<'a>,
623                                           hasher: &mut StableHasher<W>) {
624         use crate::ty::TyKind::*;
625
626         mem::discriminant(self).hash_stable(hcx, hasher);
627         match *self {
628             Bool  |
629             Char  |
630             Str   |
631             Error |
632             Never => {
633                 // Nothing more to hash.
634             }
635             Int(int_ty) => {
636                 int_ty.hash_stable(hcx, hasher);
637             }
638             Uint(uint_ty) => {
639                 uint_ty.hash_stable(hcx, hasher);
640             }
641             Float(float_ty)  => {
642                 float_ty.hash_stable(hcx, hasher);
643             }
644             Adt(adt_def, substs) => {
645                 adt_def.hash_stable(hcx, hasher);
646                 substs.hash_stable(hcx, hasher);
647             }
648             Array(inner_ty, len) => {
649                 inner_ty.hash_stable(hcx, hasher);
650                 len.hash_stable(hcx, hasher);
651             }
652             Slice(inner_ty) => {
653                 inner_ty.hash_stable(hcx, hasher);
654             }
655             RawPtr(pointee_ty) => {
656                 pointee_ty.hash_stable(hcx, hasher);
657             }
658             Ref(region, pointee_ty, mutbl) => {
659                 region.hash_stable(hcx, hasher);
660                 pointee_ty.hash_stable(hcx, hasher);
661                 mutbl.hash_stable(hcx, hasher);
662             }
663             FnDef(def_id, substs) => {
664                 def_id.hash_stable(hcx, hasher);
665                 substs.hash_stable(hcx, hasher);
666             }
667             FnPtr(ref sig) => {
668                 sig.hash_stable(hcx, hasher);
669             }
670             Dynamic(ref existential_predicates, region) => {
671                 existential_predicates.hash_stable(hcx, hasher);
672                 region.hash_stable(hcx, hasher);
673             }
674             Closure(def_id, closure_substs) => {
675                 def_id.hash_stable(hcx, hasher);
676                 closure_substs.hash_stable(hcx, hasher);
677             }
678             Generator(def_id, generator_substs, movability) => {
679                 def_id.hash_stable(hcx, hasher);
680                 generator_substs.hash_stable(hcx, hasher);
681                 movability.hash_stable(hcx, hasher);
682             }
683             GeneratorWitness(types) => {
684                 types.hash_stable(hcx, hasher)
685             }
686             Tuple(inner_tys) => {
687                 inner_tys.hash_stable(hcx, hasher);
688             }
689             Projection(ref data) | UnnormalizedProjection(ref data) => {
690                 data.hash_stable(hcx, hasher);
691             }
692             Opaque(def_id, substs) => {
693                 def_id.hash_stable(hcx, hasher);
694                 substs.hash_stable(hcx, hasher);
695             }
696             Param(param_ty) => {
697                 param_ty.hash_stable(hcx, hasher);
698             }
699             Bound(debruijn, bound_ty) => {
700                 debruijn.hash_stable(hcx, hasher);
701                 bound_ty.hash_stable(hcx, hasher);
702             }
703             ty::Placeholder(placeholder_ty) => {
704                 placeholder_ty.hash_stable(hcx, hasher);
705             }
706             Foreign(def_id) => {
707                 def_id.hash_stable(hcx, hasher);
708             }
709             Infer(infer_ty) => {
710                 infer_ty.hash_stable(hcx, hasher);
711             }
712         }
713     }
714 }
715
716 impl_stable_hash_for!(enum ty::InferTy {
717     TyVar(a),
718     IntVar(a),
719     FloatVar(a),
720     FreshTy(a),
721     FreshIntTy(a),
722     FreshFloatTy(a),
723 });
724
725 impl<'a, 'gcx> HashStable<StableHashingContext<'a>>
726 for ty::TyVid
727 {
728     fn hash_stable<W: StableHasherResult>(&self,
729                                           _hcx: &mut StableHashingContext<'a>,
730                                           _hasher: &mut StableHasher<W>) {
731         // TyVid values are confined to an inference context and hence
732         // should not be hashed.
733         bug!("ty::TyKind::hash_stable() - can't hash a TyVid {:?}.", *self)
734     }
735 }
736
737 impl<'a, 'gcx> HashStable<StableHashingContext<'a>>
738 for ty::IntVid
739 {
740     fn hash_stable<W: StableHasherResult>(&self,
741                                           _hcx: &mut StableHashingContext<'a>,
742                                           _hasher: &mut StableHasher<W>) {
743         // IntVid values are confined to an inference context and hence
744         // should not be hashed.
745         bug!("ty::TyKind::hash_stable() - can't hash an IntVid {:?}.", *self)
746     }
747 }
748
749 impl<'a, 'gcx> HashStable<StableHashingContext<'a>>
750 for ty::FloatVid
751 {
752     fn hash_stable<W: StableHasherResult>(&self,
753                                           _hcx: &mut StableHashingContext<'a>,
754                                           _hasher: &mut StableHasher<W>) {
755         // FloatVid values are confined to an inference context and hence
756         // should not be hashed.
757         bug!("ty::TyKind::hash_stable() - can't hash a FloatVid {:?}.", *self)
758     }
759 }
760
761 impl_stable_hash_for!(struct ty::ParamConst {
762     index,
763     name
764 });
765
766 impl_stable_hash_for!(struct ty::ParamTy {
767     idx,
768     name
769 });
770
771 impl_stable_hash_for!(struct ty::TypeAndMut<'tcx> {
772     ty,
773     mutbl
774 });
775
776 impl<'a, 'gcx> HashStable<StableHashingContext<'a>>
777 for ty::ExistentialPredicate<'gcx>
778 {
779     fn hash_stable<W: StableHasherResult>(&self,
780                                           hcx: &mut StableHashingContext<'a>,
781                                           hasher: &mut StableHasher<W>) {
782         mem::discriminant(self).hash_stable(hcx, hasher);
783         match *self {
784             ty::ExistentialPredicate::Trait(ref trait_ref) => {
785                 trait_ref.hash_stable(hcx, hasher);
786             }
787             ty::ExistentialPredicate::Projection(ref projection) => {
788                 projection.hash_stable(hcx, hasher);
789             }
790             ty::ExistentialPredicate::AutoTrait(def_id) => {
791                 def_id.hash_stable(hcx, hasher);
792             }
793         }
794     }
795 }
796
797 impl_stable_hash_for!(struct ty::ExistentialTraitRef<'tcx> {
798     def_id,
799     substs
800 });
801
802 impl_stable_hash_for!(struct ty::ExistentialProjection<'tcx> {
803     item_def_id,
804     substs,
805     ty
806 });
807
808 impl_stable_hash_for!(struct ty::Instance<'tcx> {
809     def,
810     substs
811 });
812
813 impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::InstanceDef<'gcx> {
814     fn hash_stable<W: StableHasherResult>(&self,
815                                           hcx: &mut StableHashingContext<'a>,
816                                           hasher: &mut StableHasher<W>) {
817         mem::discriminant(self).hash_stable(hcx, hasher);
818
819         match *self {
820             ty::InstanceDef::Item(def_id) => {
821                 def_id.hash_stable(hcx, hasher);
822             }
823             ty::InstanceDef::VtableShim(def_id) => {
824                 def_id.hash_stable(hcx, hasher);
825             }
826             ty::InstanceDef::Intrinsic(def_id) => {
827                 def_id.hash_stable(hcx, hasher);
828             }
829             ty::InstanceDef::FnPtrShim(def_id, ty) => {
830                 def_id.hash_stable(hcx, hasher);
831                 ty.hash_stable(hcx, hasher);
832             }
833             ty::InstanceDef::Virtual(def_id, n) => {
834                 def_id.hash_stable(hcx, hasher);
835                 n.hash_stable(hcx, hasher);
836             }
837             ty::InstanceDef::ClosureOnceShim { call_once } => {
838                 call_once.hash_stable(hcx, hasher);
839             }
840             ty::InstanceDef::DropGlue(def_id, ty) => {
841                 def_id.hash_stable(hcx, hasher);
842                 ty.hash_stable(hcx, hasher);
843             }
844             ty::InstanceDef::CloneShim(def_id, ty) => {
845                 def_id.hash_stable(hcx, hasher);
846                 ty.hash_stable(hcx, hasher);
847             }
848         }
849     }
850 }
851
852 impl_stable_hash_for!(struct ty::TraitDef {
853     // We already have the def_path_hash below, no need to hash it twice
854     def_id -> _,
855     unsafety,
856     paren_sugar,
857     has_auto_impl,
858     is_marker,
859     def_path_hash,
860 });
861
862 impl_stable_hash_for!(struct ty::Destructor {
863     did
864 });
865
866 impl_stable_hash_for!(struct ty::CrateVariancesMap {
867     variances,
868     // This is just an irrelevant helper value.
869     empty_variance -> _,
870 });
871
872 impl_stable_hash_for!(struct ty::CratePredicatesMap<'tcx> {
873     predicates,
874     // This is just an irrelevant helper value.
875     empty_predicate -> _,
876 });
877
878 impl_stable_hash_for!(struct ty::AssociatedItem {
879     def_id,
880     ident -> (ident.name),
881     kind,
882     vis,
883     defaultness,
884     container,
885     method_has_self_argument
886 });
887
888 impl_stable_hash_for!(enum ty::AssociatedKind {
889     Const,
890     Method,
891     Existential,
892     Type
893 });
894
895 impl_stable_hash_for!(enum ty::AssociatedItemContainer {
896     TraitContainer(def_id),
897     ImplContainer(def_id)
898 });
899
900
901 impl<'a, 'gcx, T> HashStable<StableHashingContext<'a>>
902 for ty::steal::Steal<T>
903     where T: HashStable<StableHashingContext<'a>>
904 {
905     fn hash_stable<W: StableHasherResult>(&self,
906                                           hcx: &mut StableHashingContext<'a>,
907                                           hasher: &mut StableHasher<W>) {
908         self.borrow().hash_stable(hcx, hasher);
909     }
910 }
911
912 impl_stable_hash_for!(struct ty::ParamEnv<'tcx> {
913     caller_bounds,
914     reveal,
915     def_id
916 });
917
918 impl_stable_hash_for!(enum traits::Reveal {
919     UserFacing,
920     All
921 });
922
923 impl_stable_hash_for!(enum crate::middle::privacy::AccessLevel {
924     ReachableFromImplTrait,
925     Reachable,
926     Exported,
927     Public
928 });
929
930 impl<'a> HashStable<StableHashingContext<'a>>
931 for crate::middle::privacy::AccessLevels {
932     fn hash_stable<W: StableHasherResult>(&self,
933                                           hcx: &mut StableHashingContext<'a>,
934                                           hasher: &mut StableHasher<W>) {
935         hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
936             let crate::middle::privacy::AccessLevels {
937                 ref map
938             } = *self;
939
940             map.hash_stable(hcx, hasher);
941         });
942     }
943 }
944
945 impl_stable_hash_for!(struct ty::CrateInherentImpls {
946     inherent_impls
947 });
948
949 impl_stable_hash_for!(struct crate::util::common::ErrorReported {});
950
951 impl_stable_hash_for!(tuple_struct crate::middle::reachable::ReachableSet {
952     reachable_set
953 });
954
955 impl<'a, 'gcx, N> HashStable<StableHashingContext<'a>>
956 for traits::Vtable<'gcx, N> where N: HashStable<StableHashingContext<'a>> {
957     fn hash_stable<W: StableHasherResult>(&self,
958                                           hcx: &mut StableHashingContext<'a>,
959                                           hasher: &mut StableHasher<W>) {
960         use crate::traits::Vtable::*;
961
962         mem::discriminant(self).hash_stable(hcx, hasher);
963
964         match self {
965             &VtableImpl(ref table_impl) => table_impl.hash_stable(hcx, hasher),
966             &VtableAutoImpl(ref table_def_impl) => table_def_impl.hash_stable(hcx, hasher),
967             &VtableParam(ref table_param) => table_param.hash_stable(hcx, hasher),
968             &VtableObject(ref table_obj) => table_obj.hash_stable(hcx, hasher),
969             &VtableBuiltin(ref table_builtin) => table_builtin.hash_stable(hcx, hasher),
970             &VtableClosure(ref table_closure) => table_closure.hash_stable(hcx, hasher),
971             &VtableFnPointer(ref table_fn_pointer) => table_fn_pointer.hash_stable(hcx, hasher),
972             &VtableGenerator(ref table_generator) => table_generator.hash_stable(hcx, hasher),
973             &VtableTraitAlias(ref table_alias) => table_alias.hash_stable(hcx, hasher),
974         }
975     }
976 }
977
978 impl<'a, 'gcx, N> HashStable<StableHashingContext<'a>>
979 for traits::VtableImplData<'gcx, N> where N: HashStable<StableHashingContext<'a>> {
980     fn hash_stable<W: StableHasherResult>(&self,
981                                           hcx: &mut StableHashingContext<'a>,
982                                           hasher: &mut StableHasher<W>) {
983         let traits::VtableImplData {
984             impl_def_id,
985             substs,
986             ref nested,
987         } = *self;
988         impl_def_id.hash_stable(hcx, hasher);
989         substs.hash_stable(hcx, hasher);
990         nested.hash_stable(hcx, hasher);
991     }
992 }
993
994 impl<'a, 'gcx, N> HashStable<StableHashingContext<'a>>
995 for traits::VtableAutoImplData<N> where N: HashStable<StableHashingContext<'a>> {
996     fn hash_stable<W: StableHasherResult>(&self,
997                                           hcx: &mut StableHashingContext<'a>,
998                                           hasher: &mut StableHasher<W>) {
999         let traits::VtableAutoImplData {
1000             trait_def_id,
1001             ref nested,
1002         } = *self;
1003         trait_def_id.hash_stable(hcx, hasher);
1004         nested.hash_stable(hcx, hasher);
1005     }
1006 }
1007
1008 impl<'a, 'gcx, N> HashStable<StableHashingContext<'a>>
1009 for traits::VtableObjectData<'gcx, N> where N: HashStable<StableHashingContext<'a>> {
1010     fn hash_stable<W: StableHasherResult>(&self,
1011                                           hcx: &mut StableHashingContext<'a>,
1012                                           hasher: &mut StableHasher<W>) {
1013         let traits::VtableObjectData {
1014             upcast_trait_ref,
1015             vtable_base,
1016             ref nested,
1017         } = *self;
1018         upcast_trait_ref.hash_stable(hcx, hasher);
1019         vtable_base.hash_stable(hcx, hasher);
1020         nested.hash_stable(hcx, hasher);
1021     }
1022 }
1023
1024 impl<'a, 'gcx, N> HashStable<StableHashingContext<'a>>
1025 for traits::VtableBuiltinData<N> where N: HashStable<StableHashingContext<'a>> {
1026     fn hash_stable<W: StableHasherResult>(&self,
1027                                           hcx: &mut StableHashingContext<'a>,
1028                                           hasher: &mut StableHasher<W>) {
1029         let traits::VtableBuiltinData {
1030             ref nested,
1031         } = *self;
1032         nested.hash_stable(hcx, hasher);
1033     }
1034 }
1035
1036 impl<'a, 'gcx, N> HashStable<StableHashingContext<'a>>
1037 for traits::VtableClosureData<'gcx, N> where N: HashStable<StableHashingContext<'a>> {
1038     fn hash_stable<W: StableHasherResult>(&self,
1039                                           hcx: &mut StableHashingContext<'a>,
1040                                           hasher: &mut StableHasher<W>) {
1041         let traits::VtableClosureData {
1042             closure_def_id,
1043             substs,
1044             ref nested,
1045         } = *self;
1046         closure_def_id.hash_stable(hcx, hasher);
1047         substs.hash_stable(hcx, hasher);
1048         nested.hash_stable(hcx, hasher);
1049     }
1050 }
1051
1052 impl<'a, 'gcx, N> HashStable<StableHashingContext<'a>>
1053 for traits::VtableFnPointerData<'gcx, N> where N: HashStable<StableHashingContext<'a>> {
1054     fn hash_stable<W: StableHasherResult>(&self,
1055                                           hcx: &mut StableHashingContext<'a>,
1056                                           hasher: &mut StableHasher<W>) {
1057         let traits::VtableFnPointerData {
1058             fn_ty,
1059             ref nested,
1060         } = *self;
1061         fn_ty.hash_stable(hcx, hasher);
1062         nested.hash_stable(hcx, hasher);
1063     }
1064 }
1065
1066 impl<'a, 'gcx, N> HashStable<StableHashingContext<'a>>
1067 for traits::VtableGeneratorData<'gcx, N> where N: HashStable<StableHashingContext<'a>> {
1068     fn hash_stable<W: StableHasherResult>(&self,
1069                                           hcx: &mut StableHashingContext<'a>,
1070                                           hasher: &mut StableHasher<W>) {
1071         let traits::VtableGeneratorData {
1072             generator_def_id,
1073             substs,
1074             ref nested,
1075         } = *self;
1076         generator_def_id.hash_stable(hcx, hasher);
1077         substs.hash_stable(hcx, hasher);
1078         nested.hash_stable(hcx, hasher);
1079     }
1080 }
1081
1082 impl<'a, 'gcx, N> HashStable<StableHashingContext<'a>>
1083 for traits::VtableTraitAliasData<'gcx, N> where N: HashStable<StableHashingContext<'a>> {
1084     fn hash_stable<W: StableHasherResult>(&self,
1085                                           hcx: &mut StableHashingContext<'a>,
1086                                           hasher: &mut StableHasher<W>) {
1087         let traits::VtableTraitAliasData {
1088             alias_def_id,
1089             substs,
1090             ref nested,
1091         } = *self;
1092         alias_def_id.hash_stable(hcx, hasher);
1093         substs.hash_stable(hcx, hasher);
1094         nested.hash_stable(hcx, hasher);
1095     }
1096 }
1097
1098 impl_stable_hash_for!(
1099     impl<'tcx, V> for struct infer::canonical::Canonical<'tcx, V> {
1100         max_universe, variables, value
1101     }
1102 );
1103
1104 impl_stable_hash_for!(
1105     struct infer::canonical::CanonicalVarValues<'tcx> {
1106         var_values
1107     }
1108 );
1109
1110 impl_stable_hash_for!(struct infer::canonical::CanonicalVarInfo {
1111     kind
1112 });
1113
1114 impl_stable_hash_for!(enum infer::canonical::CanonicalVarKind {
1115     Ty(k),
1116     PlaceholderTy(placeholder),
1117     Region(ui),
1118     PlaceholderRegion(placeholder),
1119 });
1120
1121 impl_stable_hash_for!(enum infer::canonical::CanonicalTyVarKind {
1122     General(ui),
1123     Int,
1124     Float
1125 });
1126
1127 impl_stable_hash_for!(
1128     impl<'tcx, R> for struct infer::canonical::QueryResponse<'tcx, R> {
1129         var_values, region_constraints, certainty, value
1130     }
1131 );
1132
1133 impl_stable_hash_for!(enum infer::canonical::Certainty {
1134     Proven, Ambiguous
1135 });
1136
1137 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::WhereClause<'tcx> {
1138     fn hash_stable<W: StableHasherResult>(&self,
1139                                           hcx: &mut StableHashingContext<'a>,
1140                                           hasher: &mut StableHasher<W>) {
1141         use crate::traits::WhereClause::*;
1142
1143         mem::discriminant(self).hash_stable(hcx, hasher);
1144         match self {
1145             Implemented(trait_ref) => trait_ref.hash_stable(hcx, hasher),
1146             ProjectionEq(projection) => projection.hash_stable(hcx, hasher),
1147             TypeOutlives(ty_outlives) => ty_outlives.hash_stable(hcx, hasher),
1148             RegionOutlives(region_outlives) => region_outlives.hash_stable(hcx, hasher),
1149         }
1150     }
1151 }
1152
1153 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::WellFormed<'tcx> {
1154     fn hash_stable<W: StableHasherResult>(&self,
1155                                           hcx: &mut StableHashingContext<'a>,
1156                                           hasher: &mut StableHasher<W>) {
1157         use crate::traits::WellFormed::*;
1158
1159         mem::discriminant(self).hash_stable(hcx, hasher);
1160         match self {
1161             Trait(trait_ref) => trait_ref.hash_stable(hcx, hasher),
1162             Ty(ty) => ty.hash_stable(hcx, hasher),
1163         }
1164     }
1165 }
1166
1167 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::FromEnv<'tcx> {
1168     fn hash_stable<W: StableHasherResult>(&self,
1169                                           hcx: &mut StableHashingContext<'a>,
1170                                           hasher: &mut StableHasher<W>) {
1171         use crate::traits::FromEnv::*;
1172
1173         mem::discriminant(self).hash_stable(hcx, hasher);
1174         match self {
1175             Trait(trait_ref) => trait_ref.hash_stable(hcx, hasher),
1176             Ty(ty) => ty.hash_stable(hcx, hasher),
1177         }
1178     }
1179 }
1180
1181 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::DomainGoal<'tcx> {
1182     fn hash_stable<W: StableHasherResult>(&self,
1183                                           hcx: &mut StableHashingContext<'a>,
1184                                           hasher: &mut StableHasher<W>) {
1185         use crate::traits::DomainGoal::*;
1186
1187         mem::discriminant(self).hash_stable(hcx, hasher);
1188         match self {
1189             Holds(wc) => wc.hash_stable(hcx, hasher),
1190             WellFormed(wf) => wf.hash_stable(hcx, hasher),
1191             FromEnv(from_env) => from_env.hash_stable(hcx, hasher),
1192             Normalize(projection) => projection.hash_stable(hcx, hasher),
1193         }
1194     }
1195 }
1196
1197 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::Goal<'tcx> {
1198     fn hash_stable<W: StableHasherResult>(&self,
1199                                           hcx: &mut StableHashingContext<'a>,
1200                                           hasher: &mut StableHasher<W>) {
1201         use crate::traits::GoalKind::*;
1202
1203         mem::discriminant(self).hash_stable(hcx, hasher);
1204         match self {
1205             Implies(hypotheses, goal) => {
1206                 hypotheses.hash_stable(hcx, hasher);
1207                 goal.hash_stable(hcx, hasher);
1208             },
1209             And(goal1, goal2) => {
1210                 goal1.hash_stable(hcx, hasher);
1211                 goal2.hash_stable(hcx, hasher);
1212             }
1213             Not(goal) => goal.hash_stable(hcx, hasher),
1214             DomainGoal(domain_goal) => domain_goal.hash_stable(hcx, hasher),
1215             Quantified(quantifier, goal) => {
1216                 quantifier.hash_stable(hcx, hasher);
1217                 goal.hash_stable(hcx, hasher);
1218             },
1219             Subtype(a, b) => {
1220                 a.hash_stable(hcx, hasher);
1221                 b.hash_stable(hcx, hasher);
1222             }
1223             CannotProve => { },
1224         }
1225     }
1226 }
1227
1228 impl_stable_hash_for!(
1229     struct traits::ProgramClause<'tcx> {
1230         goal, hypotheses, category
1231     }
1232 );
1233
1234 impl_stable_hash_for!(enum traits::ProgramClauseCategory {
1235     ImpliedBound,
1236     WellFormed,
1237     Other,
1238 });
1239
1240 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::Clause<'tcx> {
1241     fn hash_stable<W: StableHasherResult>(&self,
1242                                           hcx: &mut StableHashingContext<'a>,
1243                                           hasher: &mut StableHasher<W>) {
1244         use crate::traits::Clause::*;
1245
1246         mem::discriminant(self).hash_stable(hcx, hasher);
1247         match self {
1248             Implies(clause) => clause.hash_stable(hcx, hasher),
1249             ForAll(clause) => clause.hash_stable(hcx, hasher),
1250         }
1251     }
1252 }
1253
1254 impl_stable_hash_for!(enum traits::QuantifierKind {
1255     Universal,
1256     Existential
1257 });
1258
1259 impl_stable_hash_for!(struct ty::subst::UserSubsts<'tcx> { substs, user_self_ty });
1260
1261 impl_stable_hash_for!(struct ty::subst::UserSelfTy<'tcx> { impl_def_id, self_ty });
1262
1263 impl_stable_hash_for!(
1264     struct traits::Environment<'tcx> {
1265         clauses,
1266     }
1267 );
1268
1269 impl_stable_hash_for!(
1270     impl<'tcx, G> for struct traits::InEnvironment<'tcx, G> {
1271         environment,
1272         goal,
1273     }
1274 );
1275
1276 impl_stable_hash_for!(
1277     struct ty::CanonicalUserTypeAnnotation<'tcx> {
1278         user_ty, span, inferred_ty
1279     }
1280 );
1281
1282 impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::UserType<'gcx> {
1283     fn hash_stable<W: StableHasherResult>(&self,
1284                                           hcx: &mut StableHashingContext<'a>,
1285                                           hasher: &mut StableHasher<W>) {
1286         mem::discriminant(self).hash_stable(hcx, hasher);
1287         match *self {
1288             ty::UserType::Ty(ref ty) => {
1289                 ty.hash_stable(hcx, hasher);
1290             }
1291             ty::UserType::TypeOf(ref def_id, ref substs) => {
1292                 def_id.hash_stable(hcx, hasher);
1293                 substs.hash_stable(hcx, hasher);
1294             }
1295         }
1296     }
1297 }
1298
1299 impl<'a> HashStable<StableHashingContext<'a>> for ty::UserTypeAnnotationIndex {
1300     #[inline]
1301     fn hash_stable<W: StableHasherResult>(&self,
1302                                           hcx: &mut StableHashingContext<'a>,
1303                                           hasher: &mut StableHasher<W>) {
1304         self.index().hash_stable(hcx, hasher);
1305     }
1306 }