]> git.lizzy.rs Git - rust.git/blob - src/librustc/ich/impls_ty.rs
Rollup merge of #41135 - japaric:unstable-docs, r=steveklabnik
[rust.git] / src / librustc / ich / impls_ty.rs
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.
4 //
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.
10
11 //! This module contains `HashStable` implementations for various data types
12 //! from rustc::ty in no particular order.
13
14 use ich::StableHashingContext;
15 use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
16                                            StableHasherResult};
17 use std::hash as std_hash;
18 use std::mem;
19 use ty;
20
21
22 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::Ty<'tcx> {
23     fn hash_stable<W: StableHasherResult>(&self,
24                                           hcx: &mut StableHashingContext<'a, 'tcx>,
25                                           hasher: &mut StableHasher<W>) {
26         let type_hash = hcx.tcx().type_id_hash(*self);
27         type_hash.hash_stable(hcx, hasher);
28     }
29 }
30
31 impl_stable_hash_for!(struct ty::ItemSubsts<'tcx> { substs });
32
33 impl<'a, 'tcx, T> HashStable<StableHashingContext<'a, 'tcx>> for ty::Slice<T>
34     where T: HashStable<StableHashingContext<'a, 'tcx>> {
35     fn hash_stable<W: StableHasherResult>(&self,
36                                           hcx: &mut StableHashingContext<'a, 'tcx>,
37                                           hasher: &mut StableHasher<W>) {
38         (&**self).hash_stable(hcx, hasher);
39     }
40 }
41
42 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::subst::Kind<'tcx> {
43     fn hash_stable<W: StableHasherResult>(&self,
44                                           hcx: &mut StableHashingContext<'a, 'tcx>,
45                                           hasher: &mut StableHasher<W>) {
46         self.as_type().hash_stable(hcx, hasher);
47         self.as_region().hash_stable(hcx, hasher);
48     }
49 }
50
51 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::Region {
52     fn hash_stable<W: StableHasherResult>(&self,
53                                           hcx: &mut StableHashingContext<'a, 'tcx>,
54                                           hasher: &mut StableHasher<W>) {
55         mem::discriminant(self).hash_stable(hcx, hasher);
56         match *self {
57             ty::ReErased |
58             ty::ReStatic |
59             ty::ReEmpty => {
60                 // No variant fields to hash for these ...
61             }
62             ty::ReLateBound(db, ty::BrAnon(i)) => {
63                 db.depth.hash_stable(hcx, hasher);
64                 i.hash_stable(hcx, hasher);
65             }
66             ty::ReEarlyBound(ty::EarlyBoundRegion { index, name }) => {
67                 index.hash_stable(hcx, hasher);
68                 name.hash_stable(hcx, hasher);
69             }
70             ty::ReLateBound(..) |
71             ty::ReFree(..) |
72             ty::ReScope(..) |
73             ty::ReVar(..) |
74             ty::ReSkolemized(..) => {
75                 bug!("TypeIdHasher: unexpected region {:?}", *self)
76             }
77         }
78     }
79 }
80
81 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::adjustment::AutoBorrow<'tcx> {
82     fn hash_stable<W: StableHasherResult>(&self,
83                                           hcx: &mut StableHashingContext<'a, 'tcx>,
84                                           hasher: &mut StableHasher<W>) {
85         mem::discriminant(self).hash_stable(hcx, hasher);
86         match *self {
87             ty::adjustment::AutoBorrow::Ref(ref region, mutability) => {
88                 region.hash_stable(hcx, hasher);
89                 mutability.hash_stable(hcx, hasher);
90             }
91             ty::adjustment::AutoBorrow::RawPtr(mutability) => {
92                 mutability.hash_stable(hcx, hasher);
93             }
94         }
95     }
96 }
97
98 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::adjustment::Adjust<'tcx> {
99     fn hash_stable<W: StableHasherResult>(&self,
100                                           hcx: &mut StableHashingContext<'a, 'tcx>,
101                                           hasher: &mut StableHasher<W>) {
102         mem::discriminant(self).hash_stable(hcx, hasher);
103         match *self {
104             ty::adjustment::Adjust::NeverToAny |
105             ty::adjustment::Adjust::ReifyFnPointer |
106             ty::adjustment::Adjust::UnsafeFnPointer |
107             ty::adjustment::Adjust::ClosureFnPointer |
108             ty::adjustment::Adjust::MutToConstPointer => {}
109             ty::adjustment::Adjust::DerefRef { autoderefs, ref autoref, unsize } => {
110                 autoderefs.hash_stable(hcx, hasher);
111                 autoref.hash_stable(hcx, hasher);
112                 unsize.hash_stable(hcx, hasher);
113             }
114         }
115     }
116 }
117
118 impl_stable_hash_for!(struct ty::adjustment::Adjustment<'tcx> { kind, target });
119 impl_stable_hash_for!(struct ty::MethodCall { expr_id, autoderef });
120 impl_stable_hash_for!(struct ty::MethodCallee<'tcx> { def_id, ty, substs });
121 impl_stable_hash_for!(struct ty::UpvarId { var_id, closure_expr_id });
122 impl_stable_hash_for!(struct ty::UpvarBorrow<'tcx> { kind, region });
123
124 impl_stable_hash_for!(enum ty::BorrowKind {
125     ImmBorrow,
126     UniqueImmBorrow,
127     MutBorrow
128 });
129
130
131 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::UpvarCapture<'tcx> {
132     fn hash_stable<W: StableHasherResult>(&self,
133                                           hcx: &mut StableHashingContext<'a, 'tcx>,
134                                           hasher: &mut StableHasher<W>) {
135         mem::discriminant(self).hash_stable(hcx, hasher);
136         match *self {
137             ty::UpvarCapture::ByValue => {}
138             ty::UpvarCapture::ByRef(ref up_var_borrow) => {
139                 up_var_borrow.hash_stable(hcx, hasher);
140             }
141         }
142     }
143 }
144
145 impl_stable_hash_for!(struct ty::FnSig<'tcx> {
146     inputs_and_output,
147     variadic,
148     unsafety,
149     abi
150 });
151
152 impl<'a, 'tcx, T> HashStable<StableHashingContext<'a, 'tcx>> for ty::Binder<T>
153     where T: HashStable<StableHashingContext<'a, 'tcx>> + ty::fold::TypeFoldable<'tcx>
154 {
155     fn hash_stable<W: StableHasherResult>(&self,
156                                           hcx: &mut StableHashingContext<'a, 'tcx>,
157                                           hasher: &mut StableHasher<W>) {
158         hcx.tcx().anonymize_late_bound_regions(self).0.hash_stable(hcx, hasher);
159     }
160 }
161
162 impl_stable_hash_for!(enum ty::ClosureKind { Fn, FnMut, FnOnce });
163
164 impl_stable_hash_for!(enum ty::Visibility {
165     Public,
166     Restricted(def_id),
167     Invisible
168 });
169
170 impl_stable_hash_for!(struct ty::TraitRef<'tcx> { def_id, substs });
171 impl_stable_hash_for!(struct ty::TraitPredicate<'tcx> { trait_ref });
172 impl_stable_hash_for!(tuple_struct ty::EquatePredicate<'tcx> { t1, t2 });
173
174 impl<'a, 'tcx, A, B> HashStable<StableHashingContext<'a, 'tcx>> for ty::OutlivesPredicate<A, B>
175     where A: HashStable<StableHashingContext<'a, 'tcx>>,
176           B: HashStable<StableHashingContext<'a, 'tcx>>,
177 {
178     fn hash_stable<W: StableHasherResult>(&self,
179                                           hcx: &mut StableHashingContext<'a, 'tcx>,
180                                           hasher: &mut StableHasher<W>) {
181         let ty::OutlivesPredicate(ref a, ref b) = *self;
182         a.hash_stable(hcx, hasher);
183         b.hash_stable(hcx, hasher);
184     }
185 }
186
187 impl_stable_hash_for!(struct ty::ProjectionPredicate<'tcx> { projection_ty, ty });
188 impl_stable_hash_for!(struct ty::ProjectionTy<'tcx> { trait_ref, item_name });
189
190
191 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::Predicate<'tcx> {
192     fn hash_stable<W: StableHasherResult>(&self,
193                                           hcx: &mut StableHashingContext<'a, 'tcx>,
194                                           hasher: &mut StableHasher<W>) {
195         mem::discriminant(self).hash_stable(hcx, hasher);
196         match *self {
197             ty::Predicate::Trait(ref pred) => {
198                 pred.hash_stable(hcx, hasher);
199             }
200             ty::Predicate::Equate(ref pred) => {
201                 pred.hash_stable(hcx, hasher);
202             }
203             ty::Predicate::RegionOutlives(ref pred) => {
204                 pred.hash_stable(hcx, hasher);
205             }
206             ty::Predicate::TypeOutlives(ref pred) => {
207                 pred.hash_stable(hcx, hasher);
208             }
209             ty::Predicate::Projection(ref pred) => {
210                 pred.hash_stable(hcx, hasher);
211             }
212             ty::Predicate::WellFormed(ty) => {
213                 ty.hash_stable(hcx, hasher);
214             }
215             ty::Predicate::ObjectSafe(def_id) => {
216                 def_id.hash_stable(hcx, hasher);
217             }
218             ty::Predicate::ClosureKind(def_id, closure_kind) => {
219                 def_id.hash_stable(hcx, hasher);
220                 closure_kind.hash_stable(hcx, hasher);
221             }
222         }
223     }
224 }
225
226
227 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::AdtFlags {
228     fn hash_stable<W: StableHasherResult>(&self,
229                                           _: &mut StableHashingContext<'a, 'tcx>,
230                                           hasher: &mut StableHasher<W>) {
231         std_hash::Hash::hash(self, hasher);
232     }
233 }
234
235 impl_stable_hash_for!(struct ty::VariantDef {
236     did,
237     name,
238     discr,
239     fields,
240     ctor_kind
241 });
242
243 impl_stable_hash_for!(enum ty::VariantDiscr {
244     Explicit(def_id),
245     Relative(distance)
246 });
247
248 impl_stable_hash_for!(struct ty::FieldDef {
249     did,
250     name,
251     vis
252 });
253
254 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>>
255 for ::middle::const_val::ConstVal<'tcx> {
256     fn hash_stable<W: StableHasherResult>(&self,
257                                           hcx: &mut StableHashingContext<'a, 'tcx>,
258                                           hasher: &mut StableHasher<W>) {
259         use middle::const_val::ConstVal;
260
261         mem::discriminant(self).hash_stable(hcx, hasher);
262
263         match *self {
264             ConstVal::Float(ref value) => {
265                 value.hash_stable(hcx, hasher);
266             }
267             ConstVal::Integral(ref value) => {
268                 value.hash_stable(hcx, hasher);
269             }
270             ConstVal::Str(ref value) => {
271                 value.hash_stable(hcx, hasher);
272             }
273             ConstVal::ByteStr(ref value) => {
274                 value.hash_stable(hcx, hasher);
275             }
276             ConstVal::Bool(value) => {
277                 value.hash_stable(hcx, hasher);
278             }
279             ConstVal::Function(def_id, substs) => {
280                 def_id.hash_stable(hcx, hasher);
281                 substs.hash_stable(hcx, hasher);
282             }
283             ConstVal::Struct(ref _name_value_map) => {
284                 // BTreeMap<ast::Name, ConstVal<'tcx>>),
285                 panic!("Ordering still unstable")
286             }
287             ConstVal::Tuple(ref value) => {
288                 value.hash_stable(hcx, hasher);
289             }
290             ConstVal::Array(ref value) => {
291                 value.hash_stable(hcx, hasher);
292             }
293             ConstVal::Repeat(ref value, times) => {
294                 value.hash_stable(hcx, hasher);
295                 times.hash_stable(hcx, hasher);
296             }
297             ConstVal::Char(value) => {
298                 value.hash_stable(hcx, hasher);
299             }
300         }
301     }
302 }
303
304 impl_stable_hash_for!(struct ty::ClosureSubsts<'tcx> { substs });
305
306
307 impl_stable_hash_for!(struct ty::GenericPredicates<'tcx> {
308     parent,
309     predicates
310 });
311
312 impl_stable_hash_for!(enum ty::Variance {
313     Covariant,
314     Invariant,
315     Contravariant,
316     Bivariant
317 });
318
319 impl_stable_hash_for!(enum ty::adjustment::CustomCoerceUnsized {
320     Struct(index)
321 });
322
323 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::Generics {
324     fn hash_stable<W: StableHasherResult>(&self,
325                                           hcx: &mut StableHashingContext<'a, 'tcx>,
326                                           hasher: &mut StableHasher<W>) {
327         let ty::Generics {
328             parent,
329             parent_regions,
330             parent_types,
331             ref regions,
332             ref types,
333
334             // Reverse map to each `TypeParameterDef`'s `index` field, from
335             // `def_id.index` (`def_id.krate` is the same as the item's).
336             type_param_to_index: _, // Don't hash this
337             has_self,
338         } = *self;
339
340         parent.hash_stable(hcx, hasher);
341         parent_regions.hash_stable(hcx, hasher);
342         parent_types.hash_stable(hcx, hasher);
343         regions.hash_stable(hcx, hasher);
344         types.hash_stable(hcx, hasher);
345         has_self.hash_stable(hcx, hasher);
346     }
347 }
348
349 impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::RegionParameterDef {
350     fn hash_stable<W: StableHasherResult>(&self,
351                                           hcx: &mut StableHashingContext<'a, 'tcx>,
352                                           hasher: &mut StableHasher<W>) {
353         let ty::RegionParameterDef {
354             name,
355             def_id,
356             index,
357             issue_32330: _,
358             pure_wrt_drop
359         } = *self;
360
361         name.hash_stable(hcx, hasher);
362         def_id.hash_stable(hcx, hasher);
363         index.hash_stable(hcx, hasher);
364         pure_wrt_drop.hash_stable(hcx, hasher);
365     }
366 }
367
368 impl_stable_hash_for!(struct ty::TypeParameterDef {
369     name,
370     def_id,
371     index,
372     has_default,
373     object_lifetime_default,
374     pure_wrt_drop
375 });
376
377
378 impl<'a, 'tcx, T> HashStable<StableHashingContext<'a, 'tcx>>
379 for ::middle::resolve_lifetime::Set1<T>
380     where T: HashStable<StableHashingContext<'a, 'tcx>>
381 {
382     fn hash_stable<W: StableHasherResult>(&self,
383                                           hcx: &mut StableHashingContext<'a, 'tcx>,
384                                           hasher: &mut StableHasher<W>) {
385         use middle::resolve_lifetime::Set1;
386
387         mem::discriminant(self).hash_stable(hcx, hasher);
388         match *self {
389             Set1::Empty |
390             Set1::Many => {
391                 // Nothing to do.
392             }
393             Set1::One(ref value) => {
394                 value.hash_stable(hcx, hasher);
395             }
396         }
397     }
398 }
399
400 impl_stable_hash_for!(enum ::middle::resolve_lifetime::Region {
401     Static,
402     EarlyBound(index, decl),
403     LateBound(db_index, decl),
404     LateBoundAnon(db_index, anon_index),
405     Free(call_site_scope_data, decl)
406 });
407
408 impl_stable_hash_for!(struct ::middle::region::CallSiteScopeData {
409     fn_id,
410     body_id
411 });
412
413 impl_stable_hash_for!(struct ty::DebruijnIndex {
414     depth
415 });