]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/structural_impls.rs
Rollup merge of #67908 - ollie27:rustdoc_const_html_escape, r=GuillaumeGomez
[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
19 impl fmt::Debug for ty::GenericParamDef {
20     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21         let type_name = match self.kind {
22             ty::GenericParamDefKind::Lifetime => "Lifetime",
23             ty::GenericParamDefKind::Type { .. } => "Type",
24             ty::GenericParamDefKind::Const => "Const",
25         };
26         write!(f, "{}({}, {:?}, {})", type_name, self.name, self.def_id, self.index)
27     }
28 }
29
30 impl fmt::Debug for ty::TraitDef {
31     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32         ty::tls::with(|tcx| {
33             FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.def_id, &[])?;
34             Ok(())
35         })
36     }
37 }
38
39 impl fmt::Debug for ty::AdtDef {
40     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41         ty::tls::with(|tcx| {
42             FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.did, &[])?;
43             Ok(())
44         })
45     }
46 }
47
48 impl fmt::Debug for ty::ClosureUpvar<'tcx> {
49     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50         write!(f, "ClosureUpvar({:?},{:?})", self.res, self.ty)
51     }
52 }
53
54 impl fmt::Debug for ty::UpvarId {
55     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56         let name = ty::tls::with(|tcx| tcx.hir().name(self.var_path.hir_id));
57         write!(f, "UpvarId({:?};`{}`;{:?})", self.var_path.hir_id, name, self.closure_expr_id)
58     }
59 }
60
61 impl fmt::Debug for ty::UpvarBorrow<'tcx> {
62     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63         write!(f, "UpvarBorrow({:?}, {:?})", self.kind, self.region)
64     }
65 }
66
67 impl fmt::Debug for ty::ExistentialTraitRef<'tcx> {
68     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69         fmt::Display::fmt(self, f)
70     }
71 }
72
73 impl fmt::Debug for ty::adjustment::Adjustment<'tcx> {
74     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75         write!(f, "{:?} -> {}", self.kind, self.target)
76     }
77 }
78
79 impl fmt::Debug for ty::BoundRegion {
80     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81         match *self {
82             ty::BrAnon(n) => write!(f, "BrAnon({:?})", n),
83             ty::BrNamed(did, name) => {
84                 if did.index == CRATE_DEF_INDEX {
85                     write!(f, "BrNamed({})", name)
86                 } else {
87                     write!(f, "BrNamed({:?}, {})", did, name)
88                 }
89             }
90             ty::BrEnv => write!(f, "BrEnv"),
91         }
92     }
93 }
94
95 impl fmt::Debug for ty::RegionKind {
96     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97         match *self {
98             ty::ReEarlyBound(ref data) => write!(f, "ReEarlyBound({}, {})", data.index, data.name),
99
100             ty::ReClosureBound(ref vid) => write!(f, "ReClosureBound({:?})", vid),
101
102             ty::ReLateBound(binder_id, ref bound_region) => {
103                 write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
104             }
105
106             ty::ReFree(ref fr) => fr.fmt(f),
107
108             ty::ReScope(id) => write!(f, "ReScope({:?})", id),
109
110             ty::ReStatic => write!(f, "ReStatic"),
111
112             ty::ReVar(ref vid) => vid.fmt(f),
113
114             ty::RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),
115
116             ty::ReEmpty => write!(f, "ReEmpty"),
117
118             ty::ReErased => write!(f, "ReErased"),
119         }
120     }
121 }
122
123 impl fmt::Debug for ty::FreeRegion {
124     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125         write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region)
126     }
127 }
128
129 impl fmt::Debug for ty::Variance {
130     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131         f.write_str(match *self {
132             ty::Covariant => "+",
133             ty::Contravariant => "-",
134             ty::Invariant => "o",
135             ty::Bivariant => "*",
136         })
137     }
138 }
139
140 impl fmt::Debug for ty::FnSig<'tcx> {
141     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142         write!(f, "({:?}; c_variadic: {})->{:?}", self.inputs(), self.c_variadic, self.output())
143     }
144 }
145
146 impl fmt::Debug for ty::TyVid {
147     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148         write!(f, "_#{}t", self.index)
149     }
150 }
151
152 impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
153     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154         write!(f, "_#{}c", self.index)
155     }
156 }
157
158 impl fmt::Debug for ty::IntVid {
159     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160         write!(f, "_#{}i", self.index)
161     }
162 }
163
164 impl fmt::Debug for ty::FloatVid {
165     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
166         write!(f, "_#{}f", self.index)
167     }
168 }
169
170 impl fmt::Debug for ty::RegionVid {
171     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172         write!(f, "'_#{}r", self.index())
173     }
174 }
175
176 impl fmt::Debug for ty::InferTy {
177     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178         match *self {
179             ty::TyVar(ref v) => v.fmt(f),
180             ty::IntVar(ref v) => v.fmt(f),
181             ty::FloatVar(ref v) => v.fmt(f),
182             ty::FreshTy(v) => write!(f, "FreshTy({:?})", v),
183             ty::FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
184             ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v),
185         }
186     }
187 }
188
189 impl fmt::Debug for ty::IntVarValue {
190     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191         match *self {
192             ty::IntType(ref v) => v.fmt(f),
193             ty::UintType(ref v) => v.fmt(f),
194         }
195     }
196 }
197
198 impl fmt::Debug for ty::FloatVarValue {
199     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200         self.0.fmt(f)
201     }
202 }
203
204 impl fmt::Debug for ty::TraitRef<'tcx> {
205     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
206         fmt::Display::fmt(self, f)
207     }
208 }
209
210 impl fmt::Debug for Ty<'tcx> {
211     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212         fmt::Display::fmt(self, f)
213     }
214 }
215
216 impl fmt::Debug for ty::ParamTy {
217     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218         write!(f, "{}/#{}", self.name, self.index)
219     }
220 }
221
222 impl fmt::Debug for ty::ParamConst {
223     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
224         write!(f, "{}/#{}", self.name, self.index)
225     }
226 }
227
228 impl fmt::Debug for ty::TraitPredicate<'tcx> {
229     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
230         write!(f, "TraitPredicate({:?})", self.trait_ref)
231     }
232 }
233
234 impl fmt::Debug for ty::ProjectionPredicate<'tcx> {
235     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
236         write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty)
237     }
238 }
239
240 impl fmt::Debug for ty::Predicate<'tcx> {
241     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
242         match *self {
243             ty::Predicate::Trait(ref a) => a.fmt(f),
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) => tcx.lift(binder).map(ty::Predicate::Trait),
484             ty::Predicate::Subtype(ref binder) => tcx.lift(binder).map(ty::Predicate::Subtype),
485             ty::Predicate::RegionOutlives(ref binder) => {
486                 tcx.lift(binder).map(ty::Predicate::RegionOutlives)
487             }
488             ty::Predicate::TypeOutlives(ref binder) => {
489                 tcx.lift(binder).map(ty::Predicate::TypeOutlives)
490             }
491             ty::Predicate::Projection(ref binder) => {
492                 tcx.lift(binder).map(ty::Predicate::Projection)
493             }
494             ty::Predicate::WellFormed(ty) => tcx.lift(&ty).map(ty::Predicate::WellFormed),
495             ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
496                 tcx.lift(&closure_substs).map(|closure_substs| {
497                     ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind)
498                 })
499             }
500             ty::Predicate::ObjectSafe(trait_def_id) => {
501                 Some(ty::Predicate::ObjectSafe(trait_def_id))
502             }
503             ty::Predicate::ConstEvaluatable(def_id, substs) => {
504                 tcx.lift(&substs).map(|substs| ty::Predicate::ConstEvaluatable(def_id, substs))
505             }
506         }
507     }
508 }
509
510 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
511     type Lifted = ty::Binder<T::Lifted>;
512     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
513         tcx.lift(self.skip_binder()).map(ty::Binder::bind)
514     }
515 }
516
517 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
518     type Lifted = ty::ParamEnv<'tcx>;
519     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
520         tcx.lift(&self.caller_bounds).map(|caller_bounds| ty::ParamEnv {
521             reveal: self.reveal,
522             caller_bounds,
523             def_id: self.def_id,
524         })
525     }
526 }
527
528 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
529     type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
530     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
531         tcx.lift(&self.param_env).and_then(|param_env| {
532             tcx.lift(&self.value).map(|value| ty::ParamEnvAnd { param_env, value })
533         })
534     }
535 }
536
537 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
538     type Lifted = ty::ClosureSubsts<'tcx>;
539     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
540         tcx.lift(&self.substs).map(|substs| ty::ClosureSubsts { substs })
541     }
542 }
543
544 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
545     type Lifted = ty::GeneratorSubsts<'tcx>;
546     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
547         tcx.lift(&self.substs).map(|substs| ty::GeneratorSubsts { substs })
548     }
549 }
550
551 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
552     type Lifted = ty::adjustment::Adjustment<'tcx>;
553     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
554         tcx.lift(&self.kind).and_then(|kind| {
555             tcx.lift(&self.target).map(|target| ty::adjustment::Adjustment { kind, target })
556         })
557     }
558 }
559
560 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
561     type Lifted = ty::adjustment::Adjust<'tcx>;
562     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
563         match *self {
564             ty::adjustment::Adjust::NeverToAny => Some(ty::adjustment::Adjust::NeverToAny),
565             ty::adjustment::Adjust::Pointer(ptr) => Some(ty::adjustment::Adjust::Pointer(ptr)),
566             ty::adjustment::Adjust::Deref(ref overloaded) => {
567                 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
568             }
569             ty::adjustment::Adjust::Borrow(ref autoref) => {
570                 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
571             }
572         }
573     }
574 }
575
576 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
577     type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
578     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
579         tcx.lift(&self.region)
580             .map(|region| ty::adjustment::OverloadedDeref { region, mutbl: self.mutbl })
581     }
582 }
583
584 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
585     type Lifted = ty::adjustment::AutoBorrow<'tcx>;
586     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
587         match *self {
588             ty::adjustment::AutoBorrow::Ref(r, m) => {
589                 tcx.lift(&r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
590             }
591             ty::adjustment::AutoBorrow::RawPtr(m) => Some(ty::adjustment::AutoBorrow::RawPtr(m)),
592         }
593     }
594 }
595
596 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
597     type Lifted = ty::GenSig<'tcx>;
598     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
599         tcx.lift(&(self.yield_ty, self.return_ty))
600             .map(|(yield_ty, return_ty)| ty::GenSig { yield_ty, return_ty })
601     }
602 }
603
604 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
605     type Lifted = ty::FnSig<'tcx>;
606     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
607         tcx.lift(&self.inputs_and_output).map(|x| ty::FnSig {
608             inputs_and_output: x,
609             c_variadic: self.c_variadic,
610             unsafety: self.unsafety,
611             abi: self.abi,
612         })
613     }
614 }
615
616 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
617     type Lifted = ty::error::ExpectedFound<T::Lifted>;
618     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
619         tcx.lift(&self.expected).and_then(|expected| {
620             tcx.lift(&self.found).map(|found| ty::error::ExpectedFound { expected, found })
621         })
622     }
623 }
624
625 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
626     type Lifted = ty::error::TypeError<'tcx>;
627     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
628         use crate::ty::error::TypeError::*;
629
630         Some(match *self {
631             Mismatch => Mismatch,
632             UnsafetyMismatch(x) => UnsafetyMismatch(x),
633             AbiMismatch(x) => AbiMismatch(x),
634             Mutability => Mutability,
635             TupleSize(x) => TupleSize(x),
636             FixedArraySize(x) => FixedArraySize(x),
637             ArgCount => ArgCount,
638             RegionsDoesNotOutlive(a, b) => {
639                 return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b));
640             }
641             RegionsInsufficientlyPolymorphic(a, b) => {
642                 return tcx.lift(&b).map(|b| RegionsInsufficientlyPolymorphic(a, b));
643             }
644             RegionsOverlyPolymorphic(a, b) => {
645                 return tcx.lift(&b).map(|b| RegionsOverlyPolymorphic(a, b));
646             }
647             RegionsPlaceholderMismatch => RegionsPlaceholderMismatch,
648             IntMismatch(x) => IntMismatch(x),
649             FloatMismatch(x) => FloatMismatch(x),
650             Traits(x) => Traits(x),
651             VariadicMismatch(x) => VariadicMismatch(x),
652             CyclicTy(t) => return tcx.lift(&t).map(|t| CyclicTy(t)),
653             ProjectionMismatched(x) => ProjectionMismatched(x),
654             ProjectionBoundsLength(x) => ProjectionBoundsLength(x),
655             Sorts(ref x) => return tcx.lift(x).map(Sorts),
656             ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch),
657             ConstMismatch(ref x) => return tcx.lift(x).map(ConstMismatch),
658             IntrinsicCast => IntrinsicCast,
659             ObjectUnsafeCoercion(ref x) => return tcx.lift(x).map(ObjectUnsafeCoercion),
660         })
661     }
662 }
663
664 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
665     type Lifted = ty::InstanceDef<'tcx>;
666     fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
667         match *self {
668             ty::InstanceDef::Item(def_id) => Some(ty::InstanceDef::Item(def_id)),
669             ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
670             ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
671             ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
672             ty::InstanceDef::FnPtrShim(def_id, ref ty) => {
673                 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?))
674             }
675             ty::InstanceDef::Virtual(def_id, n) => Some(ty::InstanceDef::Virtual(def_id, n)),
676             ty::InstanceDef::ClosureOnceShim { call_once } => {
677                 Some(ty::InstanceDef::ClosureOnceShim { call_once })
678             }
679             ty::InstanceDef::DropGlue(def_id, ref ty) => {
680                 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?))
681             }
682             ty::InstanceDef::CloneShim(def_id, ref ty) => {
683                 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?))
684             }
685         }
686     }
687 }
688
689 ///////////////////////////////////////////////////////////////////////////
690 // TypeFoldable implementations.
691 //
692 // Ideally, each type should invoke `folder.fold_foo(self)` and
693 // nothing else. In some cases, though, we haven't gotten around to
694 // adding methods on the `folder` yet, and thus the folding is
695 // hard-coded here. This is less-flexible, because folders cannot
696 // override the behavior, but there are a lot of random types and one
697 // can easily refactor the folding into the TypeFolder trait as
698 // needed.
699
700 /// AdtDefs are basically the same as a DefId.
701 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
702     fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
703         *self
704     }
705
706     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
707         false
708     }
709 }
710
711 impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
712     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (T, U) {
713         (self.0.fold_with(folder), self.1.fold_with(folder))
714     }
715
716     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
717         self.0.visit_with(visitor) || self.1.visit_with(visitor)
718     }
719 }
720
721 EnumTypeFoldableImpl! {
722     impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
723         (Some)(a),
724         (None),
725     } where T: TypeFoldable<'tcx>
726 }
727
728 EnumTypeFoldableImpl! {
729     impl<'tcx, T, E> TypeFoldable<'tcx> for Result<T, E> {
730         (Ok)(a),
731         (Err)(a),
732     } where T: TypeFoldable<'tcx>, E: TypeFoldable<'tcx>,
733 }
734
735 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
736     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
737         Rc::new((**self).fold_with(folder))
738     }
739
740     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
741         (**self).visit_with(visitor)
742     }
743 }
744
745 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Arc<T> {
746     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
747         Arc::new((**self).fold_with(folder))
748     }
749
750     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
751         (**self).visit_with(visitor)
752     }
753 }
754
755 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
756     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
757         let content: T = (**self).fold_with(folder);
758         box content
759     }
760
761     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
762         (**self).visit_with(visitor)
763     }
764 }
765
766 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
767     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
768         self.iter().map(|t| t.fold_with(folder)).collect()
769     }
770
771     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
772         self.iter().any(|t| t.visit_with(visitor))
773     }
774 }
775
776 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
777     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
778         self.iter().map(|t| t.fold_with(folder)).collect::<Vec<_>>().into_boxed_slice()
779     }
780
781     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
782         self.iter().any(|t| t.visit_with(visitor))
783     }
784 }
785
786 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
787     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
788         self.map_bound_ref(|ty| ty.fold_with(folder))
789     }
790
791     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
792         folder.fold_binder(self)
793     }
794
795     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
796         self.skip_binder().visit_with(visitor)
797     }
798
799     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
800         visitor.visit_binder(self)
801     }
802 }
803
804 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
805     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
806         let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
807         folder.tcx().intern_existential_predicates(&v)
808     }
809
810     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
811         self.iter().any(|p| p.visit_with(visitor))
812     }
813 }
814
815 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
816     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
817         let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
818         folder.tcx().intern_type_list(&v)
819     }
820
821     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
822         self.iter().any(|t| t.visit_with(visitor))
823     }
824 }
825
826 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
827     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
828         let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
829         folder.tcx().intern_projs(&v)
830     }
831
832     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
833         self.iter().any(|t| t.visit_with(visitor))
834     }
835 }
836
837 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
838     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
839         use crate::ty::InstanceDef::*;
840         Self {
841             substs: self.substs.fold_with(folder),
842             def: match self.def {
843                 Item(did) => Item(did.fold_with(folder)),
844                 VtableShim(did) => VtableShim(did.fold_with(folder)),
845                 ReifyShim(did) => ReifyShim(did.fold_with(folder)),
846                 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
847                 FnPtrShim(did, ty) => FnPtrShim(did.fold_with(folder), ty.fold_with(folder)),
848                 Virtual(did, i) => Virtual(did.fold_with(folder), i),
849                 ClosureOnceShim { call_once } => {
850                     ClosureOnceShim { call_once: call_once.fold_with(folder) }
851                 }
852                 DropGlue(did, ty) => DropGlue(did.fold_with(folder), ty.fold_with(folder)),
853                 CloneShim(did, ty) => CloneShim(did.fold_with(folder), ty.fold_with(folder)),
854             },
855         }
856     }
857
858     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
859         use crate::ty::InstanceDef::*;
860         self.substs.visit_with(visitor)
861             || match self.def {
862                 Item(did) | VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
863                     did.visit_with(visitor)
864                 }
865                 FnPtrShim(did, ty) | CloneShim(did, ty) => {
866                     did.visit_with(visitor) || ty.visit_with(visitor)
867                 }
868                 DropGlue(did, ty) => did.visit_with(visitor) || ty.visit_with(visitor),
869                 ClosureOnceShim { call_once } => call_once.visit_with(visitor),
870             }
871     }
872 }
873
874 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
875     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
876         Self { instance: self.instance.fold_with(folder), promoted: self.promoted }
877     }
878
879     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
880         self.instance.visit_with(visitor)
881     }
882 }
883
884 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
885     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
886         let kind = match self.kind {
887             ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
888             ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
889             ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
890             ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
891             ty::Dynamic(ref trait_ty, ref region) => {
892                 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder))
893             }
894             ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
895             ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.fold_with(folder)),
896             ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
897             ty::Ref(ref r, ty, mutbl) => ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl),
898             ty::Generator(did, substs, movability) => {
899                 ty::Generator(did, substs.fold_with(folder), movability)
900             }
901             ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
902             ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
903             ty::Projection(ref data) => ty::Projection(data.fold_with(folder)),
904             ty::UnnormalizedProjection(ref data) => {
905                 ty::UnnormalizedProjection(data.fold_with(folder))
906             }
907             ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
908
909             ty::Bool
910             | ty::Char
911             | ty::Str
912             | ty::Int(_)
913             | ty::Uint(_)
914             | ty::Float(_)
915             | ty::Error
916             | ty::Infer(_)
917             | ty::Param(..)
918             | ty::Bound(..)
919             | ty::Placeholder(..)
920             | ty::Never
921             | ty::Foreign(..) => return self,
922         };
923
924         if self.kind == kind { self } else { folder.tcx().mk_ty(kind) }
925     }
926
927     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
928         folder.fold_ty(*self)
929     }
930
931     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
932         match self.kind {
933             ty::RawPtr(ref tm) => tm.visit_with(visitor),
934             ty::Array(typ, sz) => typ.visit_with(visitor) || sz.visit_with(visitor),
935             ty::Slice(typ) => typ.visit_with(visitor),
936             ty::Adt(_, substs) => substs.visit_with(visitor),
937             ty::Dynamic(ref trait_ty, ref reg) => {
938                 trait_ty.visit_with(visitor) || reg.visit_with(visitor)
939             }
940             ty::Tuple(ts) => ts.visit_with(visitor),
941             ty::FnDef(_, substs) => substs.visit_with(visitor),
942             ty::FnPtr(ref f) => f.visit_with(visitor),
943             ty::Ref(r, ty, _) => r.visit_with(visitor) || ty.visit_with(visitor),
944             ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
945             ty::GeneratorWitness(ref types) => types.visit_with(visitor),
946             ty::Closure(_did, ref substs) => substs.visit_with(visitor),
947             ty::Projection(ref data) | ty::UnnormalizedProjection(ref data) => {
948                 data.visit_with(visitor)
949             }
950             ty::Opaque(_, ref substs) => substs.visit_with(visitor),
951
952             ty::Bool
953             | ty::Char
954             | ty::Str
955             | ty::Int(_)
956             | ty::Uint(_)
957             | ty::Float(_)
958             | ty::Error
959             | ty::Infer(_)
960             | ty::Bound(..)
961             | ty::Placeholder(..)
962             | ty::Param(..)
963             | ty::Never
964             | ty::Foreign(..) => false,
965         }
966     }
967
968     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
969         visitor.visit_ty(self)
970     }
971 }
972
973 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
974     fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
975         *self
976     }
977
978     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
979         folder.fold_region(*self)
980     }
981
982     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
983         false
984     }
985
986     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
987         visitor.visit_region(*self)
988     }
989 }
990
991 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
992     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
993         // This code is hot enough that it's worth specializing for a list of
994         // length 0. (No other length is common enough to be worth singling
995         // out).
996         if self.len() == 0 {
997             self
998         } else {
999             // Don't bother interning if nothing changed, which is the common
1000             // case.
1001             let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
1002             if v[..] == self[..] { self } else { folder.tcx().intern_predicates(&v) }
1003         }
1004     }
1005
1006     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1007         self.iter().any(|p| p.visit_with(visitor))
1008     }
1009 }
1010
1011 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
1012     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1013         self.iter().map(|x| x.fold_with(folder)).collect()
1014     }
1015
1016     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1017         self.iter().any(|t| t.visit_with(visitor))
1018     }
1019 }
1020
1021 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1022     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1023         let ty = self.ty.fold_with(folder);
1024         let val = self.val.fold_with(folder);
1025         folder.tcx().mk_const(ty::Const { ty, val })
1026     }
1027
1028     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1029         folder.fold_const(*self)
1030     }
1031
1032     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1033         self.ty.visit_with(visitor) || self.val.visit_with(visitor)
1034     }
1035
1036     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1037         visitor.visit_const(self)
1038     }
1039 }
1040
1041 impl<'tcx> TypeFoldable<'tcx> for ty::ConstKind<'tcx> {
1042     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
1043         match *self {
1044             ty::ConstKind::Infer(ic) => ty::ConstKind::Infer(ic.fold_with(folder)),
1045             ty::ConstKind::Param(p) => ty::ConstKind::Param(p.fold_with(folder)),
1046             ty::ConstKind::Unevaluated(did, substs) => {
1047                 ty::ConstKind::Unevaluated(did, substs.fold_with(folder))
1048             }
1049             ty::ConstKind::Value(_) | ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(..) => {
1050                 *self
1051             }
1052         }
1053     }
1054
1055     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1056         match *self {
1057             ty::ConstKind::Infer(ic) => ic.visit_with(visitor),
1058             ty::ConstKind::Param(p) => p.visit_with(visitor),
1059             ty::ConstKind::Unevaluated(_, substs) => substs.visit_with(visitor),
1060             ty::ConstKind::Value(_) | ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(_) => {
1061                 false
1062             }
1063         }
1064     }
1065 }
1066
1067 impl<'tcx> TypeFoldable<'tcx> for InferConst<'tcx> {
1068     fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
1069         *self
1070     }
1071
1072     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
1073         false
1074     }
1075 }