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