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