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