]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/structural_impls.rs
Remove `EarlyBinder::{try_fold_with,visit_with}`.
[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::{FallibleTypeFolder, TypeFoldable, TypeVisitor};
8 use crate::ty::print::{with_no_trimmed_paths, FmtPrinter, Printer};
9 use crate::ty::{self, InferConst, Lift, Term, Ty, TyCtxt};
10 use rustc_data_structures::functor::IdFunctor;
11 use rustc_hir as hir;
12 use rustc_hir::def::Namespace;
13 use rustc_index::vec::{Idx, IndexVec};
14
15 use std::fmt;
16 use std::mem::ManuallyDrop;
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                 f.write_str(
26                     &FmtPrinter::new(tcx, Namespace::TypeNS)
27                         .print_def_path(self.def_id, &[])?
28                         .into_buffer(),
29                 )
30             })
31         })
32     }
33 }
34
35 impl<'tcx> fmt::Debug for ty::AdtDef<'tcx> {
36     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37         ty::tls::with(|tcx| {
38             with_no_trimmed_paths!({
39                 f.write_str(
40                     &FmtPrinter::new(tcx, Namespace::TypeNS)
41                         .print_def_path(self.did(), &[])?
42                         .into_buffer(),
43                 )
44             })
45         })
46     }
47 }
48
49 impl fmt::Debug for ty::UpvarId {
50     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51         let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
52         write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
53     }
54 }
55
56 impl<'tcx> 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<'tcx> 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.is_crate_root() {
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<'tcx> fmt::Debug for ty::FnSig<'tcx> {
115     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116         write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
117     }
118 }
119
120 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
121     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122         write!(f, "_#{}c", self.index)
123     }
124 }
125
126 impl fmt::Debug for ty::RegionVid {
127     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128         write!(f, "'_#{}r", self.index())
129     }
130 }
131
132 impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
133     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134         with_no_trimmed_paths!(fmt::Display::fmt(self, f))
135     }
136 }
137
138 impl<'tcx> fmt::Debug for Ty<'tcx> {
139     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140         with_no_trimmed_paths!(fmt::Display::fmt(self, f))
141     }
142 }
143
144 impl fmt::Debug for ty::ParamTy {
145     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146         write!(f, "{}/#{}", self.name, self.index)
147     }
148 }
149
150 impl fmt::Debug for ty::ParamConst {
151     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152         write!(f, "{}/#{}", self.name, self.index)
153     }
154 }
155
156 impl<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> {
157     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158         if let ty::BoundConstness::ConstIfConst = self.constness {
159             write!(f, "~const ")?;
160         }
161         write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity)
162     }
163 }
164
165 impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
166     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167         write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.term)
168     }
169 }
170
171 impl<'tcx> fmt::Debug for ty::Predicate<'tcx> {
172     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173         write!(f, "{:?}", self.kind())
174     }
175 }
176
177 impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> {
178     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179         match *self {
180             ty::PredicateKind::Trait(ref a) => a.fmt(f),
181             ty::PredicateKind::Subtype(ref pair) => pair.fmt(f),
182             ty::PredicateKind::Coerce(ref pair) => pair.fmt(f),
183             ty::PredicateKind::RegionOutlives(ref pair) => pair.fmt(f),
184             ty::PredicateKind::TypeOutlives(ref pair) => pair.fmt(f),
185             ty::PredicateKind::Projection(ref pair) => pair.fmt(f),
186             ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data),
187             ty::PredicateKind::ObjectSafe(trait_def_id) => {
188                 write!(f, "ObjectSafe({:?})", trait_def_id)
189             }
190             ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
191                 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
192             }
193             ty::PredicateKind::ConstEvaluatable(uv) => {
194                 write!(f, "ConstEvaluatable({:?}, {:?})", uv.def, uv.substs)
195             }
196             ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
197             ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
198                 write!(f, "TypeWellFormedFromEnv({:?})", ty)
199             }
200         }
201     }
202 }
203
204 ///////////////////////////////////////////////////////////////////////////
205 // Atomic structs
206 //
207 // For things that don't carry any arena-allocated data (and are
208 // copy...), just add them to this list.
209
210 TrivialTypeFoldableAndLiftImpls! {
211     (),
212     bool,
213     usize,
214     ::rustc_target::abi::VariantIdx,
215     u32,
216     u64,
217     String,
218     crate::middle::region::Scope,
219     crate::ty::FloatTy,
220     ::rustc_ast::InlineAsmOptions,
221     ::rustc_ast::InlineAsmTemplatePiece,
222     ::rustc_ast::NodeId,
223     ::rustc_span::symbol::Symbol,
224     ::rustc_hir::def::Res,
225     ::rustc_hir::def_id::DefId,
226     ::rustc_hir::def_id::LocalDefId,
227     ::rustc_hir::HirId,
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     ::rustc_errors::ErrorGuaranteed,
263 }
264
265 ///////////////////////////////////////////////////////////////////////////
266 // Lift implementations
267
268 // FIXME(eddyb) replace all the uses of `Option::map` with `?`.
269 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
270     type Lifted = (A::Lifted, B::Lifted);
271     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
272         Some((tcx.lift(self.0)?, tcx.lift(self.1)?))
273     }
274 }
275
276 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
277     type Lifted = (A::Lifted, B::Lifted, C::Lifted);
278     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
279         Some((tcx.lift(self.0)?, tcx.lift(self.1)?, tcx.lift(self.2)?))
280     }
281 }
282
283 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
284     type Lifted = Option<T::Lifted>;
285     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
286         match self {
287             Some(x) => tcx.lift(x).map(Some),
288             None => Some(None),
289         }
290     }
291 }
292
293 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
294     type Lifted = Result<T::Lifted, E::Lifted>;
295     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
296         match self {
297             Ok(x) => tcx.lift(x).map(Ok),
298             Err(e) => tcx.lift(e).map(Err),
299         }
300     }
301 }
302
303 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
304     type Lifted = Box<T::Lifted>;
305     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
306         tcx.lift(*self).map(Box::new)
307     }
308 }
309
310 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Rc<T> {
311     type Lifted = Rc<T::Lifted>;
312     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
313         tcx.lift(self.as_ref().clone()).map(Rc::new)
314     }
315 }
316
317 impl<'tcx, T: Lift<'tcx> + Clone> Lift<'tcx> for Arc<T> {
318     type Lifted = Arc<T::Lifted>;
319     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
320         tcx.lift(self.as_ref().clone()).map(Arc::new)
321     }
322 }
323 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
324     type Lifted = Vec<T::Lifted>;
325     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
326         self.into_iter().map(|v| tcx.lift(v)).collect()
327     }
328 }
329
330 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
331     type Lifted = IndexVec<I, T::Lifted>;
332     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
333         self.into_iter().map(|e| tcx.lift(e)).collect()
334     }
335 }
336
337 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
338     type Lifted = ty::TraitRef<'tcx>;
339     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
340         tcx.lift(self.substs).map(|substs| ty::TraitRef { def_id: self.def_id, substs })
341     }
342 }
343
344 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
345     type Lifted = ty::ExistentialTraitRef<'tcx>;
346     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
347         tcx.lift(self.substs).map(|substs| ty::ExistentialTraitRef { def_id: self.def_id, substs })
348     }
349 }
350
351 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
352     type Lifted = ty::ExistentialPredicate<'tcx>;
353     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
354         match self {
355             ty::ExistentialPredicate::Trait(x) => tcx.lift(x).map(ty::ExistentialPredicate::Trait),
356             ty::ExistentialPredicate::Projection(x) => {
357                 tcx.lift(x).map(ty::ExistentialPredicate::Projection)
358             }
359             ty::ExistentialPredicate::AutoTrait(def_id) => {
360                 Some(ty::ExistentialPredicate::AutoTrait(def_id))
361             }
362         }
363     }
364 }
365
366 impl<'a, 'tcx> Lift<'tcx> for Term<'a> {
367     type Lifted = ty::Term<'tcx>;
368     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
369         Some(match self {
370             Term::Ty(ty) => Term::Ty(tcx.lift(ty)?),
371             Term::Const(c) => Term::Const(tcx.lift(c)?),
372         })
373     }
374 }
375
376 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
377     type Lifted = ty::TraitPredicate<'tcx>;
378     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
379         tcx.lift(self.trait_ref).map(|trait_ref| ty::TraitPredicate {
380             trait_ref,
381             constness: self.constness,
382             polarity: self.polarity,
383         })
384     }
385 }
386
387 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
388     type Lifted = ty::SubtypePredicate<'tcx>;
389     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::SubtypePredicate<'tcx>> {
390         tcx.lift((self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
391             a_is_expected: self.a_is_expected,
392             a,
393             b,
394         })
395     }
396 }
397
398 impl<'a, 'tcx> Lift<'tcx> for ty::CoercePredicate<'a> {
399     type Lifted = ty::CoercePredicate<'tcx>;
400     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::CoercePredicate<'tcx>> {
401         tcx.lift((self.a, self.b)).map(|(a, b)| ty::CoercePredicate { a, b })
402     }
403 }
404
405 impl<'tcx, A: Copy + Lift<'tcx>, B: Copy + Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
406     type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
407     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
408         tcx.lift((self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
409     }
410 }
411
412 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
413     type Lifted = ty::ProjectionTy<'tcx>;
414     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionTy<'tcx>> {
415         tcx.lift(self.substs)
416             .map(|substs| ty::ProjectionTy { item_def_id: self.item_def_id, substs })
417     }
418 }
419
420 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
421     type Lifted = ty::ProjectionPredicate<'tcx>;
422     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
423         tcx.lift((self.projection_ty, self.term))
424             .map(|(projection_ty, term)| ty::ProjectionPredicate { projection_ty, term })
425     }
426 }
427
428 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
429     type Lifted = ty::ExistentialProjection<'tcx>;
430     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
431         tcx.lift(self.substs).map(|substs| ty::ExistentialProjection {
432             substs,
433             term: tcx.lift(self.term).expect("type must lift when substs do"),
434             item_def_id: self.item_def_id,
435         })
436     }
437 }
438
439 impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
440     type Lifted = ty::PredicateKind<'tcx>;
441     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
442         match self {
443             ty::PredicateKind::Trait(data) => tcx.lift(data).map(ty::PredicateKind::Trait),
444             ty::PredicateKind::Subtype(data) => tcx.lift(data).map(ty::PredicateKind::Subtype),
445             ty::PredicateKind::Coerce(data) => tcx.lift(data).map(ty::PredicateKind::Coerce),
446             ty::PredicateKind::RegionOutlives(data) => {
447                 tcx.lift(data).map(ty::PredicateKind::RegionOutlives)
448             }
449             ty::PredicateKind::TypeOutlives(data) => {
450                 tcx.lift(data).map(ty::PredicateKind::TypeOutlives)
451             }
452             ty::PredicateKind::Projection(data) => {
453                 tcx.lift(data).map(ty::PredicateKind::Projection)
454             }
455             ty::PredicateKind::WellFormed(ty) => tcx.lift(ty).map(ty::PredicateKind::WellFormed),
456             ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
457                 tcx.lift(closure_substs).map(|closure_substs| {
458                     ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind)
459                 })
460             }
461             ty::PredicateKind::ObjectSafe(trait_def_id) => {
462                 Some(ty::PredicateKind::ObjectSafe(trait_def_id))
463             }
464             ty::PredicateKind::ConstEvaluatable(uv) => {
465                 tcx.lift(uv).map(|uv| ty::PredicateKind::ConstEvaluatable(uv))
466             }
467             ty::PredicateKind::ConstEquate(c1, c2) => {
468                 tcx.lift((c1, c2)).map(|(c1, c2)| ty::PredicateKind::ConstEquate(c1, c2))
469             }
470             ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
471                 tcx.lift(ty).map(ty::PredicateKind::TypeWellFormedFromEnv)
472             }
473         }
474     }
475 }
476
477 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<'a, T>
478 where
479     <T as Lift<'tcx>>::Lifted: TypeFoldable<'tcx>,
480 {
481     type Lifted = ty::Binder<'tcx, T::Lifted>;
482     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
483         let bound_vars = tcx.lift(self.bound_vars());
484         tcx.lift(self.skip_binder())
485             .zip(bound_vars)
486             .map(|(value, vars)| ty::Binder::bind_with_vars(value, vars))
487     }
488 }
489
490 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
491     type Lifted = ty::ParamEnv<'tcx>;
492     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
493         tcx.lift(self.caller_bounds())
494             .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.constness()))
495     }
496 }
497
498 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
499     type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
500     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
501         tcx.lift(self.param_env).and_then(|param_env| {
502             tcx.lift(self.value).map(|value| ty::ParamEnvAnd { param_env, value })
503         })
504     }
505 }
506
507 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
508     type Lifted = ty::ClosureSubsts<'tcx>;
509     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
510         tcx.lift(self.substs).map(|substs| ty::ClosureSubsts { substs })
511     }
512 }
513
514 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
515     type Lifted = ty::GeneratorSubsts<'tcx>;
516     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
517         tcx.lift(self.substs).map(|substs| ty::GeneratorSubsts { substs })
518     }
519 }
520
521 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
522     type Lifted = ty::adjustment::Adjustment<'tcx>;
523     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
524         let ty::adjustment::Adjustment { kind, target } = self;
525         tcx.lift(kind).and_then(|kind| {
526             tcx.lift(target).map(|target| ty::adjustment::Adjustment { kind, target })
527         })
528     }
529 }
530
531 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
532     type Lifted = ty::adjustment::Adjust<'tcx>;
533     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
534         match self {
535             ty::adjustment::Adjust::NeverToAny => Some(ty::adjustment::Adjust::NeverToAny),
536             ty::adjustment::Adjust::Pointer(ptr) => Some(ty::adjustment::Adjust::Pointer(ptr)),
537             ty::adjustment::Adjust::Deref(overloaded) => {
538                 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
539             }
540             ty::adjustment::Adjust::Borrow(autoref) => {
541                 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
542             }
543         }
544     }
545 }
546
547 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
548     type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
549     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
550         tcx.lift(self.region).map(|region| ty::adjustment::OverloadedDeref {
551             region,
552             mutbl: self.mutbl,
553             span: self.span,
554         })
555     }
556 }
557
558 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
559     type Lifted = ty::adjustment::AutoBorrow<'tcx>;
560     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
561         match self {
562             ty::adjustment::AutoBorrow::Ref(r, m) => {
563                 tcx.lift(r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
564             }
565             ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
566         }
567     }
568 }
569
570 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
571     type Lifted = ty::GenSig<'tcx>;
572     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
573         tcx.lift((self.resume_ty, self.yield_ty, self.return_ty))
574             .map(|(resume_ty, yield_ty, return_ty)| ty::GenSig { resume_ty, yield_ty, return_ty })
575     }
576 }
577
578 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
579     type Lifted = ty::FnSig<'tcx>;
580     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
581         tcx.lift(self.inputs_and_output).map(|x| ty::FnSig {
582             inputs_and_output: x,
583             c_variadic: self.c_variadic,
584             unsafety: self.unsafety,
585             abi: self.abi,
586         })
587     }
588 }
589
590 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
591     type Lifted = ty::error::ExpectedFound<T::Lifted>;
592     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
593         let ty::error::ExpectedFound { expected, found } = self;
594         tcx.lift(expected).and_then(|expected| {
595             tcx.lift(found).map(|found| ty::error::ExpectedFound { expected, found })
596         })
597     }
598 }
599
600 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
601     type Lifted = ty::error::TypeError<'tcx>;
602     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
603         use crate::ty::error::TypeError::*;
604
605         Some(match self {
606             Mismatch => Mismatch,
607             ConstnessMismatch(x) => ConstnessMismatch(x),
608             PolarityMismatch(x) => PolarityMismatch(x),
609             UnsafetyMismatch(x) => UnsafetyMismatch(x),
610             AbiMismatch(x) => AbiMismatch(x),
611             Mutability => Mutability,
612             ArgumentMutability(i) => ArgumentMutability(i),
613             TupleSize(x) => TupleSize(x),
614             FixedArraySize(x) => FixedArraySize(x),
615             ArgCount => ArgCount,
616             FieldMisMatch(x, y) => FieldMisMatch(x, y),
617             RegionsDoesNotOutlive(a, b) => {
618                 return tcx.lift((a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
619             }
620             RegionsInsufficientlyPolymorphic(a, b) => {
621                 return tcx.lift(b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
622             }
623             RegionsOverlyPolymorphic(a, b) => {
624                 return tcx.lift(b).map(|b| RegionsOverlyPolymorphic(a, b));
625             }
626             RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
627             IntMismatch(x) => IntMismatch(x),
628             FloatMismatch(x) => FloatMismatch(x),
629             Traits(x) => Traits(x),
630             VariadicMismatch(x) => VariadicMismatch(x),
631             CyclicTy(t) => return tcx.lift(t).map(|t| CyclicTy(t)),
632             CyclicConst(ct) => return tcx.lift(ct).map(|ct| CyclicConst(ct)),
633             ProjectionMismatched(x) => ProjectionMismatched(x),
634             ArgumentSorts(x, i) => return tcx.lift(x).map(|x| ArgumentSorts(x, i)),
635             Sorts(x) => return tcx.lift(x).map(Sorts),
636             ExistentialMismatch(x) => return tcx.lift(x).map(ExistentialMismatch),
637             ConstMismatch(x) => return tcx.lift(x).map(ConstMismatch),
638             IntrinsicCast => IntrinsicCast,
639             TargetFeatureCast(x) => TargetFeatureCast(x),
640             ObjectUnsafeCoercion(x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
641         })
642     }
643 }
644
645 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
646     type Lifted = ty::InstanceDef<'tcx>;
647     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
648         match self {
649             ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
650             ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
651             ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
652             ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
653             ty::InstanceDef::FnPtrShim(def_id, ty) => {
654                 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
655             }
656             ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
657             ty::InstanceDef::ClosureOnceShim { call_once, track_caller } => {
658                 Some(ty::InstanceDef::ClosureOnceShim { call_once, track_caller })
659             }
660             ty::InstanceDef::DropGlue(def_id, ty) => {
661                 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
662             }
663             ty::InstanceDef::CloneShim(def_id, ty) => {
664                 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
665             }
666         }
667     }
668 }
669
670 ///////////////////////////////////////////////////////////////////////////
671 // TypeFoldable implementations.
672
673 /// AdtDefs are basically the same as a DefId.
674 impl<'tcx> TypeFoldable<'tcx> for ty::AdtDef<'tcx> {
675     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
676         self,
677         _folder: &mut F,
678     ) -> Result<Self, F::Error> {
679         Ok(self)
680     }
681
682     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
683         ControlFlow::CONTINUE
684     }
685 }
686
687 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
688     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
689         self,
690         folder: &mut F,
691     ) -> Result<(T, U), F::Error> {
692         Ok((self.0.try_fold_with(folder)?, self.1.try_fold_with(folder)?))
693     }
694
695     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
696         self.0.visit_with(visitor)?;
697         self.1.visit_with(visitor)
698     }
699 }
700
701 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
702     for (A, B, C)
703 {
704     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
705         self,
706         folder: &mut F,
707     ) -> Result<(A, B, C), F::Error> {
708         Ok((
709             self.0.try_fold_with(folder)?,
710             self.1.try_fold_with(folder)?,
711             self.2.try_fold_with(folder)?,
712         ))
713     }
714
715     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
716         self.0.visit_with(visitor)?;
717         self.1.visit_with(visitor)?;
718         self.2.visit_with(visitor)
719     }
720 }
721
722 EnumTypeFoldableImpl! {
723     impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
724         (Some)(a),
725         (None),
726     } where T: TypeFoldable<'tcx>
727 }
728
729 EnumTypeFoldableImpl! {
730     impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
731         (Ok)(a),
732         (Err)(a),
733     } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
734 }
735
736 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
737     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
738         mut self,
739         folder: &mut F,
740     ) -> Result<Self, F::Error> {
741         // We merely want to replace the contained `T`, if at all possible,
742         // so that we don't needlessly allocate a new `Rc` or indeed clone
743         // the contained type.
744         unsafe {
745             // First step is to ensure that we have a unique reference to
746             // the contained type, which `Rc::make_mut` will accomplish (by
747             // allocating a new `Rc` and cloning the `T` only if required).
748             // This is done *before* casting to `Rc<ManuallyDrop<T>>` so that
749             // panicking during `make_mut` does not leak the `T`.
750             Rc::make_mut(&mut self);
751
752             // Casting to `Rc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
753             // is `repr(transparent)`.
754             let ptr = Rc::into_raw(self).cast::<ManuallyDrop<T>>();
755             let mut unique = Rc::from_raw(ptr);
756
757             // Call to `Rc::make_mut` above guarantees that `unique` is the
758             // sole reference to the contained value, so we can avoid doing
759             // a checked `get_mut` here.
760             let slot = Rc::get_mut_unchecked(&mut unique);
761
762             // Semantically move the contained type out from `unique`, fold
763             // it, then move the folded value back into `unique`.  Should
764             // folding fail, `ManuallyDrop` ensures that the "moved-out"
765             // value is not re-dropped.
766             let owned = ManuallyDrop::take(slot);
767             let folded = owned.try_fold_with(folder)?;
768             *slot = ManuallyDrop::new(folded);
769
770             // Cast back to `Rc<T>`.
771             Ok(Rc::from_raw(Rc::into_raw(unique).cast()))
772         }
773     }
774
775     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
776         (**self).visit_with(visitor)
777     }
778 }
779
780 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
781     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
782         mut self,
783         folder: &mut F,
784     ) -> Result<Self, F::Error> {
785         // We merely want to replace the contained `T`, if at all possible,
786         // so that we don't needlessly allocate a new `Arc` or indeed clone
787         // the contained type.
788         unsafe {
789             // First step is to ensure that we have a unique reference to
790             // the contained type, which `Arc::make_mut` will accomplish (by
791             // allocating a new `Arc` and cloning the `T` only if required).
792             // This is done *before* casting to `Arc<ManuallyDrop<T>>` so that
793             // panicking during `make_mut` does not leak the `T`.
794             Arc::make_mut(&mut self);
795
796             // Casting to `Arc<ManuallyDrop<T>>` is safe because `ManuallyDrop`
797             // is `repr(transparent)`.
798             let ptr = Arc::into_raw(self).cast::<ManuallyDrop<T>>();
799             let mut unique = Arc::from_raw(ptr);
800
801             // Call to `Arc::make_mut` above guarantees that `unique` is the
802             // sole reference to the contained value, so we can avoid doing
803             // a checked `get_mut` here.
804             let slot = Arc::get_mut_unchecked(&mut unique);
805
806             // Semantically move the contained type out from `unique`, fold
807             // it, then move the folded value back into `unique`.  Should
808             // folding fail, `ManuallyDrop` ensures that the "moved-out"
809             // value is not re-dropped.
810             let owned = ManuallyDrop::take(slot);
811             let folded = owned.try_fold_with(folder)?;
812             *slot = ManuallyDrop::new(folded);
813
814             // Cast back to `Arc<T>`.
815             Ok(Arc::from_raw(Arc::into_raw(unique).cast()))
816         }
817     }
818
819     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
820         (**self).visit_with(visitor)
821     }
822 }
823
824 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
825     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
826         self,
827         folder: &mut F,
828     ) -> Result<Self, F::Error> {
829         self.try_map_id(|value| value.try_fold_with(folder))
830     }
831
832     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
833         (**self).visit_with(visitor)
834     }
835 }
836
837 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
838     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
839         self,
840         folder: &mut F,
841     ) -> Result<Self, F::Error> {
842         self.try_map_id(|t| t.try_fold_with(folder))
843     }
844
845     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
846         self.iter().try_for_each(|t| t.visit_with(visitor))
847     }
848 }
849
850 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
851     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
852         self,
853         folder: &mut F,
854     ) -> Result<Self, F::Error> {
855         self.try_map_id(|t| t.try_fold_with(folder))
856     }
857
858     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
859         self.iter().try_for_each(|t| t.visit_with(visitor))
860     }
861 }
862
863 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::EarlyBinder<T> {
864     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
865         self,
866         folder: &mut F,
867     ) -> Result<Self, F::Error> {
868         self.try_map_bound(|ty| ty.try_fold_with(folder))
869     }
870
871     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
872         self.as_ref().0.visit_with(visitor)
873     }
874 }
875
876 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<'tcx, T> {
877     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
878         self,
879         folder: &mut F,
880     ) -> Result<Self, F::Error> {
881         self.try_map_bound(|ty| ty.try_fold_with(folder))
882     }
883
884     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
885         folder.try_fold_binder(self)
886     }
887
888     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
889         self.as_ref().skip_binder().visit_with(visitor)
890     }
891
892     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
893         visitor.visit_binder(self)
894     }
895 }
896
897 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>> {
898     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
899         self,
900         folder: &mut F,
901     ) -> Result<Self, F::Error> {
902         ty::util::fold_list(self, folder, |tcx, v| tcx.intern_poly_existential_predicates(v))
903     }
904
905     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
906         self.iter().try_for_each(|p| p.visit_with(visitor))
907     }
908 }
909
910 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
911     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
912         self,
913         folder: &mut F,
914     ) -> Result<Self, F::Error> {
915         ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projs(v))
916     }
917
918     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
919         self.iter().try_for_each(|t| t.visit_with(visitor))
920     }
921 }
922
923 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
924     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
925         self,
926         folder: &mut F,
927     ) -> Result<Self, F::Error> {
928         use crate::ty::InstanceDef::*;
929         Ok(Self {
930             substs: self.substs.try_fold_with(folder)?,
931             def: match self.def {
932                 Item(def) => Item(def.try_fold_with(folder)?),
933                 VtableShim(did) => VtableShim(did.try_fold_with(folder)?),
934                 ReifyShim(did) => ReifyShim(did.try_fold_with(folder)?),
935                 Intrinsic(did) => Intrinsic(did.try_fold_with(folder)?),
936                 FnPtrShim(did, ty) => {
937                     FnPtrShim(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
938                 }
939                 Virtual(did, i) => Virtual(did.try_fold_with(folder)?, i),
940                 ClosureOnceShim { call_once, track_caller } => {
941                     ClosureOnceShim { call_once: call_once.try_fold_with(folder)?, track_caller }
942                 }
943                 DropGlue(did, ty) => {
944                     DropGlue(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
945                 }
946                 CloneShim(did, ty) => {
947                     CloneShim(did.try_fold_with(folder)?, ty.try_fold_with(folder)?)
948                 }
949             },
950         })
951     }
952
953     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
954         use crate::ty::InstanceDef::*;
955         self.substs.visit_with(visitor)?;
956         match self.def {
957             Item(def) => def.visit_with(visitor),
958             VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
959                 did.visit_with(visitor)
960             }
961             FnPtrShim(did, ty) | CloneShim(did, ty) => {
962                 did.visit_with(visitor)?;
963                 ty.visit_with(visitor)
964             }
965             DropGlue(did, ty) => {
966                 did.visit_with(visitor)?;
967                 ty.visit_with(visitor)
968             }
969             ClosureOnceShim { call_once, track_caller: _ } => call_once.visit_with(visitor),
970         }
971     }
972 }
973
974 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
975     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
976         self,
977         folder: &mut F,
978     ) -> Result<Self, F::Error> {
979         Ok(Self { instance: self.instance.try_fold_with(folder)?, promoted: self.promoted })
980     }
981
982     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
983         self.instance.visit_with(visitor)
984     }
985 }
986
987 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
988     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
989         self,
990         folder: &mut F,
991     ) -> Result<Self, F::Error> {
992         let kind = match *self.kind() {
993             ty::RawPtr(tm) => ty::RawPtr(tm.try_fold_with(folder)?),
994             ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?),
995             ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?),
996             ty::Adt(tid, substs) => ty::Adt(tid, substs.try_fold_with(folder)?),
997             ty::Dynamic(trait_ty, region) => {
998                 ty::Dynamic(trait_ty.try_fold_with(folder)?, region.try_fold_with(folder)?)
999             }
1000             ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
1001             ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.try_fold_with(folder)?),
1002             ty::FnPtr(f) => ty::FnPtr(f.try_fold_with(folder)?),
1003             ty::Ref(r, ty, mutbl) => {
1004                 ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
1005             }
1006             ty::Generator(did, substs, movability) => {
1007                 ty::Generator(did, substs.try_fold_with(folder)?, movability)
1008             }
1009             ty::GeneratorWitness(types) => ty::GeneratorWitness(types.try_fold_with(folder)?),
1010             ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?),
1011             ty::Projection(data) => ty::Projection(data.try_fold_with(folder)?),
1012             ty::Opaque(did, substs) => ty::Opaque(did, substs.try_fold_with(folder)?),
1013
1014             ty::Bool
1015             | ty::Char
1016             | ty::Str
1017             | ty::Int(_)
1018             | ty::Uint(_)
1019             | ty::Float(_)
1020             | ty::Error(_)
1021             | ty::Infer(_)
1022             | ty::Param(..)
1023             | ty::Bound(..)
1024             | ty::Placeholder(..)
1025             | ty::Never
1026             | ty::Foreign(..) => return Ok(self),
1027         };
1028
1029         Ok(if *self.kind() == kind { self } else { folder.tcx().mk_ty(kind) })
1030     }
1031
1032     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1033         folder.try_fold_ty(self)
1034     }
1035
1036     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1037         match self.kind() {
1038             ty::RawPtr(ref tm) => tm.visit_with(visitor),
1039             ty::Array(typ, sz) => {
1040                 typ.visit_with(visitor)?;
1041                 sz.visit_with(visitor)
1042             }
1043             ty::Slice(typ) => typ.visit_with(visitor),
1044             ty::Adt(_, substs) => substs.visit_with(visitor),
1045             ty::Dynamic(ref trait_ty, ref reg) => {
1046                 trait_ty.visit_with(visitor)?;
1047                 reg.visit_with(visitor)
1048             }
1049             ty::Tuple(ts) => ts.visit_with(visitor),
1050             ty::FnDef(_, substs) => substs.visit_with(visitor),
1051             ty::FnPtr(ref f) => f.visit_with(visitor),
1052             ty::Ref(r, ty, _) => {
1053                 r.visit_with(visitor)?;
1054                 ty.visit_with(visitor)
1055             }
1056             ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
1057             ty::GeneratorWitness(ref types) => types.visit_with(visitor),
1058             ty::Closure(_did, ref substs) => substs.visit_with(visitor),
1059             ty::Projection(ref data) => data.visit_with(visitor),
1060             ty::Opaque(_, ref substs) => substs.visit_with(visitor),
1061
1062             ty::Bool
1063             | ty::Char
1064             | ty::Str
1065             | ty::Int(_)
1066             | ty::Uint(_)
1067             | ty::Float(_)
1068             | ty::Error(_)
1069             | ty::Infer(_)
1070             | ty::Bound(..)
1071             | ty::Placeholder(..)
1072             | ty::Param(..)
1073             | ty::Never
1074             | ty::Foreign(..) => ControlFlow::CONTINUE,
1075         }
1076     }
1077
1078     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1079         visitor.visit_ty(*self)
1080     }
1081 }
1082
1083 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
1084     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1085         self,
1086         _folder: &mut F,
1087     ) -> Result<Self, F::Error> {
1088         Ok(self)
1089     }
1090
1091     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1092         folder.try_fold_region(self)
1093     }
1094
1095     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
1096         ControlFlow::CONTINUE
1097     }
1098
1099     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1100         visitor.visit_region(*self)
1101     }
1102 }
1103
1104 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
1105     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1106         folder.try_fold_predicate(self)
1107     }
1108
1109     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1110         self,
1111         folder: &mut F,
1112     ) -> Result<Self, F::Error> {
1113         let new = self.kind().try_fold_with(folder)?;
1114         Ok(folder.tcx().reuse_or_mk_predicate(self, new))
1115     }
1116
1117     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1118         self.kind().visit_with(visitor)
1119     }
1120
1121     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1122         visitor.visit_predicate(*self)
1123     }
1124
1125     fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
1126         self.outer_exclusive_binder() > binder
1127     }
1128
1129     fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
1130         self.flags().intersects(flags)
1131     }
1132 }
1133
1134 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
1135     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1136         self,
1137         folder: &mut F,
1138     ) -> Result<Self, F::Error> {
1139         ty::util::fold_list(self, folder, |tcx, v| tcx.intern_predicates(v))
1140     }
1141
1142     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1143         self.iter().try_for_each(|p| p.visit_with(visitor))
1144     }
1145 }
1146
1147 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
1148     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1149         self,
1150         folder: &mut F,
1151     ) -> Result<Self, F::Error> {
1152         self.try_map_id(|x| x.try_fold_with(folder))
1153     }
1154
1155     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1156         self.iter().try_for_each(|t| t.visit_with(visitor))
1157     }
1158 }
1159
1160 impl<'tcx> TypeFoldable<'tcx> for ty::Const<'tcx> {
1161     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1162         self,
1163         folder: &mut F,
1164     ) -> Result<Self, F::Error> {
1165         let ty = self.ty().try_fold_with(folder)?;
1166         let val = self.val().try_fold_with(folder)?;
1167         if ty != self.ty() || val != self.val() {
1168             Ok(folder.tcx().mk_const(ty::ConstS { ty, val }))
1169         } else {
1170             Ok(self)
1171         }
1172     }
1173
1174     fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
1175         folder.try_fold_const(self)
1176     }
1177
1178     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1179         self.ty().visit_with(visitor)?;
1180         self.val().visit_with(visitor)
1181     }
1182
1183     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1184         visitor.visit_const(*self)
1185     }
1186 }
1187
1188 impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
1189     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1190         self,
1191         folder: &mut F,
1192     ) -> Result<Self, F::Error> {
1193         Ok(match self {
1194             ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.try_fold_with(folder)?),
1195             ty::ConstKind::Param(p) => ty::ConstKind::Param(p.try_fold_with(folder)?),
1196             ty::ConstKind::Unevaluated(uv) => ty::ConstKind::Unevaluated(uv.try_fold_with(folder)?),
1197             ty::ConstKind::Value(_)
1198             | ty::ConstKind::Bound(..)
1199             | ty::ConstKind::Placeholder(..)
1200             | ty::ConstKind::Error(_) => self,
1201         })
1202     }
1203
1204     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1205         match *self {
1206             ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1207             ty::ConstKind::Param(p) => p.visit_with(visitor),
1208             ty::ConstKind::Unevaluated(uv) => uv.visit_with(visitor),
1209             ty::ConstKind::Value(_)
1210             | ty::ConstKind::Bound(..)
1211             | ty::ConstKind::Placeholder(_)
1212             | ty::ConstKind::Error(_) => ControlFlow::CONTINUE,
1213         }
1214     }
1215 }
1216
1217 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1218     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1219         self,
1220         _folder: &mut F,
1221     ) -> Result<Self, F::Error> {
1222         Ok(self)
1223     }
1224
1225     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> ControlFlow<V::BreakTy> {
1226         ControlFlow::CONTINUE
1227     }
1228 }
1229
1230 impl<'tcx> TypeFoldable<'tcx> for ty::Unevaluated<'tcx> {
1231     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1232         self,
1233         folder: &mut F,
1234     ) -> Result<Self, F::Error> {
1235         Ok(ty::Unevaluated {
1236             def: self.def,
1237             substs: self.substs.try_fold_with(folder)?,
1238             promoted: self.promoted,
1239         })
1240     }
1241
1242     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1243         visitor.visit_unevaluated(*self)
1244     }
1245
1246     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1247         self.substs.visit_with(visitor)
1248     }
1249 }
1250
1251 impl<'tcx> TypeFoldable<'tcx> for ty::Unevaluated<'tcx, ()> {
1252     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
1253         self,
1254         folder: &mut F,
1255     ) -> Result<Self, F::Error> {
1256         Ok(ty::Unevaluated {
1257             def: self.def,
1258             substs: self.substs.try_fold_with(folder)?,
1259             promoted: self.promoted,
1260         })
1261     }
1262
1263     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1264         visitor.visit_unevaluated(self.expand())
1265     }
1266
1267     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1268         self.substs.visit_with(visitor)
1269     }
1270 }
1271
1272 impl<'tcx> TypeFoldable<'tcx> for hir::Constness {
1273     fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(self, _: &mut F) -> Result<Self, F::Error> {
1274         Ok(self)
1275     }
1276
1277     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> ControlFlow<V::BreakTy> {
1278         ControlFlow::CONTINUE
1279     }
1280 }