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