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.
6 use mir::ProjectionKind;
7 use mir::interpret::ConstValue;
8 use ty::{self, Lift, Ty, TyCtxt};
9 use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
10 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
11 use smallvec::SmallVec;
16 ///////////////////////////////////////////////////////////////////////////
19 // For things that don't carry any arena-allocated data (and are
20 // copy...), just add them to this list.
22 CloneTypeFoldableAndLiftImpls! {
26 ::ty::layout::VariantIdx,
29 ::middle::region::Scope,
30 ::syntax::ast::FloatTy,
31 ::syntax::ast::NodeId,
32 ::syntax_pos::symbol::Symbol,
39 ::rustc_target::spec::abi::Abi,
43 ::ty::adjustment::AutoBorrowMutability,
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
59 ///////////////////////////////////////////////////////////////////////////
60 // Lift implementations
62 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
63 type Lifted = (A::Lifted, B::Lifted);
64 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
65 tcx.lift(&self.0).and_then(|a| tcx.lift(&self.1).map(|b| (a, b)))
69 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
70 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
71 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
72 tcx.lift(&self.0).and_then(|a| {
73 tcx.lift(&self.1).and_then(|b| tcx.lift(&self.2).map(|c| (a, b, c)))
78 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
79 type Lifted = Option<T::Lifted>;
80 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
82 Some(ref x) => tcx.lift(x).map(Some),
88 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
89 type Lifted = Result<T::Lifted, E::Lifted>;
90 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
92 Ok(ref x) => tcx.lift(x).map(Ok),
93 Err(ref e) => tcx.lift(e).map(Err)
98 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
99 type Lifted = Box<T::Lifted>;
100 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
101 tcx.lift(&**self).map(Box::new)
105 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for [T] {
106 type Lifted = Vec<T::Lifted>;
107 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
108 // type annotation needed to inform `projection_must_outlive`
109 let mut result : Vec<<T as Lift<'tcx>>::Lifted>
110 = Vec::with_capacity(self.len());
112 if let Some(value) = tcx.lift(x) {
122 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
123 type Lifted = Vec<T::Lifted>;
124 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
129 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
130 type Lifted = IndexVec<I, T::Lifted>;
131 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
133 .map(|e| tcx.lift(e))
138 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
139 type Lifted = ty::TraitRef<'tcx>;
140 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
141 tcx.lift(&self.substs).map(|substs| ty::TraitRef {
148 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
149 type Lifted = ty::ExistentialTraitRef<'tcx>;
150 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
151 tcx.lift(&self.substs).map(|substs| ty::ExistentialTraitRef {
158 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
159 type Lifted = ty::TraitPredicate<'tcx>;
160 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
161 -> Option<ty::TraitPredicate<'tcx>> {
162 tcx.lift(&self.trait_ref).map(|trait_ref| ty::TraitPredicate {
168 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
169 type Lifted = ty::SubtypePredicate<'tcx>;
170 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
171 -> Option<ty::SubtypePredicate<'tcx>> {
172 tcx.lift(&(self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
173 a_is_expected: self.a_is_expected,
180 impl<'tcx, A: Copy+Lift<'tcx>, B: Copy+Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
181 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
182 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
183 tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
187 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
188 type Lifted = ty::ProjectionTy<'tcx>;
189 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
190 -> Option<ty::ProjectionTy<'tcx>> {
191 tcx.lift(&self.substs).map(|substs| {
193 item_def_id: self.item_def_id,
200 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
201 type Lifted = ty::ProjectionPredicate<'tcx>;
202 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
203 -> Option<ty::ProjectionPredicate<'tcx>> {
204 tcx.lift(&(self.projection_ty, self.ty)).map(|(projection_ty, ty)| {
205 ty::ProjectionPredicate {
213 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
214 type Lifted = ty::ExistentialProjection<'tcx>;
215 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
216 tcx.lift(&self.substs).map(|substs| {
217 ty::ExistentialProjection {
219 ty: tcx.lift(&self.ty).expect("type must lift when substs do"),
220 item_def_id: self.item_def_id,
226 impl<'a, 'tcx> Lift<'tcx> for ty::Predicate<'a> {
227 type Lifted = ty::Predicate<'tcx>;
228 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
230 ty::Predicate::Trait(ref binder) => {
231 tcx.lift(binder).map(ty::Predicate::Trait)
233 ty::Predicate::Subtype(ref binder) => {
234 tcx.lift(binder).map(ty::Predicate::Subtype)
236 ty::Predicate::RegionOutlives(ref binder) => {
237 tcx.lift(binder).map(ty::Predicate::RegionOutlives)
239 ty::Predicate::TypeOutlives(ref binder) => {
240 tcx.lift(binder).map(ty::Predicate::TypeOutlives)
242 ty::Predicate::Projection(ref binder) => {
243 tcx.lift(binder).map(ty::Predicate::Projection)
245 ty::Predicate::WellFormed(ty) => {
246 tcx.lift(&ty).map(ty::Predicate::WellFormed)
248 ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
249 tcx.lift(&closure_substs)
250 .map(|closure_substs| ty::Predicate::ClosureKind(closure_def_id,
254 ty::Predicate::ObjectSafe(trait_def_id) => {
255 Some(ty::Predicate::ObjectSafe(trait_def_id))
257 ty::Predicate::ConstEvaluatable(def_id, substs) => {
258 tcx.lift(&substs).map(|substs| {
259 ty::Predicate::ConstEvaluatable(def_id, substs)
266 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
267 type Lifted = ty::Binder<T::Lifted>;
268 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
269 tcx.lift(self.skip_binder()).map(ty::Binder::bind)
273 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
274 type Lifted = ty::ParamEnv<'tcx>;
275 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
276 tcx.lift(&self.caller_bounds).map(|caller_bounds| {
286 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
287 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
288 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
289 tcx.lift(&self.param_env).and_then(|param_env| {
290 tcx.lift(&self.value).map(|value| {
300 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
301 type Lifted = ty::ClosureSubsts<'tcx>;
302 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
303 tcx.lift(&self.substs).map(|substs| {
304 ty::ClosureSubsts { substs }
309 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
310 type Lifted = ty::GeneratorSubsts<'tcx>;
311 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
312 tcx.lift(&self.substs).map(|substs| {
313 ty::GeneratorSubsts { substs }
318 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
319 type Lifted = ty::adjustment::Adjustment<'tcx>;
320 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
321 tcx.lift(&self.kind).and_then(|kind| {
322 tcx.lift(&self.target).map(|target| {
323 ty::adjustment::Adjustment { kind, target }
329 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
330 type Lifted = ty::adjustment::Adjust<'tcx>;
331 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
333 ty::adjustment::Adjust::NeverToAny =>
334 Some(ty::adjustment::Adjust::NeverToAny),
335 ty::adjustment::Adjust::ReifyFnPointer =>
336 Some(ty::adjustment::Adjust::ReifyFnPointer),
337 ty::adjustment::Adjust::UnsafeFnPointer =>
338 Some(ty::adjustment::Adjust::UnsafeFnPointer),
339 ty::adjustment::Adjust::ClosureFnPointer =>
340 Some(ty::adjustment::Adjust::ClosureFnPointer),
341 ty::adjustment::Adjust::MutToConstPointer =>
342 Some(ty::adjustment::Adjust::MutToConstPointer),
343 ty::adjustment::Adjust::Unsize =>
344 Some(ty::adjustment::Adjust::Unsize),
345 ty::adjustment::Adjust::Deref(ref overloaded) => {
346 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
348 ty::adjustment::Adjust::Borrow(ref autoref) => {
349 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
355 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
356 type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
357 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
358 tcx.lift(&self.region).map(|region| {
359 ty::adjustment::OverloadedDeref {
367 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
368 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
369 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
371 ty::adjustment::AutoBorrow::Ref(r, m) => {
372 tcx.lift(&r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
374 ty::adjustment::AutoBorrow::RawPtr(m) => {
375 Some(ty::adjustment::AutoBorrow::RawPtr(m))
381 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
382 type Lifted = ty::GenSig<'tcx>;
383 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
384 tcx.lift(&(self.yield_ty, self.return_ty))
385 .map(|(yield_ty, return_ty)| {
394 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
395 type Lifted = ty::FnSig<'tcx>;
396 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
397 tcx.lift(&self.inputs_and_output).map(|x| {
399 inputs_and_output: x,
400 variadic: self.variadic,
401 unsafety: self.unsafety,
408 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
409 type Lifted = ty::error::ExpectedFound<T::Lifted>;
410 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
411 tcx.lift(&self.expected).and_then(|expected| {
412 tcx.lift(&self.found).map(|found| {
413 ty::error::ExpectedFound {
422 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
423 type Lifted = ty::error::TypeError<'tcx>;
424 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
425 use ty::error::TypeError::*;
428 Mismatch => Mismatch,
429 UnsafetyMismatch(x) => UnsafetyMismatch(x),
430 AbiMismatch(x) => AbiMismatch(x),
431 Mutability => Mutability,
432 TupleSize(x) => TupleSize(x),
433 FixedArraySize(x) => FixedArraySize(x),
434 ArgCount => ArgCount,
435 RegionsDoesNotOutlive(a, b) => {
436 return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b))
438 RegionsInsufficientlyPolymorphic(a, b) => {
439 return tcx.lift(&b).map(|b| RegionsInsufficientlyPolymorphic(a, b))
441 RegionsOverlyPolymorphic(a, b) => {
442 return tcx.lift(&b).map(|b| RegionsOverlyPolymorphic(a, b))
444 IntMismatch(x) => IntMismatch(x),
445 FloatMismatch(x) => FloatMismatch(x),
446 Traits(x) => Traits(x),
447 VariadicMismatch(x) => VariadicMismatch(x),
448 CyclicTy(t) => return tcx.lift(&t).map(|t| CyclicTy(t)),
449 ProjectionMismatched(x) => ProjectionMismatched(x),
450 ProjectionBoundsLength(x) => ProjectionBoundsLength(x),
451 Sorts(ref x) => return tcx.lift(x).map(Sorts),
452 ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch)
457 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
458 type Lifted = ty::InstanceDef<'tcx>;
459 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
461 ty::InstanceDef::Item(def_id) =>
462 Some(ty::InstanceDef::Item(def_id)),
463 ty::InstanceDef::VtableShim(def_id) =>
464 Some(ty::InstanceDef::VtableShim(def_id)),
465 ty::InstanceDef::Intrinsic(def_id) =>
466 Some(ty::InstanceDef::Intrinsic(def_id)),
467 ty::InstanceDef::FnPtrShim(def_id, ref ty) =>
468 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?)),
469 ty::InstanceDef::Virtual(def_id, n) =>
470 Some(ty::InstanceDef::Virtual(def_id, n)),
471 ty::InstanceDef::ClosureOnceShim { call_once } =>
472 Some(ty::InstanceDef::ClosureOnceShim { call_once }),
473 ty::InstanceDef::DropGlue(def_id, ref ty) =>
474 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?)),
475 ty::InstanceDef::CloneShim(def_id, ref ty) =>
476 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?)),
481 BraceStructLiftImpl! {
482 impl<'a, 'tcx> Lift<'tcx> for ty::Instance<'a> {
483 type Lifted = ty::Instance<'tcx>;
488 BraceStructLiftImpl! {
489 impl<'a, 'tcx> Lift<'tcx> for interpret::GlobalId<'a> {
490 type Lifted = interpret::GlobalId<'tcx>;
495 ///////////////////////////////////////////////////////////////////////////
496 // TypeFoldable implementations.
498 // Ideally, each type should invoke `folder.fold_foo(self)` and
499 // nothing else. In some cases, though, we haven't gotten around to
500 // adding methods on the `folder` yet, and thus the folding is
501 // hard-coded here. This is less-flexible, because folders cannot
502 // override the behavior, but there are a lot of random types and one
503 // can easily refactor the folding into the TypeFolder trait as
506 /// AdtDefs are basically the same as a DefId.
507 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
508 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
512 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
517 impl<'tcx, T:TypeFoldable<'tcx>, U:TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
518 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> (T, U) {
519 (self.0.fold_with(folder), self.1.fold_with(folder))
522 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
523 self.0.visit_with(visitor) || self.1.visit_with(visitor)
527 EnumTypeFoldableImpl! {
528 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
531 } where T: TypeFoldable<'tcx>
534 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
535 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
536 Rc::new((**self).fold_with(folder))
539 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
540 (**self).visit_with(visitor)
544 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
545 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
546 let content: T = (**self).fold_with(folder);
550 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
551 (**self).visit_with(visitor)
555 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
556 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
557 self.iter().map(|t| t.fold_with(folder)).collect()
560 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
561 self.iter().any(|t| t.visit_with(visitor))
565 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[T]> {
566 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
567 self.iter().map(|t| t.fold_with(folder)).collect::<Vec<_>>().into_boxed_slice()
570 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
571 self.iter().any(|t| t.visit_with(visitor))
575 impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
576 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
577 self.map_bound_ref(|ty| ty.fold_with(folder))
580 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
581 folder.fold_binder(self)
584 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
585 self.skip_binder().visit_with(visitor)
588 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
589 visitor.visit_binder(self)
593 BraceStructTypeFoldableImpl! {
594 impl<'tcx> TypeFoldable<'tcx> for ty::ParamEnv<'tcx> { reveal, caller_bounds, def_id }
597 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
598 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
599 let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
600 folder.tcx().intern_existential_predicates(&v)
603 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
604 self.iter().any(|p| p.visit_with(visitor))
608 EnumTypeFoldableImpl! {
609 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialPredicate<'tcx> {
610 (ty::ExistentialPredicate::Trait)(a),
611 (ty::ExistentialPredicate::Projection)(a),
612 (ty::ExistentialPredicate::AutoTrait)(a),
616 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
617 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
618 let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
619 folder.tcx().intern_type_list(&v)
622 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
623 self.iter().any(|t| t.visit_with(visitor))
627 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind<'tcx>> {
628 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
629 let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
630 folder.tcx().intern_projs(&v)
633 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
634 self.iter().any(|t| t.visit_with(visitor))
638 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
639 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
640 use ty::InstanceDef::*;
642 substs: self.substs.fold_with(folder),
643 def: match self.def {
644 Item(did) => Item(did.fold_with(folder)),
645 VtableShim(did) => VtableShim(did.fold_with(folder)),
646 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
647 FnPtrShim(did, ty) => FnPtrShim(
648 did.fold_with(folder),
649 ty.fold_with(folder),
651 Virtual(did, i) => Virtual(
652 did.fold_with(folder),
655 ClosureOnceShim { call_once } => ClosureOnceShim {
656 call_once: call_once.fold_with(folder),
658 DropGlue(did, ty) => DropGlue(
659 did.fold_with(folder),
660 ty.fold_with(folder),
662 CloneShim(did, ty) => CloneShim(
663 did.fold_with(folder),
664 ty.fold_with(folder),
670 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
671 use ty::InstanceDef::*;
672 self.substs.visit_with(visitor) ||
674 Item(did) | VtableShim(did) | Intrinsic(did) | Virtual(did, _) => {
675 did.visit_with(visitor)
677 FnPtrShim(did, ty) | CloneShim(did, ty) => {
678 did.visit_with(visitor) || ty.visit_with(visitor)
680 DropGlue(did, ty) => {
681 did.visit_with(visitor) || ty.visit_with(visitor)
683 ClosureOnceShim { call_once } => call_once.visit_with(visitor),
688 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
689 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
691 instance: self.instance.fold_with(folder),
692 promoted: self.promoted
696 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
697 self.instance.visit_with(visitor)
701 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
702 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
703 let sty = match self.sty {
704 ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
705 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
706 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
707 ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
708 ty::Dynamic(ref trait_ty, ref region) =>
709 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder)),
710 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
711 ty::FnDef(def_id, substs) => {
712 ty::FnDef(def_id, substs.fold_with(folder))
714 ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
715 ty::Ref(ref r, ty, mutbl) => {
716 ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl)
718 ty::Generator(did, substs, movability) => {
721 substs.fold_with(folder),
724 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
725 ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
726 ty::Projection(ref data) => ty::Projection(data.fold_with(folder)),
727 ty::UnnormalizedProjection(ref data) => {
728 ty::UnnormalizedProjection(data.fold_with(folder))
730 ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
742 ty::Placeholder(..) |
744 ty::Foreign(..) => return self
750 folder.tcx().mk_ty(sty)
754 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
755 folder.fold_ty(*self)
758 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
760 ty::RawPtr(ref tm) => tm.visit_with(visitor),
761 ty::Array(typ, sz) => typ.visit_with(visitor) || sz.visit_with(visitor),
762 ty::Slice(typ) => typ.visit_with(visitor),
763 ty::Adt(_, substs) => substs.visit_with(visitor),
764 ty::Dynamic(ref trait_ty, ref reg) =>
765 trait_ty.visit_with(visitor) || reg.visit_with(visitor),
766 ty::Tuple(ts) => ts.visit_with(visitor),
767 ty::FnDef(_, substs) => substs.visit_with(visitor),
768 ty::FnPtr(ref f) => f.visit_with(visitor),
769 ty::Ref(r, ty, _) => r.visit_with(visitor) || ty.visit_with(visitor),
770 ty::Generator(_did, ref substs, _) => {
771 substs.visit_with(visitor)
773 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
774 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
775 ty::Projection(ref data) | ty::UnnormalizedProjection(ref data) => {
776 data.visit_with(visitor)
778 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
789 ty::Placeholder(..) |
792 ty::Foreign(..) => false,
796 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
797 visitor.visit_ty(self)
801 BraceStructTypeFoldableImpl! {
802 impl<'tcx> TypeFoldable<'tcx> for ty::TypeAndMut<'tcx> {
807 BraceStructTypeFoldableImpl! {
808 impl<'tcx> TypeFoldable<'tcx> for ty::GenSig<'tcx> {
813 BraceStructTypeFoldableImpl! {
814 impl<'tcx> TypeFoldable<'tcx> for ty::FnSig<'tcx> {
815 inputs_and_output, variadic, unsafety, abi
819 BraceStructTypeFoldableImpl! {
820 impl<'tcx> TypeFoldable<'tcx> for ty::TraitRef<'tcx> { def_id, substs }
823 BraceStructTypeFoldableImpl! {
824 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialTraitRef<'tcx> { def_id, substs }
827 BraceStructTypeFoldableImpl! {
828 impl<'tcx> TypeFoldable<'tcx> for ty::ImplHeader<'tcx> {
836 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
837 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
841 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
842 folder.fold_region(*self)
845 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
849 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
850 visitor.visit_region(*self)
854 BraceStructTypeFoldableImpl! {
855 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureSubsts<'tcx> {
860 BraceStructTypeFoldableImpl! {
861 impl<'tcx> TypeFoldable<'tcx> for ty::GeneratorSubsts<'tcx> {
866 BraceStructTypeFoldableImpl! {
867 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::Adjustment<'tcx> {
873 EnumTypeFoldableImpl! {
874 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::Adjust<'tcx> {
875 (ty::adjustment::Adjust::NeverToAny),
876 (ty::adjustment::Adjust::ReifyFnPointer),
877 (ty::adjustment::Adjust::UnsafeFnPointer),
878 (ty::adjustment::Adjust::ClosureFnPointer),
879 (ty::adjustment::Adjust::MutToConstPointer),
880 (ty::adjustment::Adjust::Unsize),
881 (ty::adjustment::Adjust::Deref)(a),
882 (ty::adjustment::Adjust::Borrow)(a),
886 BraceStructTypeFoldableImpl! {
887 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::OverloadedDeref<'tcx> {
892 EnumTypeFoldableImpl! {
893 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::AutoBorrow<'tcx> {
894 (ty::adjustment::AutoBorrow::Ref)(a, b),
895 (ty::adjustment::AutoBorrow::RawPtr)(m),
899 BraceStructTypeFoldableImpl! {
900 impl<'tcx> TypeFoldable<'tcx> for ty::GenericPredicates<'tcx> {
905 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
906 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
907 let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
908 folder.tcx().intern_predicates(&v)
911 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
912 self.iter().any(|p| p.visit_with(visitor))
916 EnumTypeFoldableImpl! {
917 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
918 (ty::Predicate::Trait)(a),
919 (ty::Predicate::Subtype)(a),
920 (ty::Predicate::RegionOutlives)(a),
921 (ty::Predicate::TypeOutlives)(a),
922 (ty::Predicate::Projection)(a),
923 (ty::Predicate::WellFormed)(a),
924 (ty::Predicate::ClosureKind)(a, b, c),
925 (ty::Predicate::ObjectSafe)(a),
926 (ty::Predicate::ConstEvaluatable)(a, b),
930 BraceStructTypeFoldableImpl! {
931 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionPredicate<'tcx> {
936 BraceStructTypeFoldableImpl! {
937 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialProjection<'tcx> {
938 ty, substs, item_def_id
942 BraceStructTypeFoldableImpl! {
943 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionTy<'tcx> {
948 BraceStructTypeFoldableImpl! {
949 impl<'tcx> TypeFoldable<'tcx> for ty::InstantiatedPredicates<'tcx> {
954 BraceStructTypeFoldableImpl! {
955 impl<'tcx, T> TypeFoldable<'tcx> for ty::ParamEnvAnd<'tcx, T> {
957 } where T: TypeFoldable<'tcx>
960 BraceStructTypeFoldableImpl! {
961 impl<'tcx> TypeFoldable<'tcx> for ty::SubtypePredicate<'tcx> {
966 BraceStructTypeFoldableImpl! {
967 impl<'tcx> TypeFoldable<'tcx> for ty::TraitPredicate<'tcx> {
972 TupleStructTypeFoldableImpl! {
973 impl<'tcx,T,U> TypeFoldable<'tcx> for ty::OutlivesPredicate<T,U> {
975 } where T : TypeFoldable<'tcx>, U : TypeFoldable<'tcx>,
978 BraceStructTypeFoldableImpl! {
979 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureUpvar<'tcx> {
984 BraceStructTypeFoldableImpl! {
985 impl<'tcx, T> TypeFoldable<'tcx> for ty::error::ExpectedFound<T> {
987 } where T: TypeFoldable<'tcx>
990 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
991 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
992 self.iter().map(|x| x.fold_with(folder)).collect()
995 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
996 self.iter().any(|t| t.visit_with(visitor))
1000 EnumTypeFoldableImpl! {
1001 impl<'tcx> TypeFoldable<'tcx> for ty::error::TypeError<'tcx> {
1002 (ty::error::TypeError::Mismatch),
1003 (ty::error::TypeError::UnsafetyMismatch)(x),
1004 (ty::error::TypeError::AbiMismatch)(x),
1005 (ty::error::TypeError::Mutability),
1006 (ty::error::TypeError::TupleSize)(x),
1007 (ty::error::TypeError::FixedArraySize)(x),
1008 (ty::error::TypeError::ArgCount),
1009 (ty::error::TypeError::RegionsDoesNotOutlive)(a, b),
1010 (ty::error::TypeError::RegionsInsufficientlyPolymorphic)(a, b),
1011 (ty::error::TypeError::RegionsOverlyPolymorphic)(a, b),
1012 (ty::error::TypeError::IntMismatch)(x),
1013 (ty::error::TypeError::FloatMismatch)(x),
1014 (ty::error::TypeError::Traits)(x),
1015 (ty::error::TypeError::VariadicMismatch)(x),
1016 (ty::error::TypeError::CyclicTy)(t),
1017 (ty::error::TypeError::ProjectionMismatched)(x),
1018 (ty::error::TypeError::ProjectionBoundsLength)(x),
1019 (ty::error::TypeError::Sorts)(x),
1020 (ty::error::TypeError::ExistentialMismatch)(x),
1024 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::LazyConst<'tcx> {
1025 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1026 let new = match self {
1027 ty::LazyConst::Evaluated(v) => ty::LazyConst::Evaluated(v.fold_with(folder)),
1028 ty::LazyConst::Unevaluated(def_id, substs) => {
1029 ty::LazyConst::Unevaluated(*def_id, substs.fold_with(folder))
1032 folder.tcx().intern_lazy_const(new)
1035 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1036 folder.fold_const(*self)
1039 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1041 ty::LazyConst::Evaluated(c) => c.visit_with(visitor),
1042 ty::LazyConst::Unevaluated(_, substs) => substs.visit_with(visitor),
1046 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1047 visitor.visit_const(self)
1051 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1052 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1053 let ty = self.ty.fold_with(folder);
1054 let val = self.val.fold_with(folder);
1055 folder.tcx().mk_const(ty::Const {
1061 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1062 self.ty.visit_with(visitor) || self.val.visit_with(visitor)