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