]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/structural_impls.rs
Rollup merge of #41249 - GuillaumeGomez:rustdoc-render, r=steveklabnik,frewsxcv
[rust.git] / src / librustc / ty / structural_impls.rs
1 // Copyright 2012-2015 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 use infer::type_variable;
12 use ty::{self, Lift, Ty, TyCtxt};
13 use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
14 use rustc_data_structures::accumulate_vec::AccumulateVec;
15 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
16
17 use std::rc::Rc;
18 use syntax::abi;
19
20 use hir;
21
22 ///////////////////////////////////////////////////////////////////////////
23 // Lift implementations
24
25 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
26     type Lifted = (A::Lifted, B::Lifted);
27     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
28         tcx.lift(&self.0).and_then(|a| tcx.lift(&self.1).map(|b| (a, b)))
29     }
30 }
31
32 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
33     type Lifted = Option<T::Lifted>;
34     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
35         match *self {
36             Some(ref x) => tcx.lift(x).map(Some),
37             None => Some(None)
38         }
39     }
40 }
41
42 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
43     type Lifted = Result<T::Lifted, E::Lifted>;
44     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
45         match *self {
46             Ok(ref x) => tcx.lift(x).map(Ok),
47             Err(ref e) => tcx.lift(e).map(Err)
48         }
49     }
50 }
51
52 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for [T] {
53     type Lifted = Vec<T::Lifted>;
54     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
55         // type annotation needed to inform `projection_must_outlive`
56         let mut result : Vec<<T as Lift<'tcx>>::Lifted>
57             = Vec::with_capacity(self.len());
58         for x in self {
59             if let Some(value) = tcx.lift(x) {
60                 result.push(value);
61             } else {
62                 return None;
63             }
64         }
65         Some(result)
66     }
67 }
68
69 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
70     type Lifted = Vec<T::Lifted>;
71     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
72         tcx.lift(&self[..])
73     }
74 }
75
76 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
77     type Lifted = ty::TraitRef<'tcx>;
78     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
79         tcx.lift(&self.substs).map(|substs| ty::TraitRef {
80             def_id: self.def_id,
81             substs: substs
82         })
83     }
84 }
85
86 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
87     type Lifted = ty::ExistentialTraitRef<'tcx>;
88     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
89         tcx.lift(&self.substs).map(|substs| ty::ExistentialTraitRef {
90             def_id: self.def_id,
91             substs: substs
92         })
93     }
94 }
95
96 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
97     type Lifted = ty::TraitPredicate<'tcx>;
98     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
99                              -> Option<ty::TraitPredicate<'tcx>> {
100         tcx.lift(&self.trait_ref).map(|trait_ref| ty::TraitPredicate {
101             trait_ref: trait_ref
102         })
103     }
104 }
105
106 impl<'a, 'tcx> Lift<'tcx> for ty::EquatePredicate<'a> {
107     type Lifted = ty::EquatePredicate<'tcx>;
108     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
109                              -> Option<ty::EquatePredicate<'tcx>> {
110         tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::EquatePredicate(a, b))
111     }
112 }
113
114 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
115     type Lifted = ty::SubtypePredicate<'tcx>;
116     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
117                              -> Option<ty::SubtypePredicate<'tcx>> {
118         tcx.lift(&(self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
119             a_is_expected: self.a_is_expected,
120             a: a,
121             b: b,
122         })
123     }
124 }
125
126 impl<'tcx, A: Copy+Lift<'tcx>, B: Copy+Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
127     type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
128     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
129         tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
130     }
131 }
132
133 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
134     type Lifted = ty::ProjectionTy<'tcx>;
135     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
136                              -> Option<ty::ProjectionTy<'tcx>> {
137         tcx.lift(&self.trait_ref).map(|trait_ref| {
138             ty::ProjectionTy {
139                 trait_ref: trait_ref,
140                 item_name: self.item_name
141             }
142         })
143     }
144 }
145
146 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
147     type Lifted = ty::ProjectionPredicate<'tcx>;
148     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
149                              -> Option<ty::ProjectionPredicate<'tcx>> {
150         tcx.lift(&(self.projection_ty, self.ty)).map(|(projection_ty, ty)| {
151             ty::ProjectionPredicate {
152                 projection_ty: projection_ty,
153                 ty: ty
154             }
155         })
156     }
157 }
158
159 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
160     type Lifted = ty::ExistentialProjection<'tcx>;
161     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
162         tcx.lift(&(self.trait_ref, self.ty)).map(|(trait_ref, ty)| {
163             ty::ExistentialProjection {
164                 trait_ref: trait_ref,
165                 item_name: self.item_name,
166                 ty: ty
167             }
168         })
169     }
170 }
171
172 impl<'a, 'tcx> Lift<'tcx> for ty::Predicate<'a> {
173     type Lifted = ty::Predicate<'tcx>;
174     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
175         match *self {
176             ty::Predicate::Trait(ref binder) => {
177                 tcx.lift(binder).map(ty::Predicate::Trait)
178             }
179             ty::Predicate::Equate(ref binder) => {
180                 tcx.lift(binder).map(ty::Predicate::Equate)
181             }
182             ty::Predicate::Subtype(ref binder) => {
183                 tcx.lift(binder).map(ty::Predicate::Subtype)
184             }
185             ty::Predicate::RegionOutlives(ref binder) => {
186                 tcx.lift(binder).map(ty::Predicate::RegionOutlives)
187             }
188             ty::Predicate::TypeOutlives(ref binder) => {
189                 tcx.lift(binder).map(ty::Predicate::TypeOutlives)
190             }
191             ty::Predicate::Projection(ref binder) => {
192                 tcx.lift(binder).map(ty::Predicate::Projection)
193             }
194             ty::Predicate::WellFormed(ty) => {
195                 tcx.lift(&ty).map(ty::Predicate::WellFormed)
196             }
197             ty::Predicate::ClosureKind(closure_def_id, kind) => {
198                 Some(ty::Predicate::ClosureKind(closure_def_id, kind))
199             }
200             ty::Predicate::ObjectSafe(trait_def_id) => {
201                 Some(ty::Predicate::ObjectSafe(trait_def_id))
202             }
203         }
204     }
205 }
206
207 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
208     type Lifted = ty::Binder<T::Lifted>;
209     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
210         tcx.lift(&self.0).map(|x| ty::Binder(x))
211     }
212 }
213
214 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
215     type Lifted = ty::ClosureSubsts<'tcx>;
216     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
217         tcx.lift(&self.substs).map(|substs| {
218             ty::ClosureSubsts { substs: substs }
219         })
220     }
221 }
222
223 impl<'a, 'tcx> Lift<'tcx> for ty::ItemSubsts<'a> {
224     type Lifted = ty::ItemSubsts<'tcx>;
225     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
226         tcx.lift(&self.substs).map(|substs| {
227             ty::ItemSubsts {
228                 substs: substs
229             }
230         })
231     }
232 }
233
234 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
235     type Lifted = ty::adjustment::AutoBorrow<'tcx>;
236     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
237         match *self {
238             ty::adjustment::AutoBorrow::Ref(r, m) => {
239                 tcx.lift(&r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
240             }
241             ty::adjustment::AutoBorrow::RawPtr(m) => {
242                 Some(ty::adjustment::AutoBorrow::RawPtr(m))
243             }
244         }
245     }
246 }
247
248 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
249     type Lifted = ty::FnSig<'tcx>;
250     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
251         tcx.lift(&self.inputs_and_output).map(|x| {
252             ty::FnSig {
253                 inputs_and_output: x,
254                 variadic: self.variadic,
255                 unsafety: self.unsafety,
256                 abi: self.abi,
257             }
258         })
259     }
260 }
261
262 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
263     type Lifted = ty::error::ExpectedFound<T::Lifted>;
264     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
265         tcx.lift(&self.expected).and_then(|expected| {
266             tcx.lift(&self.found).map(|found| {
267                 ty::error::ExpectedFound {
268                     expected: expected,
269                     found: found
270                 }
271             })
272         })
273     }
274 }
275
276 impl<'a, 'tcx> Lift<'tcx> for type_variable::Default<'a> {
277     type Lifted = type_variable::Default<'tcx>;
278     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
279         tcx.lift(&self.ty).map(|ty| {
280             type_variable::Default {
281                 ty: ty,
282                 origin_span: self.origin_span,
283                 def_id: self.def_id
284             }
285         })
286     }
287 }
288
289 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
290     type Lifted = ty::error::TypeError<'tcx>;
291     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
292         use ty::error::TypeError::*;
293
294         Some(match *self {
295             Mismatch => Mismatch,
296             UnsafetyMismatch(x) => UnsafetyMismatch(x),
297             AbiMismatch(x) => AbiMismatch(x),
298             Mutability => Mutability,
299             TupleSize(x) => TupleSize(x),
300             FixedArraySize(x) => FixedArraySize(x),
301             ArgCount => ArgCount,
302             RegionsDoesNotOutlive(a, b) => {
303                 return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b))
304             }
305             RegionsNotSame(a, b) => {
306                 return tcx.lift(&(a, b)).map(|(a, b)| RegionsNotSame(a, b))
307             }
308             RegionsNoOverlap(a, b) => {
309                 return tcx.lift(&(a, b)).map(|(a, b)| RegionsNoOverlap(a, b))
310             }
311             RegionsInsufficientlyPolymorphic(a, b, ref c) => {
312                 let c = c.clone();
313                 return tcx.lift(&b).map(|b| RegionsInsufficientlyPolymorphic(a, b, c))
314             }
315             RegionsOverlyPolymorphic(a, b, ref c) => {
316                 let c = c.clone();
317                 return tcx.lift(&b).map(|b| RegionsOverlyPolymorphic(a, b, c))
318             }
319             IntMismatch(x) => IntMismatch(x),
320             FloatMismatch(x) => FloatMismatch(x),
321             Traits(x) => Traits(x),
322             VariadicMismatch(x) => VariadicMismatch(x),
323             CyclicTy => CyclicTy,
324             ProjectionNameMismatched(x) => ProjectionNameMismatched(x),
325             ProjectionBoundsLength(x) => ProjectionBoundsLength(x),
326
327             Sorts(ref x) => return tcx.lift(x).map(Sorts),
328             TyParamDefaultMismatch(ref x) => {
329                 return tcx.lift(x).map(TyParamDefaultMismatch)
330             }
331             ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch)
332         })
333     }
334 }
335
336 ///////////////////////////////////////////////////////////////////////////
337 // TypeFoldable implementations.
338 //
339 // Ideally, each type should invoke `folder.fold_foo(self)` and
340 // nothing else. In some cases, though, we haven't gotten around to
341 // adding methods on the `folder` yet, and thus the folding is
342 // hard-coded here. This is less-flexible, because folders cannot
343 // override the behavior, but there are a lot of random types and one
344 // can easily refactor the folding into the TypeFolder trait as
345 // needed.
346
347 macro_rules! CopyImpls {
348     ($($ty:ty),+) => {
349         $(
350             impl<'tcx> TypeFoldable<'tcx> for $ty {
351                 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _: &mut F) -> $ty {
352                     *self
353                 }
354
355                 fn super_visit_with<F: TypeVisitor<'tcx>>(&self, _: &mut F) -> bool {
356                     false
357                 }
358             }
359         )+
360     }
361 }
362
363 CopyImpls! { (), hir::Unsafety, abi::Abi }
364
365 impl<'tcx, T:TypeFoldable<'tcx>, U:TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
366     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> (T, U) {
367         (self.0.fold_with(folder), self.1.fold_with(folder))
368     }
369
370     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
371         self.0.visit_with(visitor) || self.1.visit_with(visitor)
372     }
373 }
374
375 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Option<T> {
376     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
377         self.as_ref().map(|t| t.fold_with(folder))
378     }
379
380     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
381         self.iter().any(|t| t.visit_with(visitor))
382     }
383 }
384
385 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
386     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
387         Rc::new((**self).fold_with(folder))
388     }
389
390     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
391         (**self).visit_with(visitor)
392     }
393 }
394
395 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
396     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
397         let content: T = (**self).fold_with(folder);
398         box content
399     }
400
401     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
402         (**self).visit_with(visitor)
403     }
404 }
405
406 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
407     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
408         self.iter().map(|t| t.fold_with(folder)).collect()
409     }
410
411     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
412         self.iter().any(|t| t.visit_with(visitor))
413     }
414 }
415
416 impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
417     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
418         ty::Binder(self.0.fold_with(folder))
419     }
420
421     fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
422         folder.fold_binder(self)
423     }
424
425     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
426         self.0.visit_with(visitor)
427     }
428
429     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
430         visitor.visit_binder(self)
431     }
432 }
433
434 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Slice<ty::ExistentialPredicate<'tcx>> {
435     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
436         let v = self.iter().map(|p| p.fold_with(folder)).collect::<AccumulateVec<[_; 8]>>();
437         folder.tcx().intern_existential_predicates(&v)
438     }
439
440     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
441         self.iter().any(|p| p.visit_with(visitor))
442     }
443 }
444
445 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialPredicate<'tcx> {
446     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self  {
447         use ty::ExistentialPredicate::*;
448         match *self {
449             Trait(ref tr) => Trait(tr.fold_with(folder)),
450             Projection(ref p) => Projection(p.fold_with(folder)),
451             AutoTrait(did) => AutoTrait(did),
452         }
453     }
454
455     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
456         match *self {
457             ty::ExistentialPredicate::Trait(ref tr) => tr.visit_with(visitor),
458             ty::ExistentialPredicate::Projection(ref p) => p.visit_with(visitor),
459             ty::ExistentialPredicate::AutoTrait(_) => false,
460         }
461     }
462 }
463
464 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Slice<Ty<'tcx>> {
465     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
466         let v = self.iter().map(|t| t.fold_with(folder)).collect::<AccumulateVec<[_; 8]>>();
467         folder.tcx().intern_type_list(&v)
468     }
469
470     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
471         self.iter().any(|t| t.visit_with(visitor))
472     }
473 }
474
475 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
476     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
477         let sty = match self.sty {
478             ty::TyRawPtr(tm) => ty::TyRawPtr(tm.fold_with(folder)),
479             ty::TyArray(typ, sz) => ty::TyArray(typ.fold_with(folder), sz),
480             ty::TySlice(typ) => ty::TySlice(typ.fold_with(folder)),
481             ty::TyAdt(tid, substs) => ty::TyAdt(tid, substs.fold_with(folder)),
482             ty::TyDynamic(ref trait_ty, ref region) =>
483                 ty::TyDynamic(trait_ty.fold_with(folder), region.fold_with(folder)),
484             ty::TyTuple(ts, defaulted) => ty::TyTuple(ts.fold_with(folder), defaulted),
485             ty::TyFnDef(def_id, substs, f) => {
486                 ty::TyFnDef(def_id,
487                             substs.fold_with(folder),
488                             f.fold_with(folder))
489             }
490             ty::TyFnPtr(f) => ty::TyFnPtr(f.fold_with(folder)),
491             ty::TyRef(ref r, tm) => {
492                 ty::TyRef(r.fold_with(folder), tm.fold_with(folder))
493             }
494             ty::TyClosure(did, substs) => ty::TyClosure(did, substs.fold_with(folder)),
495             ty::TyProjection(ref data) => ty::TyProjection(data.fold_with(folder)),
496             ty::TyAnon(did, substs) => ty::TyAnon(did, substs.fold_with(folder)),
497             ty::TyBool | ty::TyChar | ty::TyStr | ty::TyInt(_) |
498             ty::TyUint(_) | ty::TyFloat(_) | ty::TyError | ty::TyInfer(_) |
499             ty::TyParam(..) | ty::TyNever => return self
500         };
501
502         if self.sty == sty {
503             self
504         } else {
505             folder.tcx().mk_ty(sty)
506         }
507     }
508
509     fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
510         folder.fold_ty(*self)
511     }
512
513     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
514         match self.sty {
515             ty::TyRawPtr(ref tm) => tm.visit_with(visitor),
516             ty::TyArray(typ, _sz) => typ.visit_with(visitor),
517             ty::TySlice(typ) => typ.visit_with(visitor),
518             ty::TyAdt(_, substs) => substs.visit_with(visitor),
519             ty::TyDynamic(ref trait_ty, ref reg) =>
520                 trait_ty.visit_with(visitor) || reg.visit_with(visitor),
521             ty::TyTuple(ts, _) => ts.visit_with(visitor),
522             ty::TyFnDef(_, substs, ref f) => {
523                 substs.visit_with(visitor) || f.visit_with(visitor)
524             }
525             ty::TyFnPtr(ref f) => f.visit_with(visitor),
526             ty::TyRef(r, ref tm) => r.visit_with(visitor) || tm.visit_with(visitor),
527             ty::TyClosure(_did, ref substs) => substs.visit_with(visitor),
528             ty::TyProjection(ref data) => data.visit_with(visitor),
529             ty::TyAnon(_, ref substs) => substs.visit_with(visitor),
530             ty::TyBool | ty::TyChar | ty::TyStr | ty::TyInt(_) |
531             ty::TyUint(_) | ty::TyFloat(_) | ty::TyError | ty::TyInfer(_) |
532             ty::TyParam(..) | ty::TyNever => false,
533         }
534     }
535
536     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
537         visitor.visit_ty(self)
538     }
539 }
540
541 impl<'tcx> TypeFoldable<'tcx> for ty::TypeAndMut<'tcx> {
542     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
543         ty::TypeAndMut { ty: self.ty.fold_with(folder), mutbl: self.mutbl }
544     }
545
546     fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
547         folder.fold_mt(self)
548     }
549
550     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
551         self.ty.visit_with(visitor)
552     }
553 }
554
555 impl<'tcx> TypeFoldable<'tcx> for ty::FnSig<'tcx> {
556     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
557         let inputs_and_output = self.inputs_and_output.fold_with(folder);
558         ty::FnSig {
559             inputs_and_output: folder.tcx().intern_type_list(&inputs_and_output),
560             variadic: self.variadic,
561             unsafety: self.unsafety,
562             abi: self.abi,
563         }
564     }
565
566     fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
567         folder.fold_fn_sig(self)
568     }
569
570     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
571         self.inputs().iter().any(|i| i.visit_with(visitor)) ||
572         self.output().visit_with(visitor)
573     }
574 }
575
576 impl<'tcx> TypeFoldable<'tcx> for ty::TraitRef<'tcx> {
577     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
578         ty::TraitRef {
579             def_id: self.def_id,
580             substs: self.substs.fold_with(folder),
581         }
582     }
583
584     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
585         self.substs.visit_with(visitor)
586     }
587
588     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
589         visitor.visit_trait_ref(*self)
590     }
591 }
592
593 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialTraitRef<'tcx> {
594     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
595         ty::ExistentialTraitRef {
596             def_id: self.def_id,
597             substs: self.substs.fold_with(folder),
598         }
599     }
600
601     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
602         self.substs.visit_with(visitor)
603     }
604 }
605
606 impl<'tcx> TypeFoldable<'tcx> for ty::ImplHeader<'tcx> {
607     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
608         ty::ImplHeader {
609             impl_def_id: self.impl_def_id,
610             self_ty: self.self_ty.fold_with(folder),
611             trait_ref: self.trait_ref.map(|t| t.fold_with(folder)),
612             predicates: self.predicates.iter().map(|p| p.fold_with(folder)).collect(),
613         }
614     }
615
616     fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
617         folder.fold_impl_header(self)
618     }
619
620     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
621         self.self_ty.visit_with(visitor) ||
622             self.trait_ref.map(|r| r.visit_with(visitor)).unwrap_or(false) ||
623             self.predicates.iter().any(|p| p.visit_with(visitor))
624     }
625 }
626
627 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Region {
628     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
629         *self
630     }
631
632     fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
633         folder.fold_region(*self)
634     }
635
636     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
637         false
638     }
639
640     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
641         visitor.visit_region(*self)
642     }
643 }
644
645 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureSubsts<'tcx> {
646     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
647         ty::ClosureSubsts {
648             substs: self.substs.fold_with(folder),
649         }
650     }
651
652     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
653         self.substs.visit_with(visitor)
654     }
655 }
656
657 impl<'tcx> TypeFoldable<'tcx> for ty::ItemSubsts<'tcx> {
658     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
659         ty::ItemSubsts {
660             substs: self.substs.fold_with(folder),
661         }
662     }
663
664     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
665         self.substs.visit_with(visitor)
666     }
667 }
668
669 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::AutoBorrow<'tcx> {
670     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
671         match *self {
672             ty::adjustment::AutoBorrow::Ref(ref r, m) => {
673                 ty::adjustment::AutoBorrow::Ref(r.fold_with(folder), m)
674             }
675             ty::adjustment::AutoBorrow::RawPtr(m) => ty::adjustment::AutoBorrow::RawPtr(m)
676         }
677     }
678
679     fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
680         folder.fold_autoref(self)
681     }
682
683     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
684         match *self {
685             ty::adjustment::AutoBorrow::Ref(r, _m) => r.visit_with(visitor),
686             ty::adjustment::AutoBorrow::RawPtr(_m) => false,
687         }
688     }
689 }
690
691 impl<'tcx> TypeFoldable<'tcx> for ty::GenericPredicates<'tcx> {
692     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
693         ty::GenericPredicates {
694             parent: self.parent,
695             predicates: self.predicates.fold_with(folder),
696         }
697     }
698
699     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
700         self.predicates.visit_with(visitor)
701     }
702 }
703
704 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
705     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
706         match *self {
707             ty::Predicate::Trait(ref a) =>
708                 ty::Predicate::Trait(a.fold_with(folder)),
709             ty::Predicate::Equate(ref binder) =>
710                 ty::Predicate::Equate(binder.fold_with(folder)),
711             ty::Predicate::Subtype(ref binder) =>
712                 ty::Predicate::Subtype(binder.fold_with(folder)),
713             ty::Predicate::RegionOutlives(ref binder) =>
714                 ty::Predicate::RegionOutlives(binder.fold_with(folder)),
715             ty::Predicate::TypeOutlives(ref binder) =>
716                 ty::Predicate::TypeOutlives(binder.fold_with(folder)),
717             ty::Predicate::Projection(ref binder) =>
718                 ty::Predicate::Projection(binder.fold_with(folder)),
719             ty::Predicate::WellFormed(data) =>
720                 ty::Predicate::WellFormed(data.fold_with(folder)),
721             ty::Predicate::ClosureKind(closure_def_id, kind) =>
722                 ty::Predicate::ClosureKind(closure_def_id, kind),
723             ty::Predicate::ObjectSafe(trait_def_id) =>
724                 ty::Predicate::ObjectSafe(trait_def_id),
725         }
726     }
727
728     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
729         match *self {
730             ty::Predicate::Trait(ref a) => a.visit_with(visitor),
731             ty::Predicate::Equate(ref binder) => binder.visit_with(visitor),
732             ty::Predicate::Subtype(ref binder) => binder.visit_with(visitor),
733             ty::Predicate::RegionOutlives(ref binder) => binder.visit_with(visitor),
734             ty::Predicate::TypeOutlives(ref binder) => binder.visit_with(visitor),
735             ty::Predicate::Projection(ref binder) => binder.visit_with(visitor),
736             ty::Predicate::WellFormed(data) => data.visit_with(visitor),
737             ty::Predicate::ClosureKind(_closure_def_id, _kind) => false,
738             ty::Predicate::ObjectSafe(_trait_def_id) => false,
739         }
740     }
741 }
742
743 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionPredicate<'tcx> {
744     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
745         ty::ProjectionPredicate {
746             projection_ty: self.projection_ty.fold_with(folder),
747             ty: self.ty.fold_with(folder),
748         }
749     }
750
751     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
752         self.projection_ty.visit_with(visitor) || self.ty.visit_with(visitor)
753     }
754 }
755
756 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialProjection<'tcx> {
757     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
758         ty::ExistentialProjection {
759             trait_ref: self.trait_ref.fold_with(folder),
760             item_name: self.item_name,
761             ty: self.ty.fold_with(folder),
762         }
763     }
764
765     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
766         self.trait_ref.visit_with(visitor) || self.ty.visit_with(visitor)
767     }
768 }
769
770 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionTy<'tcx> {
771     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
772         ty::ProjectionTy {
773             trait_ref: self.trait_ref.fold_with(folder),
774             item_name: self.item_name,
775         }
776     }
777
778     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
779         self.trait_ref.visit_with(visitor)
780     }
781 }
782
783 impl<'tcx> TypeFoldable<'tcx> for ty::InstantiatedPredicates<'tcx> {
784     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
785         ty::InstantiatedPredicates {
786             predicates: self.predicates.fold_with(folder),
787         }
788     }
789
790     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
791         self.predicates.visit_with(visitor)
792     }
793 }
794
795 impl<'tcx> TypeFoldable<'tcx> for ty::EquatePredicate<'tcx> {
796     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
797         ty::EquatePredicate(self.0.fold_with(folder), self.1.fold_with(folder))
798     }
799
800     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
801         self.0.visit_with(visitor) || self.1.visit_with(visitor)
802     }
803 }
804
805 impl<'tcx> TypeFoldable<'tcx> for ty::SubtypePredicate<'tcx> {
806     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
807         ty::SubtypePredicate {
808             a_is_expected: self.a_is_expected,
809             a: self.a.fold_with(folder),
810             b: self.b.fold_with(folder)
811         }
812     }
813
814     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
815         self.a.visit_with(visitor) || self.b.visit_with(visitor)
816     }
817 }
818
819 impl<'tcx> TypeFoldable<'tcx> for ty::TraitPredicate<'tcx> {
820     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
821         ty::TraitPredicate {
822             trait_ref: self.trait_ref.fold_with(folder)
823         }
824     }
825
826     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
827         self.trait_ref.visit_with(visitor)
828     }
829 }
830
831 impl<'tcx,T,U> TypeFoldable<'tcx> for ty::OutlivesPredicate<T,U>
832     where T : TypeFoldable<'tcx>,
833           U : TypeFoldable<'tcx>,
834 {
835     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
836         ty::OutlivesPredicate(self.0.fold_with(folder),
837                               self.1.fold_with(folder))
838     }
839
840     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
841         self.0.visit_with(visitor) || self.1.visit_with(visitor)
842     }
843 }
844
845 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureUpvar<'tcx> {
846     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
847         ty::ClosureUpvar {
848             def: self.def,
849             span: self.span,
850             ty: self.ty.fold_with(folder),
851         }
852     }
853
854     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
855         self.ty.visit_with(visitor)
856     }
857 }
858
859 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::error::ExpectedFound<T> {
860     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
861         ty::error::ExpectedFound {
862             expected: self.expected.fold_with(folder),
863             found: self.found.fold_with(folder),
864         }
865     }
866
867     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
868         self.expected.visit_with(visitor) || self.found.visit_with(visitor)
869     }
870 }
871
872 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
873     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
874         self.iter().map(|x| x.fold_with(folder)).collect()
875     }
876
877     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
878         self.iter().any(|t| t.visit_with(visitor))
879     }
880 }