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