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