1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This module contains implements of the `Lift` and `TypeFoldable`
12 //! traits for various types in the Rust compiler. Most are written by
13 //! hand, though we've recently added some macros (e.g.,
14 //! `BraceStructLiftImpl!`) to help with the tedium.
16 use mir::ProjectionKind;
17 use mir::interpret::ConstValue;
18 use ty::{self, Lift, Ty, TyCtxt};
19 use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
20 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
21 use smallvec::SmallVec;
26 ///////////////////////////////////////////////////////////////////////////
29 // For things that don't carry any arena-allocated data (and are
30 // copy...), just add them to this list.
32 CloneTypeFoldableAndLiftImpls! {
37 ::middle::region::Scope,
38 ::syntax::ast::FloatTy,
39 ::syntax::ast::NodeId,
40 ::syntax_pos::symbol::Symbol,
47 ::rustc_target::spec::abi::Abi,
51 ::ty::adjustment::AutoBorrowMutability,
53 // Including `BoundRegion` is a *bit* dubious, but direct
54 // references to bound region appear in `ty::Error`, and aren't
55 // really meant to be folded. In general, we can only fold a fully
66 ///////////////////////////////////////////////////////////////////////////
67 // Lift implementations
69 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
70 type Lifted = (A::Lifted, B::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| tcx.lift(&self.1).map(|b| (a, b)))
76 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C) {
77 type Lifted = (A::Lifted, B::Lifted, C::Lifted);
78 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
79 tcx.lift(&self.0).and_then(|a| {
80 tcx.lift(&self.1).and_then(|b| tcx.lift(&self.2).map(|c| (a, b, c)))
85 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
86 type Lifted = Option<T::Lifted>;
87 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
89 Some(ref x) => tcx.lift(x).map(Some),
95 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
96 type Lifted = Result<T::Lifted, E::Lifted>;
97 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
99 Ok(ref x) => tcx.lift(x).map(Ok),
100 Err(ref e) => tcx.lift(e).map(Err)
105 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Box<T> {
106 type Lifted = Box<T::Lifted>;
107 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
108 tcx.lift(&**self).map(Box::new)
112 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for [T] {
113 type Lifted = Vec<T::Lifted>;
114 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
115 // type annotation needed to inform `projection_must_outlive`
116 let mut result : Vec<<T as Lift<'tcx>>::Lifted>
117 = Vec::with_capacity(self.len());
119 if let Some(value) = tcx.lift(x) {
129 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
130 type Lifted = Vec<T::Lifted>;
131 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
136 impl<'tcx, I: Idx, T: Lift<'tcx>> Lift<'tcx> for IndexVec<I, T> {
137 type Lifted = IndexVec<I, T::Lifted>;
138 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
140 .map(|e| tcx.lift(e))
145 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
146 type Lifted = ty::TraitRef<'tcx>;
147 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
148 tcx.lift(&self.substs).map(|substs| ty::TraitRef {
155 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
156 type Lifted = ty::ExistentialTraitRef<'tcx>;
157 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
158 tcx.lift(&self.substs).map(|substs| ty::ExistentialTraitRef {
165 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
166 type Lifted = ty::TraitPredicate<'tcx>;
167 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
168 -> Option<ty::TraitPredicate<'tcx>> {
169 tcx.lift(&self.trait_ref).map(|trait_ref| ty::TraitPredicate {
175 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
176 type Lifted = ty::SubtypePredicate<'tcx>;
177 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
178 -> Option<ty::SubtypePredicate<'tcx>> {
179 tcx.lift(&(self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
180 a_is_expected: self.a_is_expected,
187 impl<'tcx, A: Copy+Lift<'tcx>, B: Copy+Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
188 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
189 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
190 tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
194 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
195 type Lifted = ty::ProjectionTy<'tcx>;
196 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
197 -> Option<ty::ProjectionTy<'tcx>> {
198 tcx.lift(&self.substs).map(|substs| {
200 item_def_id: self.item_def_id,
207 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
208 type Lifted = ty::ProjectionPredicate<'tcx>;
209 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
210 -> Option<ty::ProjectionPredicate<'tcx>> {
211 tcx.lift(&(self.projection_ty, self.ty)).map(|(projection_ty, ty)| {
212 ty::ProjectionPredicate {
220 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
221 type Lifted = ty::ExistentialProjection<'tcx>;
222 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
223 tcx.lift(&self.substs).map(|substs| {
224 ty::ExistentialProjection {
226 ty: tcx.lift(&self.ty).expect("type must lift when substs do"),
227 item_def_id: self.item_def_id,
233 impl<'a, 'tcx> Lift<'tcx> for ty::Predicate<'a> {
234 type Lifted = ty::Predicate<'tcx>;
235 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
237 ty::Predicate::Trait(ref binder) => {
238 tcx.lift(binder).map(ty::Predicate::Trait)
240 ty::Predicate::Subtype(ref binder) => {
241 tcx.lift(binder).map(ty::Predicate::Subtype)
243 ty::Predicate::RegionOutlives(ref binder) => {
244 tcx.lift(binder).map(ty::Predicate::RegionOutlives)
246 ty::Predicate::TypeOutlives(ref binder) => {
247 tcx.lift(binder).map(ty::Predicate::TypeOutlives)
249 ty::Predicate::Projection(ref binder) => {
250 tcx.lift(binder).map(ty::Predicate::Projection)
252 ty::Predicate::WellFormed(ty) => {
253 tcx.lift(&ty).map(ty::Predicate::WellFormed)
255 ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
256 tcx.lift(&closure_substs)
257 .map(|closure_substs| ty::Predicate::ClosureKind(closure_def_id,
261 ty::Predicate::ObjectSafe(trait_def_id) => {
262 Some(ty::Predicate::ObjectSafe(trait_def_id))
264 ty::Predicate::ConstEvaluatable(def_id, substs) => {
265 tcx.lift(&substs).map(|substs| {
266 ty::Predicate::ConstEvaluatable(def_id, substs)
273 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
274 type Lifted = ty::Binder<T::Lifted>;
275 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
276 tcx.lift(self.skip_binder()).map(ty::Binder::bind)
280 impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> {
281 type Lifted = ty::ParamEnv<'tcx>;
282 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
283 tcx.lift(&self.caller_bounds).map(|caller_bounds| {
292 impl<'a, 'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::ParamEnvAnd<'a, T> {
293 type Lifted = ty::ParamEnvAnd<'tcx, T::Lifted>;
294 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
295 tcx.lift(&self.param_env).and_then(|param_env| {
296 tcx.lift(&self.value).map(|value| {
306 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
307 type Lifted = ty::ClosureSubsts<'tcx>;
308 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
309 tcx.lift(&self.substs).map(|substs| {
310 ty::ClosureSubsts { substs }
315 impl<'a, 'tcx> Lift<'tcx> for ty::GeneratorSubsts<'a> {
316 type Lifted = ty::GeneratorSubsts<'tcx>;
317 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
318 tcx.lift(&self.substs).map(|substs| {
319 ty::GeneratorSubsts { substs }
324 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjustment<'a> {
325 type Lifted = ty::adjustment::Adjustment<'tcx>;
326 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
327 tcx.lift(&self.kind).and_then(|kind| {
328 tcx.lift(&self.target).map(|target| {
329 ty::adjustment::Adjustment { kind, target }
335 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> {
336 type Lifted = ty::adjustment::Adjust<'tcx>;
337 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
339 ty::adjustment::Adjust::NeverToAny =>
340 Some(ty::adjustment::Adjust::NeverToAny),
341 ty::adjustment::Adjust::ReifyFnPointer =>
342 Some(ty::adjustment::Adjust::ReifyFnPointer),
343 ty::adjustment::Adjust::UnsafeFnPointer =>
344 Some(ty::adjustment::Adjust::UnsafeFnPointer),
345 ty::adjustment::Adjust::ClosureFnPointer =>
346 Some(ty::adjustment::Adjust::ClosureFnPointer),
347 ty::adjustment::Adjust::MutToConstPointer =>
348 Some(ty::adjustment::Adjust::MutToConstPointer),
349 ty::adjustment::Adjust::Unsize =>
350 Some(ty::adjustment::Adjust::Unsize),
351 ty::adjustment::Adjust::Deref(ref overloaded) => {
352 tcx.lift(overloaded).map(ty::adjustment::Adjust::Deref)
354 ty::adjustment::Adjust::Borrow(ref autoref) => {
355 tcx.lift(autoref).map(ty::adjustment::Adjust::Borrow)
361 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::OverloadedDeref<'a> {
362 type Lifted = ty::adjustment::OverloadedDeref<'tcx>;
363 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
364 tcx.lift(&self.region).map(|region| {
365 ty::adjustment::OverloadedDeref {
373 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
374 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
375 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
377 ty::adjustment::AutoBorrow::Ref(r, m) => {
378 tcx.lift(&r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
380 ty::adjustment::AutoBorrow::RawPtr(m) => {
381 Some(ty::adjustment::AutoBorrow::RawPtr(m))
387 impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
388 type Lifted = ty::GenSig<'tcx>;
389 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
390 tcx.lift(&(self.yield_ty, self.return_ty))
391 .map(|(yield_ty, return_ty)| {
400 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
401 type Lifted = ty::FnSig<'tcx>;
402 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
403 tcx.lift(&self.inputs_and_output).map(|x| {
405 inputs_and_output: x,
406 variadic: self.variadic,
407 unsafety: self.unsafety,
414 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
415 type Lifted = ty::error::ExpectedFound<T::Lifted>;
416 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
417 tcx.lift(&self.expected).and_then(|expected| {
418 tcx.lift(&self.found).map(|found| {
419 ty::error::ExpectedFound {
428 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
429 type Lifted = ty::error::TypeError<'tcx>;
430 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
431 use ty::error::TypeError::*;
434 Mismatch => Mismatch,
435 UnsafetyMismatch(x) => UnsafetyMismatch(x),
436 AbiMismatch(x) => AbiMismatch(x),
437 Mutability => Mutability,
438 TupleSize(x) => TupleSize(x),
439 FixedArraySize(x) => FixedArraySize(x),
440 ArgCount => ArgCount,
441 RegionsDoesNotOutlive(a, b) => {
442 return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b))
444 RegionsInsufficientlyPolymorphic(a, b) => {
445 return tcx.lift(&b).map(|b| RegionsInsufficientlyPolymorphic(a, b))
447 RegionsOverlyPolymorphic(a, b) => {
448 return tcx.lift(&b).map(|b| RegionsOverlyPolymorphic(a, b))
450 IntMismatch(x) => IntMismatch(x),
451 FloatMismatch(x) => FloatMismatch(x),
452 Traits(x) => Traits(x),
453 VariadicMismatch(x) => VariadicMismatch(x),
454 CyclicTy(t) => return tcx.lift(&t).map(|t| CyclicTy(t)),
455 ProjectionMismatched(x) => ProjectionMismatched(x),
456 ProjectionBoundsLength(x) => ProjectionBoundsLength(x),
457 Sorts(ref x) => return tcx.lift(x).map(Sorts),
458 ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch)
463 impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
464 type Lifted = ty::InstanceDef<'tcx>;
465 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
467 ty::InstanceDef::Item(def_id) =>
468 Some(ty::InstanceDef::Item(def_id)),
469 ty::InstanceDef::Intrinsic(def_id) =>
470 Some(ty::InstanceDef::Intrinsic(def_id)),
471 ty::InstanceDef::FnPtrShim(def_id, ref ty) =>
472 Some(ty::InstanceDef::FnPtrShim(def_id, tcx.lift(ty)?)),
473 ty::InstanceDef::Virtual(def_id, n) =>
474 Some(ty::InstanceDef::Virtual(def_id, n)),
475 ty::InstanceDef::ClosureOnceShim { call_once } =>
476 Some(ty::InstanceDef::ClosureOnceShim { call_once }),
477 ty::InstanceDef::DropGlue(def_id, ref ty) =>
478 Some(ty::InstanceDef::DropGlue(def_id, tcx.lift(ty)?)),
479 ty::InstanceDef::CloneShim(def_id, ref ty) =>
480 Some(ty::InstanceDef::CloneShim(def_id, tcx.lift(ty)?)),
485 BraceStructLiftImpl! {
486 impl<'a, 'tcx> Lift<'tcx> for ty::Instance<'a> {
487 type Lifted = ty::Instance<'tcx>;
492 BraceStructLiftImpl! {
493 impl<'a, 'tcx> Lift<'tcx> for interpret::GlobalId<'a> {
494 type Lifted = interpret::GlobalId<'tcx>;
499 ///////////////////////////////////////////////////////////////////////////
500 // TypeFoldable implementations.
502 // Ideally, each type should invoke `folder.fold_foo(self)` and
503 // nothing else. In some cases, though, we haven't gotten around to
504 // adding methods on the `folder` yet, and thus the folding is
505 // hard-coded here. This is less-flexible, because folders cannot
506 // override the behavior, but there are a lot of random types and one
507 // can easily refactor the folding into the TypeFolder trait as
510 /// AdtDefs are basically the same as a DefId.
511 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::AdtDef {
512 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
516 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
521 impl<'tcx, T:TypeFoldable<'tcx>, U:TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
522 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> (T, U) {
523 (self.0.fold_with(folder), self.1.fold_with(folder))
526 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
527 self.0.visit_with(visitor) || self.1.visit_with(visitor)
531 EnumTypeFoldableImpl! {
532 impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
535 } where T: TypeFoldable<'tcx>
538 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
539 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
540 Rc::new((**self).fold_with(folder))
543 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
544 (**self).visit_with(visitor)
548 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
549 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
550 let content: T = (**self).fold_with(folder);
554 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
555 (**self).visit_with(visitor)
559 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
560 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
561 self.iter().map(|t| t.fold_with(folder)).collect()
564 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
565 self.iter().any(|t| t.visit_with(visitor))
569 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<[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::<Vec<_>>().into_boxed_slice()
574 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
575 self.iter().any(|t| t.visit_with(visitor))
579 impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
580 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
581 self.map_bound_ref(|ty| ty.fold_with(folder))
584 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
585 folder.fold_binder(self)
588 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
589 self.skip_binder().visit_with(visitor)
592 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
593 visitor.visit_binder(self)
597 BraceStructTypeFoldableImpl! {
598 impl<'tcx> TypeFoldable<'tcx> for ty::ParamEnv<'tcx> { reveal, caller_bounds }
601 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
602 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
603 let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
604 folder.tcx().intern_existential_predicates(&v)
607 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
608 self.iter().any(|p| p.visit_with(visitor))
612 EnumTypeFoldableImpl! {
613 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialPredicate<'tcx> {
614 (ty::ExistentialPredicate::Trait)(a),
615 (ty::ExistentialPredicate::Projection)(a),
616 (ty::ExistentialPredicate::AutoTrait)(a),
620 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
621 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
622 let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
623 folder.tcx().intern_type_list(&v)
626 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
627 self.iter().any(|t| t.visit_with(visitor))
631 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind<'tcx>> {
632 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
633 let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
634 folder.tcx().intern_projs(&v)
637 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
638 self.iter().any(|t| t.visit_with(visitor))
642 impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
643 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
644 use ty::InstanceDef::*;
646 substs: self.substs.fold_with(folder),
647 def: match self.def {
648 Item(did) => Item(did.fold_with(folder)),
649 Intrinsic(did) => Intrinsic(did.fold_with(folder)),
650 FnPtrShim(did, ty) => FnPtrShim(
651 did.fold_with(folder),
652 ty.fold_with(folder),
654 Virtual(did, i) => Virtual(
655 did.fold_with(folder),
658 ClosureOnceShim { call_once } => ClosureOnceShim {
659 call_once: call_once.fold_with(folder),
661 DropGlue(did, ty) => DropGlue(
662 did.fold_with(folder),
663 ty.fold_with(folder),
665 CloneShim(did, ty) => CloneShim(
666 did.fold_with(folder),
667 ty.fold_with(folder),
673 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
674 use ty::InstanceDef::*;
675 self.substs.visit_with(visitor) ||
677 Item(did) | Intrinsic(did) | Virtual(did, _) => {
678 did.visit_with(visitor)
680 FnPtrShim(did, ty) | CloneShim(did, ty) => {
681 did.visit_with(visitor) || ty.visit_with(visitor)
683 DropGlue(did, ty) => {
684 did.visit_with(visitor) || ty.visit_with(visitor)
686 ClosureOnceShim { call_once } => call_once.visit_with(visitor),
691 impl<'tcx> TypeFoldable<'tcx> for interpret::GlobalId<'tcx> {
692 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
694 instance: self.instance.fold_with(folder),
695 promoted: self.promoted
699 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
700 self.instance.visit_with(visitor)
704 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
705 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
706 let sty = match self.sty {
707 ty::RawPtr(tm) => ty::RawPtr(tm.fold_with(folder)),
708 ty::Array(typ, sz) => ty::Array(typ.fold_with(folder), sz.fold_with(folder)),
709 ty::Slice(typ) => ty::Slice(typ.fold_with(folder)),
710 ty::Adt(tid, substs) => ty::Adt(tid, substs.fold_with(folder)),
711 ty::Dynamic(ref trait_ty, ref region) =>
712 ty::Dynamic(trait_ty.fold_with(folder), region.fold_with(folder)),
713 ty::Tuple(ts) => ty::Tuple(ts.fold_with(folder)),
714 ty::FnDef(def_id, substs) => {
715 ty::FnDef(def_id, substs.fold_with(folder))
717 ty::FnPtr(f) => ty::FnPtr(f.fold_with(folder)),
718 ty::Ref(ref r, ty, mutbl) => {
719 ty::Ref(r.fold_with(folder), ty.fold_with(folder), mutbl)
721 ty::Generator(did, substs, movability) => {
724 substs.fold_with(folder),
727 ty::GeneratorWitness(types) => ty::GeneratorWitness(types.fold_with(folder)),
728 ty::Closure(did, substs) => ty::Closure(did, substs.fold_with(folder)),
729 ty::Projection(ref data) => ty::Projection(data.fold_with(folder)),
730 ty::UnnormalizedProjection(ref data) => {
731 ty::UnnormalizedProjection(data.fold_with(folder))
733 ty::Opaque(did, substs) => ty::Opaque(did, substs.fold_with(folder)),
734 ty::Bool | ty::Char | ty::Str | ty::Int(_) |
735 ty::Uint(_) | ty::Float(_) | ty::Error | ty::Infer(_) |
736 ty::Param(..) | ty::Never | ty::Foreign(..) => return self
742 folder.tcx().mk_ty(sty)
746 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
747 folder.fold_ty(*self)
750 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
752 ty::RawPtr(ref tm) => tm.visit_with(visitor),
753 ty::Array(typ, sz) => typ.visit_with(visitor) || sz.visit_with(visitor),
754 ty::Slice(typ) => typ.visit_with(visitor),
755 ty::Adt(_, substs) => substs.visit_with(visitor),
756 ty::Dynamic(ref trait_ty, ref reg) =>
757 trait_ty.visit_with(visitor) || reg.visit_with(visitor),
758 ty::Tuple(ts) => ts.visit_with(visitor),
759 ty::FnDef(_, substs) => substs.visit_with(visitor),
760 ty::FnPtr(ref f) => f.visit_with(visitor),
761 ty::Ref(r, ty, _) => r.visit_with(visitor) || ty.visit_with(visitor),
762 ty::Generator(_did, ref substs, _) => {
763 substs.visit_with(visitor)
765 ty::GeneratorWitness(ref types) => types.visit_with(visitor),
766 ty::Closure(_did, ref substs) => substs.visit_with(visitor),
767 ty::Projection(ref data) | ty::UnnormalizedProjection(ref data) => {
768 data.visit_with(visitor)
770 ty::Opaque(_, ref substs) => substs.visit_with(visitor),
771 ty::Bool | ty::Char | ty::Str | ty::Int(_) |
772 ty::Uint(_) | ty::Float(_) | ty::Error | ty::Infer(_) |
773 ty::Param(..) | ty::Never | ty::Foreign(..) => false,
777 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
778 visitor.visit_ty(self)
782 BraceStructTypeFoldableImpl! {
783 impl<'tcx> TypeFoldable<'tcx> for ty::TypeAndMut<'tcx> {
788 BraceStructTypeFoldableImpl! {
789 impl<'tcx> TypeFoldable<'tcx> for ty::GenSig<'tcx> {
794 BraceStructTypeFoldableImpl! {
795 impl<'tcx> TypeFoldable<'tcx> for ty::FnSig<'tcx> {
796 inputs_and_output, variadic, unsafety, abi
800 BraceStructTypeFoldableImpl! {
801 impl<'tcx> TypeFoldable<'tcx> for ty::TraitRef<'tcx> { def_id, substs }
804 BraceStructTypeFoldableImpl! {
805 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialTraitRef<'tcx> { def_id, substs }
808 BraceStructTypeFoldableImpl! {
809 impl<'tcx> TypeFoldable<'tcx> for ty::ImplHeader<'tcx> {
817 impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
818 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
822 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
823 folder.fold_region(*self)
826 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
830 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
831 visitor.visit_region(*self)
835 BraceStructTypeFoldableImpl! {
836 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureSubsts<'tcx> {
841 BraceStructTypeFoldableImpl! {
842 impl<'tcx> TypeFoldable<'tcx> for ty::GeneratorSubsts<'tcx> {
847 BraceStructTypeFoldableImpl! {
848 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::Adjustment<'tcx> {
854 EnumTypeFoldableImpl! {
855 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::Adjust<'tcx> {
856 (ty::adjustment::Adjust::NeverToAny),
857 (ty::adjustment::Adjust::ReifyFnPointer),
858 (ty::adjustment::Adjust::UnsafeFnPointer),
859 (ty::adjustment::Adjust::ClosureFnPointer),
860 (ty::adjustment::Adjust::MutToConstPointer),
861 (ty::adjustment::Adjust::Unsize),
862 (ty::adjustment::Adjust::Deref)(a),
863 (ty::adjustment::Adjust::Borrow)(a),
867 BraceStructTypeFoldableImpl! {
868 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::OverloadedDeref<'tcx> {
873 EnumTypeFoldableImpl! {
874 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::AutoBorrow<'tcx> {
875 (ty::adjustment::AutoBorrow::Ref)(a, b),
876 (ty::adjustment::AutoBorrow::RawPtr)(m),
880 BraceStructTypeFoldableImpl! {
881 impl<'tcx> TypeFoldable<'tcx> for ty::GenericPredicates<'tcx> {
886 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
887 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
888 let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
889 folder.tcx().intern_predicates(&v)
892 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
893 self.iter().any(|p| p.visit_with(visitor))
897 EnumTypeFoldableImpl! {
898 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
899 (ty::Predicate::Trait)(a),
900 (ty::Predicate::Subtype)(a),
901 (ty::Predicate::RegionOutlives)(a),
902 (ty::Predicate::TypeOutlives)(a),
903 (ty::Predicate::Projection)(a),
904 (ty::Predicate::WellFormed)(a),
905 (ty::Predicate::ClosureKind)(a, b, c),
906 (ty::Predicate::ObjectSafe)(a),
907 (ty::Predicate::ConstEvaluatable)(a, b),
911 BraceStructTypeFoldableImpl! {
912 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionPredicate<'tcx> {
917 BraceStructTypeFoldableImpl! {
918 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialProjection<'tcx> {
919 ty, substs, item_def_id
923 BraceStructTypeFoldableImpl! {
924 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionTy<'tcx> {
929 BraceStructTypeFoldableImpl! {
930 impl<'tcx> TypeFoldable<'tcx> for ty::InstantiatedPredicates<'tcx> {
935 BraceStructTypeFoldableImpl! {
936 impl<'tcx, T> TypeFoldable<'tcx> for ty::ParamEnvAnd<'tcx, T> {
938 } where T: TypeFoldable<'tcx>
941 BraceStructTypeFoldableImpl! {
942 impl<'tcx> TypeFoldable<'tcx> for ty::SubtypePredicate<'tcx> {
947 BraceStructTypeFoldableImpl! {
948 impl<'tcx> TypeFoldable<'tcx> for ty::TraitPredicate<'tcx> {
953 TupleStructTypeFoldableImpl! {
954 impl<'tcx,T,U> TypeFoldable<'tcx> for ty::OutlivesPredicate<T,U> {
956 } where T : TypeFoldable<'tcx>, U : TypeFoldable<'tcx>,
959 BraceStructTypeFoldableImpl! {
960 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureUpvar<'tcx> {
965 BraceStructTypeFoldableImpl! {
966 impl<'tcx, T> TypeFoldable<'tcx> for ty::error::ExpectedFound<T> {
968 } where T: TypeFoldable<'tcx>
971 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
972 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
973 self.iter().map(|x| x.fold_with(folder)).collect()
976 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
977 self.iter().any(|t| t.visit_with(visitor))
981 EnumTypeFoldableImpl! {
982 impl<'tcx> TypeFoldable<'tcx> for ty::error::TypeError<'tcx> {
983 (ty::error::TypeError::Mismatch),
984 (ty::error::TypeError::UnsafetyMismatch)(x),
985 (ty::error::TypeError::AbiMismatch)(x),
986 (ty::error::TypeError::Mutability),
987 (ty::error::TypeError::TupleSize)(x),
988 (ty::error::TypeError::FixedArraySize)(x),
989 (ty::error::TypeError::ArgCount),
990 (ty::error::TypeError::RegionsDoesNotOutlive)(a, b),
991 (ty::error::TypeError::RegionsInsufficientlyPolymorphic)(a, b),
992 (ty::error::TypeError::RegionsOverlyPolymorphic)(a, b),
993 (ty::error::TypeError::IntMismatch)(x),
994 (ty::error::TypeError::FloatMismatch)(x),
995 (ty::error::TypeError::Traits)(x),
996 (ty::error::TypeError::VariadicMismatch)(x),
997 (ty::error::TypeError::CyclicTy)(t),
998 (ty::error::TypeError::ProjectionMismatched)(x),
999 (ty::error::TypeError::ProjectionBoundsLength)(x),
1000 (ty::error::TypeError::Sorts)(x),
1001 (ty::error::TypeError::ExistentialMismatch)(x),
1005 impl<'tcx> TypeFoldable<'tcx> for ConstValue<'tcx> {
1006 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1008 ConstValue::Scalar(v) => ConstValue::Scalar(v),
1009 ConstValue::ScalarPair(a, b) => ConstValue::ScalarPair(a, b),
1010 ConstValue::ByRef(id, alloc, offset) => ConstValue::ByRef(id, alloc, offset),
1011 ConstValue::Unevaluated(def_id, substs) => {
1012 ConstValue::Unevaluated(def_id, substs.fold_with(folder))
1017 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1019 ConstValue::Scalar(_) |
1020 ConstValue::ScalarPair(_, _) |
1021 ConstValue::ByRef(_, _, _) => false,
1022 ConstValue::Unevaluated(_, substs) => substs.visit_with(visitor),
1027 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
1028 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1029 let ty = self.ty.fold_with(folder);
1030 let val = self.val.fold_with(folder);
1031 folder.tcx().mk_const(ty::Const {
1037 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
1038 folder.fold_const(*self)
1041 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1042 self.ty.visit_with(visitor) || self.val.visit_with(visitor)
1045 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
1046 visitor.visit_const(self)