]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/structural_impls.rs
35d0359dbcfd255406490a2a3a12a7610c3ea2ba
[rust.git] / src / librustc / ty / structural_impls.rs
1 //! This module contains implements of the `Lift` and `TypeFoldable`
2 //! traits for various types in the Rust compiler. Most are written by
3 //! hand, though we've recently added some macros (e.g.,
4 //! `BraceStructLiftImpl!`) to help with the tedium.
5
6 use crate::hir::def::Namespace;
7 use crate::mir::ProjectionKind;
8 use crate::mir::interpret::ConstValue;
9 use crate::ty::{self, Lift, Ty, TyCtxt, ConstVid, InferConst};
10 use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
11 use crate::ty::print::{FmtPrinter, Printer};
12 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
13 use smallvec::SmallVec;
14 use crate::mir::interpret;
15
16 use std::fmt;
17 use std::marker::PhantomData;
18 use std::rc::Rc;
19
20 impl fmt::Debug for ty::GenericParamDef {
21     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22         let type_name = match self.kind {
23             ty::GenericParamDefKind::Lifetime => "Lifetime",
24             ty::GenericParamDefKind::Type {..} => "Type",
25             ty::GenericParamDefKind::Const => "Const",
26         };
27         write!(f, "{}({}, {:?}, {})",
28                type_name,
29                self.name,
30                self.def_id,
31                self.index)
32     }
33 }
34
35 impl fmt::Debug for ty::TraitDef {
36     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37         ty::tls::with(|tcx| {
38             FmtPrinter::new(tcx, f, Namespace::TypeNS)
39                 .print_def_path(self.def_id, &[])?;
40             Ok(())
41         })
42     }
43 }
44
45 impl fmt::Debug for ty::AdtDef {
46     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47         ty::tls::with(|tcx| {
48             FmtPrinter::new(tcx, f, Namespace::TypeNS)
49                 .print_def_path(self.did, &[])?;
50             Ok(())
51         })
52     }
53 }
54
55 impl fmt::Debug for ty::ClosureUpvar<'tcx> {
56     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57         write!(f, "ClosureUpvar({:?},{:?})",
58                self.res,
59                self.ty)
60     }
61 }
62
63 impl fmt::Debug for ty::UpvarId {
64     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65         let name = ty::tls::with(|tcx| {
66             tcx.hir().name_by_hir_id(self.var_path.hir_id)
67         });
68         write!(f, "UpvarId({:?};`{}`;{:?})",
69             self.var_path.hir_id,
70             name,
71             self.closure_expr_id)
72     }
73 }
74
75 impl fmt::Debug for ty::UpvarBorrow<'tcx> {
76     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77         write!(f, "UpvarBorrow({:?}, {:?})",
78                self.kind, self.region)
79     }
80 }
81
82 impl fmt::Debug for ty::ExistentialTraitRef<'tcx> {
83     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84         fmt::Display::fmt(self, f)
85     }
86 }
87
88 impl fmt::Debug for ty::adjustment::Adjustment<'tcx> {
89     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90         write!(f, "{:?} -> {}", self.kind, self.target)
91     }
92 }
93
94 impl fmt::Debug for ty::BoundRegion {
95     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96         match *self {
97             ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
98             ty::BrFresh(n) => write!(f, "BrFresh({:?})", n),
99             ty::BrNamed(did, name) => {
100                 write!(f, "BrNamed({:?}:{:?}, {})",
101                         did.krate, did.index, name)
102             }
103             ty::BrEnv => write!(f, "BrEnv"),
104         }
105     }
106 }
107
108 impl fmt::Debug for ty::RegionKind {
109     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110         match *self {
111             ty::ReEarlyBound(ref data) => {
112                 write!(f, "ReEarlyBound({}, {})",
113                         data.index,
114                         data.name)
115             }
116
117             ty::ReClosureBound(ref vid) => {
118                 write!(f, "ReClosureBound({:?})", vid)
119             }
120
121             ty::ReLateBound(binder_id, ref bound_region) => {
122                 write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
123             }
124
125             ty::ReFree(ref fr) => fr.fmt(f),
126
127             ty::ReScope(id) => write!(f, "ReScope({:?})", id),
128
129             ty::ReStatic => write!(f, "ReStatic"),
130
131             ty::ReVar(ref vid) => vid.fmt(f),
132
133             ty::RePlaceholder(placeholder) => {
134                 write!(f, "RePlaceholder({:?})", placeholder)
135             }
136
137             ty::ReEmpty => write!(f, "ReEmpty"),
138
139             ty::ReErased => write!(f, "ReErased"),
140         }
141     }
142 }
143
144 impl fmt::Debug for ty::FreeRegion {
145     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146         write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
147     }
148 }
149
150 impl fmt::Debug for ty::Variance {
151     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152         f.write_str(match *self {
153             ty::Covariant => "+",
154             ty::Contravariant => "-",
155             ty::Invariant => "o",
156             ty::Bivariant => "*",
157         })
158     }
159 }
160
161 impl fmt::Debug for ty::FnSig<'tcx> {
162     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163         write!(f, "({:?}; c_variadic: {})->{:?}",
164                 self.inputs(), self.c_variadic, self.output())
165     }
166 }
167
168 impl fmt::Debug for ty::TyVid {
169     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170         write!(f, "_#{}t", self.index)
171     }
172 }
173
174 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
175     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
176         write!(f, "_#{}c", self.index)
177     }
178 }
179
180 impl fmt::Debug for ty::IntVid {
181     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182         write!(f, "_#{}i", self.index)
183     }
184 }
185
186 impl fmt::Debug for ty::FloatVid {
187     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
188         write!(f, "_#{}f", self.index)
189     }
190 }
191
192 impl fmt::Debug for ty::RegionVid {
193     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
194         write!(f, "'_#{}r", self.index())
195     }
196 }
197
198 impl fmt::Debug for ty::InferTy {
199     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200         match *self {
201             ty::TyVar(ref v) => v.fmt(f),
202             ty::IntVar(ref v) => v.fmt(f),
203             ty::FloatVar(ref v) => v.fmt(f),
204             ty::FreshTy(v) => write!(f, "FreshTy({:?})", v),
205             ty::FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
206             ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v),
207         }
208     }
209 }
210
211 impl fmt::Debug for ty::IntVarValue {
212     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
213         match *self {
214             ty::IntType(ref v) => v.fmt(f),
215             ty::UintType(ref v) => v.fmt(f),
216         }
217     }
218 }
219
220 impl fmt::Debug for ty::FloatVarValue {
221     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
222         self.0.fmt(f)
223     }
224 }
225
226 impl fmt::Debug for ty::TraitRef<'tcx> {
227     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
228         // FIXME(#59188) this is used across the compiler to print
229         // a `TraitRef` qualified (with the Self type explicit),
230         // instead of having a different way to make that choice.
231         write!(f, "<{} as {}>", self.self_ty(), self)
232     }
233 }
234
235 impl fmt::Debug for Ty<'tcx> {
236     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
237         fmt::Display::fmt(self, f)
238     }
239 }
240
241 impl fmt::Debug for ty::ParamTy {
242     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
243         write!(f, "{}/#{}", self.name, self.index)
244     }
245 }
246
247 impl fmt::Debug for ty::ParamConst {
248     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
249         write!(f, "{}/#{}", self.name, self.index)
250     }
251 }
252
253 impl fmt::Debug for ty::TraitPredicate<'tcx> {
254     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
255         write!(f, "TraitPredicate({:?})", self.trait_ref)
256     }
257 }
258
259 impl fmt::Debug for ty::ProjectionPredicate<'tcx> {
260     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
261         write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty)
262     }
263 }
264
265 impl fmt::Debug for ty::Predicate<'tcx> {
266     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
267         match *self {
268             ty::Predicate::Trait(ref a) => a.fmt(f),
269             ty::Predicate::Subtype(ref pair) => pair.fmt(f),
270             ty::Predicate::RegionOutlives(ref pair) => pair.fmt(f),
271             ty::Predicate::TypeOutlives(ref pair) => pair.fmt(f),
272             ty::Predicate::Projection(ref pair) => pair.fmt(f),
273             ty::Predicate::WellFormed(ty) => write!(f, "WellFormed({:?})", ty),
274             ty::Predicate::ObjectSafe(trait_def_id) => {
275                 write!(f, "ObjectSafe({:?})", trait_def_id)
276             }
277             ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
278                 write!(f, "ClosureKind({:?}, {:?}, {:?})",
279                     closure_def_id, closure_substs, kind)
280             }
281             ty::Predicate::ConstEvaluatable(def_id, substs) => {
282                 write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs)
283             }
284         }
285     }
286 }
287
288 ///////////////////////////////////////////////////////////////////////////
289 // Atomic structs
290 //
291 // For things that don't carry any arena-allocated data (and are
292 // copy...), just add them to this list.
293
294 CloneTypeFoldableAndLiftImpls! {
295     (),
296     bool,
297     usize,
298     u32,
299     crate::ty::BoundVar,
300     crate::ty::DebruijnIndex,
301     crate::ty::layout::VariantIdx,
302     u64,
303     String,
304     crate::middle::region::Scope,
305     ::syntax::ast::FloatTy,
306     ::syntax::ast::NodeId,
307     ::syntax_pos::symbol::Symbol,
308     crate::hir::def::Res,
309     crate::hir::def_id::DefId,
310     crate::hir::InlineAsm,
311     crate::hir::MatchSource,
312     crate::hir::Mutability,
313     crate::hir::Unsafety,
314     ::rustc_target::spec::abi::Abi,
315     crate::mir::Local,
316     crate::mir::Promoted,
317     crate::mir::interpret::Scalar,
318     crate::mir::interpret::Pointer,
319     crate::traits::Reveal,
320     crate::ty::adjustment::AutoBorrowMutability,
321     crate::ty::AdtKind,
322     // Including `BoundRegion` is a *bit* dubious, but direct
323     // references to bound region appear in `ty::Error`, and aren't
324     // really meant to be folded. In general, we can only fold a fully
325     // general `Region`.
326     crate::ty::BoundRegion,
327     crate::ty::Placeholder<crate::ty::BoundRegion>,
328     crate::ty::ClosureKind,
329     crate::ty::FreeRegion,
330     crate::ty::InferTy,
331     crate::ty::IntVarValue,
332     crate::ty::ParamConst,
333     crate::ty::ParamTy,
334     crate::ty::adjustment::PointerCast,
335     crate::ty::RegionVid,
336     crate::ty::UniverseIndex,
337     crate::ty::Variance,
338     ::syntax_pos::Span,
339 }
340
341 ///////////////////////////////////////////////////////////////////////////
342 // Lift implementations
343
344 // FIXME(eddyb) replace all the uses of `Option::map` with `?`.
345 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
346     type Lifted = (A::Lifted, B::Lifted);
347     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
348         tcx.lift(&self.0).and_then(|a| tcx.lift(&self.1).map(|b| (a, b)))
349     }
350 }
351
352 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
353     type Lifted = (A::Lifted, B::Lifted, C::Lifted);
354     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
355         tcx.lift(&self.0).and_then(|a| {
356             tcx.lift(&self.1).and_then(|b| tcx.lift(&self.2).map(|c| (a, b, c)))
357         })
358     }
359 }
360
361 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
362     type Lifted = Option<T::Lifted>;
363     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
364         match *self {
365             Some(ref x) => tcx.lift(x).map(Some),
366             None => Some(None)
367         }
368     }
369 }
370
371 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
372     type Lifted = Result<T::Lifted, E::Lifted>;
373     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
374         match *self {
375             Ok(ref x) => tcx.lift(x).map(Ok),
376             Err(ref e) => tcx.lift(e).map(Err)
377         }
378     }
379 }
380
381 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
382     type Lifted = Box<T::Lifted>;
383     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
384         tcx.lift(&**self).map(Box::new)
385     }
386 }
387
388 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for [T] {
389     type Lifted = Vec<T::Lifted>;
390     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
391         // type annotation needed to inform `projection_must_outlive`
392         let mut result : Vec<<T as Lift<'tcx>>::Lifted>
393             = Vec::with_capacity(self.len());
394         for x in self {
395             if let Some(value) = tcx.lift(x) {
396                 result.push(value);
397             } else {
398                 return None;
399             }
400         }
401         Some(result)
402     }
403 }
404
405 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
406     type Lifted = Vec<T::Lifted>;
407     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
408         tcx.lift(&self[..])
409     }
410 }
411
412 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
413     type Lifted = IndexVec<I, T::Lifted>;
414     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
415         self.iter()
416             .map(|e| tcx.lift(e))
417             .collect()
418     }
419 }
420
421 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
422     type Lifted = ty::TraitRef<'tcx>;
423     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
424         tcx.lift(&self.substs).map(|substs| ty::TraitRef {
425             def_id: self.def_id,
426             substs,
427         })
428     }
429 }
430
431 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
432     type Lifted = ty::ExistentialTraitRef<'tcx>;
433     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
434         tcx.lift(&self.substs).map(|substs| ty::ExistentialTraitRef {
435             def_id: self.def_id,
436             substs,
437         })
438     }
439 }
440
441 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
442     type Lifted = ty::ExistentialPredicate<'tcx>;
443     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
444         match self {
445             ty::ExistentialPredicate::Trait(x) => {
446                 tcx.lift(x).map(ty::ExistentialPredicate::Trait)
447             }
448             ty::ExistentialPredicate::Projection(x) => {
449                 tcx.lift(x).map(ty::ExistentialPredicate::Projection)
450             }
451             ty::ExistentialPredicate::AutoTrait(def_id) => {
452                 Some(ty::ExistentialPredicate::AutoTrait(*def_id))
453             }
454         }
455     }
456 }
457
458 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
459     type Lifted = ty::TraitPredicate<'tcx>;
460     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
461                              -> Option<ty::TraitPredicate<'tcx>> {
462         tcx.lift(&self.trait_ref).map(|trait_ref| ty::TraitPredicate {
463             trait_ref,
464         })
465     }
466 }
467
468 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
469     type Lifted = ty::SubtypePredicate<'tcx>;
470     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
471                              -> Option<ty::SubtypePredicate<'tcx>> {
472         tcx.lift(&(self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
473             a_is_expected: self.a_is_expected,
474             a,
475             b,
476         })
477     }
478 }
479
480 impl<'tcx, A: Copy+Lift<'tcx>, B: Copy+Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
481     type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
482     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
483         tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
484     }
485 }
486
487 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
488     type Lifted = ty::ProjectionTy<'tcx>;
489     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
490                              -> Option<ty::ProjectionTy<'tcx>> {
491         tcx.lift(&self.substs).map(|substs| {
492             ty::ProjectionTy {
493                 item_def_id: self.item_def_id,
494                 substs,
495             }
496         })
497     }
498 }
499
500 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
501     type Lifted = ty::ProjectionPredicate<'tcx>;
502     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
503                              -> Option<ty::ProjectionPredicate<'tcx>> {
504         tcx.lift(&(self.projection_ty, self.ty)).map(|(projection_ty, ty)| {
505             ty::ProjectionPredicate {
506                 projection_ty,
507                 ty,
508             }
509         })
510     }
511 }
512
513 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
514     type Lifted = ty::ExistentialProjection<'tcx>;
515     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
516         tcx.lift(&self.substs).map(|substs| {
517             ty::ExistentialProjection {
518                 substs,
519                 ty: tcx.lift(&self.ty).expect("type must lift when substs do"),
520                 item_def_id: self.item_def_id,
521             }
522         })
523     }
524 }
525
526 impl<'a, 'tcx> Lift<'tcx> for ty::Predicate<'a> {
527     type Lifted = ty::Predicate<'tcx>;
528     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
529         match *self {
530             ty::Predicate::Trait(ref binder) => {
531                 tcx.lift(binder).map(ty::Predicate::Trait)
532             }
533             ty::Predicate::Subtype(ref binder) => {
534                 tcx.lift(binder).map(ty::Predicate::Subtype)
535             }
536             ty::Predicate::RegionOutlives(ref binder) => {
537                 tcx.lift(binder).map(ty::Predicate::RegionOutlives)
538             }
539             ty::Predicate::TypeOutlives(ref binder) => {
540                 tcx.lift(binder).map(ty::Predicate::TypeOutlives)
541             }
542             ty::Predicate::Projection(ref binder) => {
543                 tcx.lift(binder).map(ty::Predicate::Projection)
544             }
545             ty::Predicate::WellFormed(ty) => {
546                 tcx.lift(&ty).map(ty::Predicate::WellFormed)
547             }
548             ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
549                 tcx.lift(&closure_substs)
550                    .map(|closure_substs| ty::Predicate::ClosureKind(closure_def_id,
551                                                                     closure_substs,
552                                                                     kind))
553             }
554             ty::Predicate::ObjectSafe(trait_def_id) => {
555                 Some(ty::Predicate::ObjectSafe(trait_def_id))
556             }
557             ty::Predicate::ConstEvaluatable(def_id, substs) => {
558                 tcx.lift(&substs).map(|substs| {
559                     ty::Predicate::ConstEvaluatable(def_id, substs)
560                 })
561             }
562         }
563     }
564 }
565
566 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
567     type Lifted = ty::Binder<T::Lifted>;
568     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
569         tcx.lift(self.skip_binder()).map(ty::Binder::bind)
570     }
571 }
572
573 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
574     type Lifted = ty::ParamEnv<'tcx>;
575     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
576         tcx.lift(&self.caller_bounds).map(|caller_bounds| {
577             ty::ParamEnv {
578                 reveal: self.reveal,
579                 caller_bounds,
580                 def_id: self.def_id,
581             }
582         })
583     }
584 }
585
586 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
587     type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
588     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
589         tcx.lift(&self.param_env).and_then(|param_env| {
590             tcx.lift(&self.value).map(|value| {
591                 ty::ParamEnvAnd {
592                     param_env,
593                     value,
594                 }
595             })
596         })
597     }
598 }
599
600 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
601     type Lifted = ty::ClosureSubsts<'tcx>;
602     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
603         tcx.lift(&self.substs).map(|substs| {
604             ty::ClosureSubsts { substs }
605         })
606     }
607 }
608
609 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
610     type Lifted = ty::GeneratorSubsts<'tcx>;
611     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
612         tcx.lift(&self.substs).map(|substs| {
613             ty::GeneratorSubsts { substs }
614         })
615     }
616 }
617
618 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
619     type Lifted = ty::adjustment::Adjustment<'tcx>;
620     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
621         tcx.lift(&self.kind).and_then(|kind| {
622             tcx.lift(&self.target).map(|target| {
623                 ty::adjustment::Adjustment { kind, target }
624             })
625         })
626     }
627 }
628
629 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
630     type Lifted = ty::adjustment::Adjust<'tcx>;
631     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
632         match *self {
633             ty::adjustment::Adjust::NeverToAny =>
634                 Some(ty::adjustment::Adjust::NeverToAny),
635             ty::adjustment::Adjust::Pointer(ptr) =>
636                 Some(ty::adjustment::Adjust::Pointer(ptr)),
637             ty::adjustment::Adjust::Deref(ref overloaded) => {
638                 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
639             }
640             ty::adjustment::Adjust::Borrow(ref autoref) => {
641                 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
642             }
643         }
644     }
645 }
646
647 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
648     type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
649     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
650         tcx.lift(&self.region).map(|region| {
651             ty::adjustment::OverloadedDeref {
652                 region,
653                 mutbl: self.mutbl,
654             }
655         })
656     }
657 }
658
659 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
660     type Lifted = ty::adjustment::AutoBorrow<'tcx>;
661     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
662         match *self {
663             ty::adjustment::AutoBorrow::Ref(r, m) => {
664                 tcx.lift(&r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
665             }
666             ty::adjustment::AutoBorrow::RawPtr(m) => {
667                 Some(ty::adjustment::AutoBorrow::RawPtr(m))
668             }
669         }
670     }
671 }
672
673 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
674     type Lifted = ty::GenSig<'tcx>;
675     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
676         tcx.lift(&(self.yield_ty, self.return_ty))
677            .map(|(yield_ty, return_ty)| {
678                ty::GenSig {
679                    yield_ty,
680                    return_ty,
681                }
682            })
683     }
684 }
685
686 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
687     type Lifted = ty::FnSig<'tcx>;
688     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
689         tcx.lift(&self.inputs_and_output).map(|x| {
690             ty::FnSig {
691                 inputs_and_output: x,
692                 c_variadic: self.c_variadic,
693                 unsafety: self.unsafety,
694                 abi: self.abi,
695             }
696         })
697     }
698 }
699
700 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
701     type Lifted = ty::error::ExpectedFound<T::Lifted>;
702     fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
703         tcx.lift(&self.expected).and_then(|expected| {
704             tcx.lift(&self.found).map(|found| {
705                 ty::error::ExpectedFound {
706                     expected,
707                     found,
708                 }
709             })
710         })
711     }
712 }
713
714 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
715     type Lifted = ty::error::TypeError<'tcx>;
716     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
717         use crate::ty::error::TypeError::*;
718
719         Some(match *self {
720             Mismatch => Mismatch,
721             UnsafetyMismatch(x) => UnsafetyMismatch(x),
722             AbiMismatch(x) => AbiMismatch(x),
723             Mutability => Mutability,
724             TupleSize(x) => TupleSize(x),
725             FixedArraySize(x) => FixedArraySize(x),
726             ArgCount => ArgCount,
727             RegionsDoesNotOutlive(a, b) => {
728                 return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b))
729             }
730             RegionsInsufficientlyPolymorphic(a, b) => {
731                 return tcx.lift(&b).map(|b| RegionsInsufficientlyPolymorphic(a, b))
732             }
733             RegionsOverlyPolymorphic(a, b) => {
734                 return tcx.lift(&b).map(|b| RegionsOverlyPolymorphic(a, b))
735             }
736             RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
737             IntMismatch(x) => IntMismatch(x),
738             FloatMismatch(x) => FloatMismatch(x),
739             Traits(x) => Traits(x),
740             VariadicMismatch(x) => VariadicMismatch(x),
741             CyclicTy(t) => return tcx.lift(&t).map(|t| CyclicTy(t)),
742             ProjectionMismatched(x) => ProjectionMismatched(x),
743             ProjectionBoundsLength(x) => ProjectionBoundsLength(x),
744             Sorts(ref x) => return tcx.lift(x).map(Sorts),
745             ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch),
746             ConstMismatch(ref x) => return tcx.lift(x).map(ConstMismatch),
747         })
748     }
749 }
750
751 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
752     type Lifted = ty::InstanceDef<'tcx>;
753     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
754         match *self {
755             ty::InstanceDef::Item(def_id) =>
756                 Some(ty::InstanceDef::Item(def_id)),
757             ty::InstanceDef::VtableShim(def_id) =>
758                 Some(ty::InstanceDef::VtableShim(def_id)),
759             ty::InstanceDef::Intrinsic(def_id) =>
760                 Some(ty::InstanceDef::Intrinsic(def_id)),
761             ty::InstanceDef::FnPtrShim(def_id, ref ty) =>
762                 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?)),
763             ty::InstanceDef::Virtual(def_id, n) =>
764                 Some(ty::InstanceDef::Virtual(def_id, n)),
765             ty::InstanceDef::ClosureOnceShim { call_once } =>
766                 Some(ty::InstanceDef::ClosureOnceShim { call_once }),
767             ty::InstanceDef::DropGlue(def_id, ref ty) =>
768                 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?)),
769             ty::InstanceDef::CloneShim(def_id, ref ty) =>
770                 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?)),
771         }
772     }
773 }
774
775 BraceStructLiftImpl! {
776     impl<'a, 'tcx> Lift<'tcx> for ty::TypeAndMut<'a> {
777         type Lifted = ty::TypeAndMut<'tcx>;
778         ty, mutbl
779     }
780 }
781
782 BraceStructLiftImpl! {
783     impl<'a, 'tcx> Lift<'tcx> for ty::Instance<'a> {
784         type Lifted = ty::Instance<'tcx>;
785         def, substs
786     }
787 }
788
789 BraceStructLiftImpl! {
790     impl<'a, 'tcx> Lift<'tcx> for interpret::GlobalId<'a> {
791         type Lifted = interpret::GlobalId<'tcx>;
792         instance, promoted
793     }
794 }
795
796 BraceStructLiftImpl! {
797     impl<'a, 'tcx> Lift<'tcx> for ty::Const<'a> {
798         type Lifted = ty::Const<'tcx>;
799         val, ty
800     }
801 }
802
803 EnumLiftImpl! {
804     impl<'a, 'tcx> Lift<'tcx> for interpret::ConstValue<'a> {
805         type Lifted = interpret::ConstValue<'tcx>;
806         (interpret::ConstValue::Unevaluated)(a, b),
807         (interpret::ConstValue::Param)(a),
808         (interpret::ConstValue::Infer)(a),
809         (interpret::ConstValue::Scalar)(a),
810         (interpret::ConstValue::Slice)(a, b),
811         (interpret::ConstValue::ByRef)(a, b),
812     }
813 }
814
815 EnumLiftImpl! {
816     impl<'a, 'tcx> Lift<'tcx> for ty::InferConst<'a> {
817         type Lifted = ty::InferConst<'tcx>;
818         (ty::InferConst::Var)(a),
819         (ty::InferConst::Fresh)(a),
820         (ty::InferConst::Canonical)(a, b),
821     }
822 }
823
824 impl<'a, 'tcx> Lift<'tcx> for ConstVid<'a> {
825     type Lifted = ConstVid<'tcx>;
826     fn lift_to_tcx<'b, 'gcx>(&self, _: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
827         Some(ConstVid {
828             index: self.index,
829             phantom: PhantomData,
830         })
831     }
832 }
833
834 ///////////////////////////////////////////////////////////////////////////
835 // TypeFoldable implementations.
836 //
837 // Ideally, each type should invoke `folder.fold_foo(self)` and
838 // nothing else. In some cases, though, we haven't gotten around to
839 // adding methods on the `folder` yet, and thus the folding is
840 // hard-coded here. This is less-flexible, because folders cannot
841 // override the behavior, but there are a lot of random types and one
842 // can easily refactor the folding into the TypeFolder trait as
843 // needed.
844
845 /// AdtDefs are basically the same as a DefId.
846 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
847     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
848         *self
849     }
850
851     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
852         false
853     }
854 }
855
856 impl<'tcx, T:TypeFoldable<'tcx>, U:TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
857     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> (T, U) {
858         (self.0.fold_with(folder), self.1.fold_with(folder))
859     }
860
861     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
862         self.0.visit_with(visitor) || self.1.visit_with(visitor)
863     }
864 }
865
866 EnumTypeFoldableImpl! {
867     impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
868         (Some)(a),
869         (None),
870     } where T: TypeFoldable<'tcx>
871 }
872
873 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
874     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
875         Rc::new((**self).fold_with(folder))
876     }
877
878     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
879         (**self).visit_with(visitor)
880     }
881 }
882
883 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
884     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
885         let content: T = (**self).fold_with(folder);
886         box content
887     }
888
889     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
890         (**self).visit_with(visitor)
891     }
892 }
893
894 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
895     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
896         self.iter().map(|t| t.fold_with(folder)).collect()
897     }
898
899     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
900         self.iter().any(|t| t.visit_with(visitor))
901     }
902 }
903
904 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
905     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
906         self.iter().map(|t| t.fold_with(folder)).collect::<Vec<_>>().into_boxed_slice()
907     }
908
909     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
910         self.iter().any(|t| t.visit_with(visitor))
911     }
912 }
913
914 impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
915     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
916         self.map_bound_ref(|ty| ty.fold_with(folder))
917     }
918
919     fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
920         folder.fold_binder(self)
921     }
922
923     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
924         self.skip_binder().visit_with(visitor)
925     }
926
927     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
928         visitor.visit_binder(self)
929     }
930 }
931
932 BraceStructTypeFoldableImpl! {
933     impl<'tcx> TypeFoldable<'tcx> for ty::ParamEnv<'tcx> { reveal, caller_bounds, def_id }
934 }
935
936 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
937     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
938         let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
939         folder.tcx().intern_existential_predicates(&v)
940     }
941
942     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
943         self.iter().any(|p| p.visit_with(visitor))
944     }
945 }
946
947 EnumTypeFoldableImpl! {
948     impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialPredicate<'tcx> {
949         (ty::ExistentialPredicate::Trait)(a),
950         (ty::ExistentialPredicate::Projection)(a),
951         (ty::ExistentialPredicate::AutoTrait)(a),
952     }
953 }
954
955 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
956     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
957         let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
958         folder.tcx().intern_type_list(&v)
959     }
960
961     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
962         self.iter().any(|t| t.visit_with(visitor))
963     }
964 }
965
966 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
967     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
968         let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
969         folder.tcx().intern_projs(&v)
970     }
971
972     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
973         self.iter().any(|t| t.visit_with(visitor))
974     }
975 }
976
977 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
978     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
979         use crate::ty::InstanceDef::*;
980         Self {
981             substs: self.substs.fold_with(folder),
982             def: match self.def {
983                 Item(did) => Item(did.fold_with(folder)),
984                 VtableShim(did) => VtableShim(did.fold_with(folder)),
985                 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
986                 FnPtrShim(did, ty) => FnPtrShim(
987                     did.fold_with(folder),
988                     ty.fold_with(folder),
989                 ),
990                 Virtual(did, i) => Virtual(
991                     did.fold_with(folder),
992                     i,
993                 ),
994                 ClosureOnceShim { call_once } => ClosureOnceShim {
995                     call_once: call_once.fold_with(folder),
996                 },
997                 DropGlue(did, ty) => DropGlue(
998                     did.fold_with(folder),
999                     ty.fold_with(folder),
1000                 ),
1001                 CloneShim(did, ty) => CloneShim(
1002                     did.fold_with(folder),
1003                     ty.fold_with(folder),
1004                 ),
1005             },
1006         }
1007     }
1008
1009     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1010         use crate::ty::InstanceDef::*;
1011         self.substs.visit_with(visitor) ||
1012         match self.def {
1013             Item(did) | VtableShim(did) | Intrinsic(did) | Virtual(did, _) => {
1014                 did.visit_with(visitor)
1015             },
1016             FnPtrShim(did, ty) | CloneShim(did, ty) => {
1017                 did.visit_with(visitor) || ty.visit_with(visitor)
1018             },
1019             DropGlue(did, ty) => {
1020                 did.visit_with(visitor) || ty.visit_with(visitor)
1021             },
1022             ClosureOnceShim { call_once } => call_once.visit_with(visitor),
1023         }
1024     }
1025 }
1026
1027 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
1028     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1029         Self {
1030             instance: self.instance.fold_with(folder),
1031             promoted: self.promoted
1032         }
1033     }
1034
1035     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1036         self.instance.visit_with(visitor)
1037     }
1038 }
1039
1040 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
1041     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1042         let sty = match self.sty {
1043             ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
1044             ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
1045             ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
1046             ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
1047             ty::Dynamic(ref trait_ty, ref region) =>
1048                 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder)),
1049             ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
1050             ty::FnDef(def_id, substs) => {
1051                 ty::FnDef(def_id, substs.fold_with(folder))
1052             }
1053             ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
1054             ty::Ref(ref r, ty, mutbl) => {
1055                 ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl)
1056             }
1057             ty::Generator(did, substs, movability) => {
1058                 ty::Generator(
1059                     did,
1060                     substs.fold_with(folder),
1061                     movability)
1062             }
1063             ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
1064             ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
1065             ty::Projection(ref data) => ty::Projection(data.fold_with(folder)),
1066             ty::UnnormalizedProjection(ref data) => {
1067                 ty::UnnormalizedProjection(data.fold_with(folder))
1068             }
1069             ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
1070
1071             ty::Bool |
1072             ty::Char |
1073             ty::Str |
1074             ty::Int(_) |
1075             ty::Uint(_) |
1076             ty::Float(_) |
1077             ty::Error |
1078             ty::Infer(_) |
1079             ty::Param(..) |
1080             ty::Bound(..) |
1081             ty::Placeholder(..) |
1082             ty::Never |
1083             ty::Foreign(..) => return self
1084         };
1085
1086         if self.sty == sty {
1087             self
1088         } else {
1089             folder.tcx().mk_ty(sty)
1090         }
1091     }
1092
1093     fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1094         folder.fold_ty(*self)
1095     }
1096
1097     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1098         match self.sty {
1099             ty::RawPtr(ref tm) => tm.visit_with(visitor),
1100             ty::Array(typ, sz) => typ.visit_with(visitor) || sz.visit_with(visitor),
1101             ty::Slice(typ) => typ.visit_with(visitor),
1102             ty::Adt(_, substs) => substs.visit_with(visitor),
1103             ty::Dynamic(ref trait_ty, ref reg) =>
1104                 trait_ty.visit_with(visitor) || reg.visit_with(visitor),
1105             ty::Tuple(ts) => ts.visit_with(visitor),
1106             ty::FnDef(_, substs) => substs.visit_with(visitor),
1107             ty::FnPtr(ref f) => f.visit_with(visitor),
1108             ty::Ref(r, ty, _) => r.visit_with(visitor) || ty.visit_with(visitor),
1109             ty::Generator(_did, ref substs, _) => {
1110                 substs.visit_with(visitor)
1111             }
1112             ty::GeneratorWitness(ref types) => types.visit_with(visitor),
1113             ty::Closure(_did, ref substs) => substs.visit_with(visitor),
1114             ty::Projection(ref data) | ty::UnnormalizedProjection(ref data) => {
1115                 data.visit_with(visitor)
1116             }
1117             ty::Opaque(_, ref substs) => substs.visit_with(visitor),
1118
1119             ty::Bool |
1120             ty::Char |
1121             ty::Str |
1122             ty::Int(_) |
1123             ty::Uint(_) |
1124             ty::Float(_) |
1125             ty::Error |
1126             ty::Infer(_) |
1127             ty::Bound(..) |
1128             ty::Placeholder(..) |
1129             ty::Param(..) |
1130             ty::Never |
1131             ty::Foreign(..) => false,
1132         }
1133     }
1134
1135     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1136         visitor.visit_ty(self)
1137     }
1138 }
1139
1140 BraceStructTypeFoldableImpl! {
1141     impl<'tcx> TypeFoldable<'tcx> for ty::TypeAndMut<'tcx> {
1142         ty, mutbl
1143     }
1144 }
1145
1146 BraceStructTypeFoldableImpl! {
1147     impl<'tcx> TypeFoldable<'tcx> for ty::GenSig<'tcx> {
1148         yield_ty, return_ty
1149     }
1150 }
1151
1152 BraceStructTypeFoldableImpl! {
1153     impl<'tcx> TypeFoldable<'tcx> for ty::FnSig<'tcx> {
1154         inputs_and_output, c_variadic, unsafety, abi
1155     }
1156 }
1157
1158 BraceStructTypeFoldableImpl! {
1159     impl<'tcx> TypeFoldable<'tcx> for ty::TraitRef<'tcx> { def_id, substs }
1160 }
1161
1162 BraceStructTypeFoldableImpl! {
1163     impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialTraitRef<'tcx> { def_id, substs }
1164 }
1165
1166 BraceStructTypeFoldableImpl! {
1167     impl<'tcx> TypeFoldable<'tcx> for ty::ImplHeader<'tcx> {
1168         impl_def_id,
1169         self_ty,
1170         trait_ref,
1171         predicates,
1172     }
1173 }
1174
1175 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
1176     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
1177         *self
1178     }
1179
1180     fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1181         folder.fold_region(*self)
1182     }
1183
1184     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
1185         false
1186     }
1187
1188     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1189         visitor.visit_region(*self)
1190     }
1191 }
1192
1193 BraceStructTypeFoldableImpl! {
1194     impl<'tcx> TypeFoldable<'tcx> for ty::ClosureSubsts<'tcx> {
1195         substs,
1196     }
1197 }
1198
1199 BraceStructTypeFoldableImpl! {
1200     impl<'tcx> TypeFoldable<'tcx> for ty::GeneratorSubsts<'tcx> {
1201         substs,
1202     }
1203 }
1204
1205 BraceStructTypeFoldableImpl! {
1206     impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::Adjustment<'tcx> {
1207         kind,
1208         target,
1209     }
1210 }
1211
1212 EnumTypeFoldableImpl! {
1213     impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::Adjust<'tcx> {
1214         (ty::adjustment::Adjust::NeverToAny),
1215         (ty::adjustment::Adjust::Pointer)(a),
1216         (ty::adjustment::Adjust::Deref)(a),
1217         (ty::adjustment::Adjust::Borrow)(a),
1218     }
1219 }
1220
1221 BraceStructTypeFoldableImpl! {
1222     impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::OverloadedDeref<'tcx> {
1223         region, mutbl,
1224     }
1225 }
1226
1227 EnumTypeFoldableImpl! {
1228     impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::AutoBorrow<'tcx> {
1229         (ty::adjustment::AutoBorrow::Ref)(a, b),
1230         (ty::adjustment::AutoBorrow::RawPtr)(m),
1231     }
1232 }
1233
1234 BraceStructTypeFoldableImpl! {
1235     impl<'tcx> TypeFoldable<'tcx> for ty::GenericPredicates<'tcx> {
1236         parent, predicates
1237     }
1238 }
1239
1240 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
1241     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1242         let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
1243         folder.tcx().intern_predicates(&v)
1244     }
1245
1246     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1247         self.iter().any(|p| p.visit_with(visitor))
1248     }
1249 }
1250
1251 EnumTypeFoldableImpl! {
1252     impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
1253         (ty::Predicate::Trait)(a),
1254         (ty::Predicate::Subtype)(a),
1255         (ty::Predicate::RegionOutlives)(a),
1256         (ty::Predicate::TypeOutlives)(a),
1257         (ty::Predicate::Projection)(a),
1258         (ty::Predicate::WellFormed)(a),
1259         (ty::Predicate::ClosureKind)(a, b, c),
1260         (ty::Predicate::ObjectSafe)(a),
1261         (ty::Predicate::ConstEvaluatable)(a, b),
1262     }
1263 }
1264
1265 BraceStructTypeFoldableImpl! {
1266     impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionPredicate<'tcx> {
1267         projection_ty, ty
1268     }
1269 }
1270
1271 BraceStructTypeFoldableImpl! {
1272     impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialProjection<'tcx> {
1273         ty, substs, item_def_id
1274     }
1275 }
1276
1277 BraceStructTypeFoldableImpl! {
1278     impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionTy<'tcx> {
1279         substs, item_def_id
1280     }
1281 }
1282
1283 BraceStructTypeFoldableImpl! {
1284     impl<'tcx> TypeFoldable<'tcx> for ty::InstantiatedPredicates<'tcx> {
1285         predicates
1286     }
1287 }
1288
1289 BraceStructTypeFoldableImpl! {
1290     impl<'tcx, T> TypeFoldable<'tcx> for ty::ParamEnvAnd<'tcx, T> {
1291         param_env, value
1292     } where T: TypeFoldable<'tcx>
1293 }
1294
1295 BraceStructTypeFoldableImpl! {
1296     impl<'tcx> TypeFoldable<'tcx> for ty::SubtypePredicate<'tcx> {
1297         a_is_expected, a, b
1298     }
1299 }
1300
1301 BraceStructTypeFoldableImpl! {
1302     impl<'tcx> TypeFoldable<'tcx> for ty::TraitPredicate<'tcx> {
1303         trait_ref
1304     }
1305 }
1306
1307 TupleStructTypeFoldableImpl! {
1308     impl<'tcx,T,U> TypeFoldable<'tcx> for ty::OutlivesPredicate<T,U> {
1309         a, b
1310     } where T : TypeFoldable<'tcx>, U : TypeFoldable<'tcx>,
1311 }
1312
1313 BraceStructTypeFoldableImpl! {
1314     impl<'tcx> TypeFoldable<'tcx> for ty::ClosureUpvar<'tcx> {
1315         res, span, ty
1316     }
1317 }
1318
1319 BraceStructTypeFoldableImpl! {
1320     impl<'tcx, T> TypeFoldable<'tcx> for ty::error::ExpectedFound<T> {
1321         expected, found
1322     } where T: TypeFoldable<'tcx>
1323 }
1324
1325 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
1326     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1327         self.iter().map(|x| x.fold_with(folder)).collect()
1328     }
1329
1330     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1331         self.iter().any(|t| t.visit_with(visitor))
1332     }
1333 }
1334
1335 EnumTypeFoldableImpl! {
1336     impl<'tcx> TypeFoldable<'tcx> for ty::error::TypeError<'tcx> {
1337         (ty::error::TypeError::Mismatch),
1338         (ty::error::TypeError::UnsafetyMismatch)(x),
1339         (ty::error::TypeError::AbiMismatch)(x),
1340         (ty::error::TypeError::Mutability),
1341         (ty::error::TypeError::TupleSize)(x),
1342         (ty::error::TypeError::FixedArraySize)(x),
1343         (ty::error::TypeError::ArgCount),
1344         (ty::error::TypeError::RegionsDoesNotOutlive)(a, b),
1345         (ty::error::TypeError::RegionsInsufficientlyPolymorphic)(a, b),
1346         (ty::error::TypeError::RegionsOverlyPolymorphic)(a, b),
1347         (ty::error::TypeError::RegionsPlaceholderMismatch),
1348         (ty::error::TypeError::IntMismatch)(x),
1349         (ty::error::TypeError::FloatMismatch)(x),
1350         (ty::error::TypeError::Traits)(x),
1351         (ty::error::TypeError::VariadicMismatch)(x),
1352         (ty::error::TypeError::CyclicTy)(t),
1353         (ty::error::TypeError::ProjectionMismatched)(x),
1354         (ty::error::TypeError::ProjectionBoundsLength)(x),
1355         (ty::error::TypeError::Sorts)(x),
1356         (ty::error::TypeError::ExistentialMismatch)(x),
1357         (ty::error::TypeError::ConstMismatch)(x),
1358     }
1359 }
1360
1361 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1362     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1363         let ty = self.ty.fold_with(folder);
1364         let val = self.val.fold_with(folder);
1365         folder.tcx().mk_const(ty::Const {
1366             ty,
1367             val
1368         })
1369     }
1370
1371     fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1372         folder.fold_const(*self)
1373     }
1374
1375     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1376         self.ty.visit_with(visitor) || self.val.visit_with(visitor)
1377     }
1378
1379     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1380         visitor.visit_const(self)
1381     }
1382 }
1383
1384 impl<'tcx> TypeFoldable<'tcx> for ConstValue<'tcx> {
1385     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1386         match *self {
1387             ConstValue::ByRef(ptr, alloc) => ConstValue::ByRef(ptr, alloc),
1388             ConstValue::Infer(ic) => ConstValue::Infer(ic.fold_with(folder)),
1389             ConstValue::Param(p) => ConstValue::Param(p.fold_with(folder)),
1390             ConstValue::Placeholder(p) => ConstValue::Placeholder(p),
1391             ConstValue::Scalar(a) => ConstValue::Scalar(a),
1392             ConstValue::Slice(a, b) => ConstValue::Slice(a, b),
1393             ConstValue::Unevaluated(did, substs)
1394                 => ConstValue::Unevaluated(did, substs.fold_with(folder)),
1395         }
1396     }
1397
1398     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1399         match *self {
1400             ConstValue::ByRef(..) => false,
1401             ConstValue::Infer(ic) => ic.visit_with(visitor),
1402             ConstValue::Param(p) => p.visit_with(visitor),
1403             ConstValue::Placeholder(_) => false,
1404             ConstValue::Scalar(_) => false,
1405             ConstValue::Slice(..) => false,
1406             ConstValue::Unevaluated(_, substs) => substs.visit_with(visitor),
1407         }
1408     }
1409 }
1410
1411 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1412     fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
1413         *self
1414     }
1415
1416     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
1417         false
1418     }
1419 }