]> git.lizzy.rs Git - rust.git/blob - src/librustc_middle/ty/structural_impls.rs
introduce PredicateAtom
[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::ForAll(binder) => write!(f, "ForAll({:?})", binder),
230             ty::PredicateKind::Atom(atom) => write!(f, "{:?}", atom),
231         }
232     }
233 }
234
235 impl fmt::Debug for ty::PredicateAtom<'tcx> {
236     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
237         match *self {
238             ty::PredicateAtom::Trait(ref a, constness) => {
239                 if let hir::Constness::Const = constness {
240                     write!(f, "const ")?;
241                 }
242                 a.fmt(f)
243             }
244             ty::PredicateAtom::Subtype(ref pair) => pair.fmt(f),
245             ty::PredicateAtom::RegionOutlives(ref pair) => pair.fmt(f),
246             ty::PredicateAtom::TypeOutlives(ref pair) => pair.fmt(f),
247             ty::PredicateAtom::Projection(ref pair) => pair.fmt(f),
248             ty::PredicateAtom::WellFormed(data) => write!(f, "WellFormed({:?})", data),
249             ty::PredicateAtom::ObjectSafe(trait_def_id) => {
250                 write!(f, "ObjectSafe({:?})", trait_def_id)
251             }
252             ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind) => {
253                 write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
254             }
255             ty::PredicateAtom::ConstEvaluatable(def_id, substs) => {
256                 write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs)
257             }
258             ty::PredicateAtom::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
259         }
260     }
261 }
262
263 ///////////////////////////////////////////////////////////////////////////
264 // Atomic structs
265 //
266 // For things that don't carry any arena-allocated data (and are
267 // copy...), just add them to this list.
268
269 CloneTypeFoldableAndLiftImpls! {
270     (),
271     bool,
272     usize,
273     ::rustc_target::abi::VariantIdx,
274     u64,
275     String,
276     crate::middle::region::Scope,
277     ::rustc_ast::ast::FloatTy,
278     ::rustc_ast::ast::InlineAsmOptions,
279     ::rustc_ast::ast::InlineAsmTemplatePiece,
280     ::rustc_ast::ast::NodeId,
281     ::rustc_span::symbol::Symbol,
282     ::rustc_hir::def::Res,
283     ::rustc_hir::def_id::DefId,
284     ::rustc_hir::def_id::LocalDefId,
285     ::rustc_hir::LlvmInlineAsmInner,
286     ::rustc_hir::MatchSource,
287     ::rustc_hir::Mutability,
288     ::rustc_hir::Unsafety,
289     ::rustc_target::asm::InlineAsmRegOrRegClass,
290     ::rustc_target::spec::abi::Abi,
291     crate::mir::Local,
292     crate::mir::Promoted,
293     crate::traits::Reveal,
294     crate::ty::adjustment::AutoBorrowMutability,
295     crate::ty::AdtKind,
296     // Including `BoundRegion` is a *bit* dubious, but direct
297     // references to bound region appear in `ty::Error`, and aren't
298     // really meant to be folded. In general, we can only fold a fully
299     // general `Region`.
300     crate::ty::BoundRegion,
301     crate::ty::Placeholder<crate::ty::BoundRegion>,
302     crate::ty::ClosureKind,
303     crate::ty::FreeRegion,
304     crate::ty::InferTy,
305     crate::ty::IntVarValue,
306     crate::ty::ParamConst,
307     crate::ty::ParamTy,
308     crate::ty::adjustment::PointerCast,
309     crate::ty::RegionVid,
310     crate::ty::UniverseIndex,
311     crate::ty::Variance,
312     ::rustc_span::Span,
313 }
314
315 ///////////////////////////////////////////////////////////////////////////
316 // Lift implementations
317
318 // FIXME(eddyb) replace all the uses of `Option::map` with `?`.
319 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
320     type Lifted = (A::Lifted, B::Lifted);
321     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
322         tcx.lift(&self.0).and_then(|a| tcx.lift(&self.1).map(|b| (a, b)))
323     }
324 }
325
326 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
327     type Lifted = (A::Lifted, B::Lifted, C::Lifted);
328     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
329         tcx.lift(&self.0)
330             .and_then(|a| tcx.lift(&self.1).and_then(|b| tcx.lift(&self.2).map(|c| (a, b, c))))
331     }
332 }
333
334 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
335     type Lifted = Option<T::Lifted>;
336     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
337         match *self {
338             Some(ref x) => tcx.lift(x).map(Some),
339             None => Some(None),
340         }
341     }
342 }
343
344 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
345     type Lifted = Result<T::Lifted, E::Lifted>;
346     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
347         match *self {
348             Ok(ref x) => tcx.lift(x).map(Ok),
349             Err(ref e) => tcx.lift(e).map(Err),
350         }
351     }
352 }
353
354 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
355     type Lifted = Box<T::Lifted>;
356     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
357         tcx.lift(&**self).map(Box::new)
358     }
359 }
360
361 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Rc<T> {
362     type Lifted = Rc<T::Lifted>;
363     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
364         tcx.lift(&**self).map(Rc::new)
365     }
366 }
367
368 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Arc<T> {
369     type Lifted = Arc<T::Lifted>;
370     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
371         tcx.lift(&**self).map(Arc::new)
372     }
373 }
374
375 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for [T] {
376     type Lifted = Vec<T::Lifted>;
377     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
378         // type annotation needed to inform `projection_must_outlive`
379         let mut result: Vec<<T as Lift<'tcx>>::Lifted> = Vec::with_capacity(self.len());
380         for x in self {
381             if let Some(value) = tcx.lift(x) {
382                 result.push(value);
383             } else {
384                 return None;
385             }
386         }
387         Some(result)
388     }
389 }
390
391 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
392     type Lifted = Vec<T::Lifted>;
393     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
394         tcx.lift(&self[..])
395     }
396 }
397
398 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
399     type Lifted = IndexVec<I, T::Lifted>;
400     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
401         self.iter().map(|e| tcx.lift(e)).collect()
402     }
403 }
404
405 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
406     type Lifted = ty::TraitRef<'tcx>;
407     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
408         tcx.lift(&self.substs).map(|substs| ty::TraitRef { def_id: self.def_id, substs })
409     }
410 }
411
412 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
413     type Lifted = ty::ExistentialTraitRef<'tcx>;
414     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
415         tcx.lift(&self.substs).map(|substs| ty::ExistentialTraitRef { def_id: self.def_id, substs })
416     }
417 }
418
419 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialPredicate<'a> {
420     type Lifted = ty::ExistentialPredicate<'tcx>;
421     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
422         match self {
423             ty::ExistentialPredicate::Trait(x) => tcx.lift(x).map(ty::ExistentialPredicate::Trait),
424             ty::ExistentialPredicate::Projection(x) => {
425                 tcx.lift(x).map(ty::ExistentialPredicate::Projection)
426             }
427             ty::ExistentialPredicate::AutoTrait(def_id) => {
428                 Some(ty::ExistentialPredicate::AutoTrait(*def_id))
429             }
430         }
431     }
432 }
433
434 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
435     type Lifted = ty::TraitPredicate<'tcx>;
436     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
437         tcx.lift(&self.trait_ref).map(|trait_ref| ty::TraitPredicate { trait_ref })
438     }
439 }
440
441 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
442     type Lifted = ty::SubtypePredicate<'tcx>;
443     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::SubtypePredicate<'tcx>> {
444         tcx.lift(&(self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
445             a_is_expected: self.a_is_expected,
446             a,
447             b,
448         })
449     }
450 }
451
452 impl<'tcx, A: Copy + Lift<'tcx>, B: Copy + Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
453     type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
454     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
455         tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
456     }
457 }
458
459 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
460     type Lifted = ty::ProjectionTy<'tcx>;
461     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionTy<'tcx>> {
462         tcx.lift(&self.substs)
463             .map(|substs| ty::ProjectionTy { item_def_id: self.item_def_id, substs })
464     }
465 }
466
467 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
468     type Lifted = ty::ProjectionPredicate<'tcx>;
469     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
470         tcx.lift(&(self.projection_ty, self.ty))
471             .map(|(projection_ty, ty)| ty::ProjectionPredicate { projection_ty, ty })
472     }
473 }
474
475 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
476     type Lifted = ty::ExistentialProjection<'tcx>;
477     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
478         tcx.lift(&self.substs).map(|substs| ty::ExistentialProjection {
479             substs,
480             ty: tcx.lift(&self.ty).expect("type must lift when substs do"),
481             item_def_id: self.item_def_id,
482         })
483     }
484 }
485
486 impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
487     type Lifted = ty::PredicateKind<'tcx>;
488     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
489         match *self {
490             ty::PredicateKind::ForAll(ref binder) => {
491                 tcx.lift(binder).map(ty::PredicateKind::ForAll)
492             }
493             ty::PredicateKind::Atom(ref atom) => tcx.lift(atom).map(ty::PredicateKind::Atom),
494         }
495     }
496 }
497
498 impl<'a, 'tcx> Lift<'tcx> for ty::PredicateAtom<'a> {
499     type Lifted = ty::PredicateAtom<'tcx>;
500     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
501         match *self {
502             ty::PredicateAtom::Trait(ref data, constness) => {
503                 tcx.lift(data).map(|data| ty::PredicateAtom::Trait(data, constness))
504             }
505             ty::PredicateAtom::Subtype(ref data) => tcx.lift(data).map(ty::PredicateAtom::Subtype),
506             ty::PredicateAtom::RegionOutlives(ref data) => {
507                 tcx.lift(data).map(ty::PredicateAtom::RegionOutlives)
508             }
509             ty::PredicateAtom::TypeOutlives(ref data) => {
510                 tcx.lift(data).map(ty::PredicateAtom::TypeOutlives)
511             }
512             ty::PredicateAtom::Projection(ref data) => {
513                 tcx.lift(data).map(ty::PredicateAtom::Projection)
514             }
515             ty::PredicateAtom::WellFormed(ty) => tcx.lift(&ty).map(ty::PredicateAtom::WellFormed),
516             ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind) => {
517                 tcx.lift(&closure_substs).map(|closure_substs| {
518                     ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind)
519                 })
520             }
521             ty::PredicateAtom::ObjectSafe(trait_def_id) => {
522                 Some(ty::PredicateAtom::ObjectSafe(trait_def_id))
523             }
524             ty::PredicateAtom::ConstEvaluatable(def_id, substs) => {
525                 tcx.lift(&substs).map(|substs| ty::PredicateAtom::ConstEvaluatable(def_id, substs))
526             }
527             ty::PredicateAtom::ConstEquate(c1, c2) => {
528                 tcx.lift(&(c1, c2)).map(|(c1, c2)| ty::PredicateAtom::ConstEquate(c1, c2))
529             }
530         }
531     }
532 }
533
534 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
535     type Lifted = ty::Binder<T::Lifted>;
536     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
537         tcx.lift(self.as_ref().skip_binder()).map(ty::Binder::bind)
538     }
539 }
540
541 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
542     type Lifted = ty::ParamEnv<'tcx>;
543     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
544         tcx.lift(&self.caller_bounds())
545             .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.def_id))
546     }
547 }
548
549 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
550     type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
551     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
552         tcx.lift(&self.param_env).and_then(|param_env| {
553             tcx.lift(&self.value).map(|value| ty::ParamEnvAnd { param_env, value })
554         })
555     }
556 }
557
558 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
559     type Lifted = ty::ClosureSubsts<'tcx>;
560     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
561         tcx.lift(&self.substs).map(|substs| ty::ClosureSubsts { substs })
562     }
563 }
564
565 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
566     type Lifted = ty::GeneratorSubsts<'tcx>;
567     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
568         tcx.lift(&self.substs).map(|substs| ty::GeneratorSubsts { substs })
569     }
570 }
571
572 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
573     type Lifted = ty::adjustment::Adjustment<'tcx>;
574     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
575         tcx.lift(&self.kind).and_then(|kind| {
576             tcx.lift(&self.target).map(|target| ty::adjustment::Adjustment { kind, target })
577         })
578     }
579 }
580
581 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
582     type Lifted = ty::adjustment::Adjust<'tcx>;
583     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
584         match *self {
585             ty::adjustment::Adjust::NeverToAny => Some(ty::adjustment::Adjust::NeverToAny),
586             ty::adjustment::Adjust::Pointer(ptr) => Some(ty::adjustment::Adjust::Pointer(ptr)),
587             ty::adjustment::Adjust::Deref(ref overloaded) => {
588                 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
589             }
590             ty::adjustment::Adjust::Borrow(ref autoref) => {
591                 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
592             }
593         }
594     }
595 }
596
597 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
598     type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
599     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
600         tcx.lift(&self.region)
601             .map(|region| ty::adjustment::OverloadedDeref { region, mutbl: self.mutbl })
602     }
603 }
604
605 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
606     type Lifted = ty::adjustment::AutoBorrow<'tcx>;
607     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
608         match *self {
609             ty::adjustment::AutoBorrow::Ref(r, m) => {
610                 tcx.lift(&r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
611             }
612             ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
613         }
614     }
615 }
616
617 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
618     type Lifted = ty::GenSig<'tcx>;
619     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
620         tcx.lift(&(self.resume_ty, self.yield_ty, self.return_ty))
621             .map(|(resume_ty, yield_ty, return_ty)| ty::GenSig { resume_ty, yield_ty, return_ty })
622     }
623 }
624
625 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
626     type Lifted = ty::FnSig<'tcx>;
627     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
628         tcx.lift(&self.inputs_and_output).map(|x| ty::FnSig {
629             inputs_and_output: x,
630             c_variadic: self.c_variadic,
631             unsafety: self.unsafety,
632             abi: self.abi,
633         })
634     }
635 }
636
637 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
638     type Lifted = ty::error::ExpectedFound<T::Lifted>;
639     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
640         tcx.lift(&self.expected).and_then(|expected| {
641             tcx.lift(&self.found).map(|found| ty::error::ExpectedFound { expected, found })
642         })
643     }
644 }
645
646 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
647     type Lifted = ty::error::TypeError<'tcx>;
648     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
649         use crate::ty::error::TypeError::*;
650
651         Some(match *self {
652             Mismatch => Mismatch,
653             UnsafetyMismatch(x) => UnsafetyMismatch(x),
654             AbiMismatch(x) => AbiMismatch(x),
655             Mutability => Mutability,
656             TupleSize(x) => TupleSize(x),
657             FixedArraySize(x) => FixedArraySize(x),
658             ArgCount => ArgCount,
659             RegionsDoesNotOutlive(a, b) => {
660                 return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
661             }
662             RegionsInsufficientlyPolymorphic(a, b) => {
663                 return tcx.lift(&b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
664             }
665             RegionsOverlyPolymorphic(a, b) => {
666                 return tcx.lift(&b).map(|b| RegionsOverlyPolymorphic(a, b));
667             }
668             RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
669             IntMismatch(x) => IntMismatch(x),
670             FloatMismatch(x) => FloatMismatch(x),
671             Traits(x) => Traits(x),
672             VariadicMismatch(x) => VariadicMismatch(x),
673             CyclicTy(t) => return tcx.lift(&t).map(|t| CyclicTy(t)),
674             ProjectionMismatched(x) => ProjectionMismatched(x),
675             Sorts(ref x) => return tcx.lift(x).map(Sorts),
676             ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch),
677             ConstMismatch(ref x) => return tcx.lift(x).map(ConstMismatch),
678             IntrinsicCast => IntrinsicCast,
679             TargetFeatureCast(ref x) => TargetFeatureCast(*x),
680             ObjectUnsafeCoercion(ref x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
681         })
682     }
683 }
684
685 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
686     type Lifted = ty::InstanceDef<'tcx>;
687     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
688         match *self {
689             ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
690             ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
691             ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
692             ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
693             ty::InstanceDef::FnPtrShim(def_id, ref ty) => {
694                 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
695             }
696             ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
697             ty::InstanceDef::ClosureOnceShim { call_once } => {
698                 Some(ty::InstanceDef::ClosureOnceShim { call_once })
699             }
700             ty::InstanceDef::DropGlue(def_id, ref ty) => {
701                 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
702             }
703             ty::InstanceDef::CloneShim(def_id, ref ty) => {
704                 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
705             }
706         }
707     }
708 }
709
710 ///////////////////////////////////////////////////////////////////////////
711 // TypeFoldable implementations.
712 //
713 // Ideally, each type should invoke `folder.fold_foo(self)` and
714 // nothing else. In some cases, though, we haven't gotten around to
715 // adding methods on the `folder` yet, and thus the folding is
716 // hard-coded here. This is less-flexible, because folders cannot
717 // override the behavior, but there are a lot of random types and one
718 // can easily refactor the folding into the TypeFolder trait as
719 // needed.
720
721 /// AdtDefs are basically the same as a DefId.
722 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
723     fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
724         *self
725     }
726
727     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
728         false
729     }
730 }
731
732 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
733     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (T, U) {
734         (self.0.fold_with(folder), self.1.fold_with(folder))
735     }
736
737     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
738         self.0.visit_with(visitor) || self.1.visit_with(visitor)
739     }
740 }
741
742 impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
743     for (A, B, C)
744 {
745     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (A, B, C) {
746         (self.0.fold_with(folder), self.1.fold_with(folder), self.2.fold_with(folder))
747     }
748
749     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
750         self.0.visit_with(visitor) || self.1.visit_with(visitor) || self.2.visit_with(visitor)
751     }
752 }
753
754 EnumTypeFoldableImpl! {
755     impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
756         (Some)(a),
757         (None),
758     } where T: TypeFoldable<'tcx>
759 }
760
761 EnumTypeFoldableImpl! {
762     impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
763         (Ok)(a),
764         (Err)(a),
765     } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
766 }
767
768 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
769     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
770         Rc::new((**self).fold_with(folder))
771     }
772
773     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
774         (**self).visit_with(visitor)
775     }
776 }
777
778 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
779     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
780         Arc::new((**self).fold_with(folder))
781     }
782
783     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
784         (**self).visit_with(visitor)
785     }
786 }
787
788 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
789     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
790         let content: T = (**self).fold_with(folder);
791         box content
792     }
793
794     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
795         (**self).visit_with(visitor)
796     }
797 }
798
799 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
800     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
801         self.iter().map(|t| t.fold_with(folder)).collect()
802     }
803
804     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
805         self.iter().any(|t| t.visit_with(visitor))
806     }
807 }
808
809 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
810     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
811         self.iter().map(|t| t.fold_with(folder)).collect::<Vec<_>>().into_boxed_slice()
812     }
813
814     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
815         self.iter().any(|t| t.visit_with(visitor))
816     }
817 }
818
819 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
820     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
821         self.map_bound_ref(|ty| ty.fold_with(folder))
822     }
823
824     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
825         folder.fold_binder(self)
826     }
827
828     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
829         self.as_ref().skip_binder().visit_with(visitor)
830     }
831
832     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
833         visitor.visit_binder(self)
834     }
835 }
836
837 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
838     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
839         fold_list(*self, folder, |tcx, v| tcx.intern_existential_predicates(v))
840     }
841
842     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
843         self.iter().any(|p| p.visit_with(visitor))
844     }
845 }
846
847 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
848     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
849         fold_list(*self, folder, |tcx, v| tcx.intern_type_list(v))
850     }
851
852     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
853         self.iter().any(|t| t.visit_with(visitor))
854     }
855 }
856
857 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
858     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
859         fold_list(*self, folder, |tcx, v| tcx.intern_projs(v))
860     }
861
862     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
863         self.iter().any(|t| t.visit_with(visitor))
864     }
865 }
866
867 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
868     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
869         use crate::ty::InstanceDef::*;
870         Self {
871             substs: self.substs.fold_with(folder),
872             def: match self.def {
873                 Item(def) => Item(def.fold_with(folder)),
874                 VtableShim(did) => VtableShim(did.fold_with(folder)),
875                 ReifyShim(did) => ReifyShim(did.fold_with(folder)),
876                 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
877                 FnPtrShim(did, ty) => FnPtrShim(did.fold_with(folder), ty.fold_with(folder)),
878                 Virtual(did, i) => Virtual(did.fold_with(folder), i),
879                 ClosureOnceShim { call_once } => {
880                     ClosureOnceShim { call_once: call_once.fold_with(folder) }
881                 }
882                 DropGlue(did, ty) => DropGlue(did.fold_with(folder), ty.fold_with(folder)),
883                 CloneShim(did, ty) => CloneShim(did.fold_with(folder), ty.fold_with(folder)),
884             },
885         }
886     }
887
888     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
889         use crate::ty::InstanceDef::*;
890         self.substs.visit_with(visitor)
891             || match self.def {
892                 Item(def) => def.visit_with(visitor),
893                 VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
894                     did.visit_with(visitor)
895                 }
896                 FnPtrShim(did, ty) | CloneShim(did, ty) => {
897                     did.visit_with(visitor) || ty.visit_with(visitor)
898                 }
899                 DropGlue(did, ty) => did.visit_with(visitor) || ty.visit_with(visitor),
900                 ClosureOnceShim { call_once } => call_once.visit_with(visitor),
901             }
902     }
903 }
904
905 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
906     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
907         Self { instance: self.instance.fold_with(folder), promoted: self.promoted }
908     }
909
910     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
911         self.instance.visit_with(visitor)
912     }
913 }
914
915 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
916     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
917         let kind = match self.kind {
918             ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
919             ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
920             ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
921             ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
922             ty::Dynamic(ref trait_ty, ref region) => {
923                 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
924             }
925             ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
926             ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.fold_with(folder)),
927             ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
928             ty::Ref(ref r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl),
929             ty::Generator(did, substs, movability) => {
930                 ty::Generator(did, substs.fold_with(folder), movability)
931             }
932             ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
933             ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
934             ty::Projection(ref data) => ty::Projection(data.fold_with(folder)),
935             ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
936
937             ty::Bool
938             | ty::Char
939             | ty::Str
940             | ty::Int(_)
941             | ty::Uint(_)
942             | ty::Float(_)
943             | ty::Error(_)
944             | ty::Infer(_)
945             | ty::Param(..)
946             | ty::Bound(..)
947             | ty::Placeholder(..)
948             | ty::Never
949             | ty::Foreign(..) => return self,
950         };
951
952         if self.kind == kind { self } else { folder.tcx().mk_ty(kind) }
953     }
954
955     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
956         folder.fold_ty(*self)
957     }
958
959     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
960         match self.kind {
961             ty::RawPtr(ref tm) => tm.visit_with(visitor),
962             ty::Array(typ, sz) => typ.visit_with(visitor) || sz.visit_with(visitor),
963             ty::Slice(typ) => typ.visit_with(visitor),
964             ty::Adt(_, substs) => substs.visit_with(visitor),
965             ty::Dynamic(ref trait_ty, ref reg) => {
966                 trait_ty.visit_with(visitor) || reg.visit_with(visitor)
967             }
968             ty::Tuple(ts) => ts.visit_with(visitor),
969             ty::FnDef(_, substs) => substs.visit_with(visitor),
970             ty::FnPtr(ref f) => f.visit_with(visitor),
971             ty::Ref(r, ty, _) => r.visit_with(visitor) || ty.visit_with(visitor),
972             ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
973             ty::GeneratorWitness(ref types) => types.visit_with(visitor),
974             ty::Closure(_did, ref substs) => substs.visit_with(visitor),
975             ty::Projection(ref data) => data.visit_with(visitor),
976             ty::Opaque(_, ref substs) => substs.visit_with(visitor),
977
978             ty::Bool
979             | ty::Char
980             | ty::Str
981             | ty::Int(_)
982             | ty::Uint(_)
983             | ty::Float(_)
984             | ty::Error(_)
985             | ty::Infer(_)
986             | ty::Bound(..)
987             | ty::Placeholder(..)
988             | ty::Param(..)
989             | ty::Never
990             | ty::Foreign(..) => false,
991         }
992     }
993
994     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
995         visitor.visit_ty(self)
996     }
997 }
998
999 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
1000     fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
1001         *self
1002     }
1003
1004     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1005         folder.fold_region(*self)
1006     }
1007
1008     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
1009         false
1010     }
1011
1012     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1013         visitor.visit_region(*self)
1014     }
1015 }
1016
1017 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
1018     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1019         let new = ty::PredicateKind::super_fold_with(&self.inner.kind, folder);
1020         folder.tcx().reuse_or_mk_predicate(*self, new)
1021     }
1022
1023     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1024         ty::PredicateKind::super_visit_with(&self.inner.kind, visitor)
1025     }
1026
1027     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1028         visitor.visit_predicate(*self)
1029     }
1030
1031     fn has_vars_bound_at_or_above(&self, binder: ty::DebruijnIndex) -> bool {
1032         self.inner.outer_exclusive_binder > binder
1033     }
1034
1035     fn has_type_flags(&self, flags: ty::TypeFlags) -> bool {
1036         self.inner.flags.intersects(flags)
1037     }
1038 }
1039
1040 pub(super) trait PredicateVisitor<'tcx>: TypeVisitor<'tcx> {
1041     fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> bool;
1042 }
1043
1044 impl<T: TypeVisitor<'tcx>> PredicateVisitor<'tcx> for T {
1045     default fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> bool {
1046         predicate.super_visit_with(self)
1047     }
1048 }
1049
1050 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
1051     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1052         fold_list(*self, folder, |tcx, v| tcx.intern_predicates(v))
1053     }
1054
1055     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1056         self.iter().any(|p| p.visit_with(visitor))
1057     }
1058 }
1059
1060 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
1061     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1062         self.iter().map(|x| x.fold_with(folder)).collect()
1063     }
1064
1065     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1066         self.iter().any(|t| t.visit_with(visitor))
1067     }
1068 }
1069
1070 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1071     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1072         let ty = self.ty.fold_with(folder);
1073         let val = self.val.fold_with(folder);
1074         if ty != self.ty || val != self.val {
1075             folder.tcx().mk_const(ty::Const { ty, val })
1076         } else {
1077             *self
1078         }
1079     }
1080
1081     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1082         folder.fold_const(*self)
1083     }
1084
1085     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1086         self.ty.visit_with(visitor) || self.val.visit_with(visitor)
1087     }
1088
1089     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1090         visitor.visit_const(self)
1091     }
1092 }
1093
1094 impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
1095     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1096         match *self {
1097             ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.fold_with(folder)),
1098             ty::ConstKind::Param(p) => ty::ConstKind::Param(p.fold_with(folder)),
1099             ty::ConstKind::Unevaluated(did, substs, promoted) => {
1100                 ty::ConstKind::Unevaluated(did, substs.fold_with(folder), promoted)
1101             }
1102             ty::ConstKind::Value(_)
1103             | ty::ConstKind::Bound(..)
1104             | ty::ConstKind::Placeholder(..)
1105             | ty::ConstKind::Error(_) => *self,
1106         }
1107     }
1108
1109     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1110         match *self {
1111             ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1112             ty::ConstKind::Param(p) => p.visit_with(visitor),
1113             ty::ConstKind::Unevaluated(_, substs, _) => substs.visit_with(visitor),
1114             ty::ConstKind::Value(_)
1115             | ty::ConstKind::Bound(..)
1116             | ty::ConstKind::Placeholder(_)
1117             | ty::ConstKind::Error(_) => false,
1118         }
1119     }
1120 }
1121
1122 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1123     fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
1124         *self
1125     }
1126
1127     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
1128         false
1129     }
1130 }
1131
1132 // Does the equivalent of
1133 // ```
1134 // let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
1135 // folder.tcx().intern_*(&v)
1136 // ```
1137 fn fold_list<'tcx, F, T>(
1138     list: &'tcx ty::List<T>,
1139     folder: &mut F,
1140     intern: impl FnOnce(TyCtxt<'tcx>, &[T]) -> &'tcx ty::List<T>,
1141 ) -> &'tcx ty::List<T>
1142 where
1143     F: TypeFolder<'tcx>,
1144     T: TypeFoldable<'tcx> + PartialEq + Copy,
1145 {
1146     let mut iter = list.iter();
1147     // Look for the first element that changed
1148     if let Some((i, new_t)) = iter.by_ref().enumerate().find_map(|(i, t)| {
1149         let new_t = t.fold_with(folder);
1150         if new_t == t { None } else { Some((i, new_t)) }
1151     }) {
1152         // An element changed, prepare to intern the resulting list
1153         let mut new_list = SmallVec::<[_; 8]>::with_capacity(list.len());
1154         new_list.extend_from_slice(&list[..i]);
1155         new_list.push(new_t);
1156         new_list.extend(iter.map(|t| t.fold_with(folder)));
1157         intern(folder.tcx(), &new_list)
1158     } else {
1159         list
1160     }
1161 }