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