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 use infer::type_variable;
12 use ty::{self, Lift, Ty, TyCtxt};
13 use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
14 use rustc_data_structures::accumulate_vec::AccumulateVec;
15 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
22 ///////////////////////////////////////////////////////////////////////////
23 // Lift implementations
25 impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>> Lift<'tcx> for (A, B) {
26 type Lifted = (A::Lifted, B::Lifted);
27 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
28 tcx.lift(&self.0).and_then(|a| tcx.lift(&self.1).map(|b| (a, b)))
32 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
33 type Lifted = Option<T::Lifted>;
34 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
36 Some(ref x) => tcx.lift(x).map(Some),
42 impl<'tcx, T: Lift<'tcx>, E: Lift<'tcx>> Lift<'tcx> for Result<T, E> {
43 type Lifted = Result<T::Lifted, E::Lifted>;
44 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
46 Ok(ref x) => tcx.lift(x).map(Ok),
47 Err(ref e) => tcx.lift(e).map(Err)
52 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for [T] {
53 type Lifted = Vec<T::Lifted>;
54 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
55 // type annotation needed to inform `projection_must_outlive`
56 let mut result : Vec<<T as Lift<'tcx>>::Lifted>
57 = Vec::with_capacity(self.len());
59 if let Some(value) = tcx.lift(x) {
69 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Vec<T> {
70 type Lifted = Vec<T::Lifted>;
71 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
76 impl<'a, 'tcx> Lift<'tcx> for ty::TraitRef<'a> {
77 type Lifted = ty::TraitRef<'tcx>;
78 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
79 tcx.lift(&self.substs).map(|substs| ty::TraitRef {
86 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialTraitRef<'a> {
87 type Lifted = ty::ExistentialTraitRef<'tcx>;
88 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
89 tcx.lift(&self.substs).map(|substs| ty::ExistentialTraitRef {
96 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
97 type Lifted = ty::TraitPredicate<'tcx>;
98 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
99 -> Option<ty::TraitPredicate<'tcx>> {
100 tcx.lift(&self.trait_ref).map(|trait_ref| ty::TraitPredicate {
106 impl<'a, 'tcx> Lift<'tcx> for ty::EquatePredicate<'a> {
107 type Lifted = ty::EquatePredicate<'tcx>;
108 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
109 -> Option<ty::EquatePredicate<'tcx>> {
110 tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::EquatePredicate(a, b))
114 impl<'a, 'tcx> Lift<'tcx> for ty::SubtypePredicate<'a> {
115 type Lifted = ty::SubtypePredicate<'tcx>;
116 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
117 -> Option<ty::SubtypePredicate<'tcx>> {
118 tcx.lift(&(self.a, self.b)).map(|(a, b)| ty::SubtypePredicate {
119 a_is_expected: self.a_is_expected,
126 impl<'tcx, A: Copy+Lift<'tcx>, B: Copy+Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
127 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
128 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
129 tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
133 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionTy<'a> {
134 type Lifted = ty::ProjectionTy<'tcx>;
135 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
136 -> Option<ty::ProjectionTy<'tcx>> {
137 tcx.lift(&self.trait_ref).map(|trait_ref| {
139 trait_ref: trait_ref,
140 item_name: self.item_name
146 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
147 type Lifted = ty::ProjectionPredicate<'tcx>;
148 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>)
149 -> Option<ty::ProjectionPredicate<'tcx>> {
150 tcx.lift(&(self.projection_ty, self.ty)).map(|(projection_ty, ty)| {
151 ty::ProjectionPredicate {
152 projection_ty: projection_ty,
159 impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
160 type Lifted = ty::ExistentialProjection<'tcx>;
161 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
162 tcx.lift(&(self.trait_ref, self.ty)).map(|(trait_ref, ty)| {
163 ty::ExistentialProjection {
164 trait_ref: trait_ref,
165 item_name: self.item_name,
172 impl<'a, 'tcx> Lift<'tcx> for ty::Predicate<'a> {
173 type Lifted = ty::Predicate<'tcx>;
174 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
176 ty::Predicate::Trait(ref binder) => {
177 tcx.lift(binder).map(ty::Predicate::Trait)
179 ty::Predicate::Equate(ref binder) => {
180 tcx.lift(binder).map(ty::Predicate::Equate)
182 ty::Predicate::Subtype(ref binder) => {
183 tcx.lift(binder).map(ty::Predicate::Subtype)
185 ty::Predicate::RegionOutlives(ref binder) => {
186 tcx.lift(binder).map(ty::Predicate::RegionOutlives)
188 ty::Predicate::TypeOutlives(ref binder) => {
189 tcx.lift(binder).map(ty::Predicate::TypeOutlives)
191 ty::Predicate::Projection(ref binder) => {
192 tcx.lift(binder).map(ty::Predicate::Projection)
194 ty::Predicate::WellFormed(ty) => {
195 tcx.lift(&ty).map(ty::Predicate::WellFormed)
197 ty::Predicate::ClosureKind(closure_def_id, kind) => {
198 Some(ty::Predicate::ClosureKind(closure_def_id, kind))
200 ty::Predicate::ObjectSafe(trait_def_id) => {
201 Some(ty::Predicate::ObjectSafe(trait_def_id))
207 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
208 type Lifted = ty::Binder<T::Lifted>;
209 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
210 tcx.lift(&self.0).map(|x| ty::Binder(x))
214 impl<'a, 'tcx> Lift<'tcx> for ty::ClosureSubsts<'a> {
215 type Lifted = ty::ClosureSubsts<'tcx>;
216 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
217 tcx.lift(&self.substs).map(|substs| {
218 ty::ClosureSubsts { substs: substs }
223 impl<'a, 'tcx> Lift<'tcx> for ty::ItemSubsts<'a> {
224 type Lifted = ty::ItemSubsts<'tcx>;
225 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
226 tcx.lift(&self.substs).map(|substs| {
234 impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
235 type Lifted = ty::adjustment::AutoBorrow<'tcx>;
236 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
238 ty::adjustment::AutoBorrow::Ref(r, m) => {
239 tcx.lift(&r).map(|r| ty::adjustment::AutoBorrow::Ref(r, m))
241 ty::adjustment::AutoBorrow::RawPtr(m) => {
242 Some(ty::adjustment::AutoBorrow::RawPtr(m))
248 impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
249 type Lifted = ty::FnSig<'tcx>;
250 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
251 tcx.lift(&self.inputs_and_output).map(|x| {
253 inputs_and_output: x,
254 variadic: self.variadic,
255 unsafety: self.unsafety,
262 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::error::ExpectedFound<T> {
263 type Lifted = ty::error::ExpectedFound<T::Lifted>;
264 fn lift_to_tcx<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Option<Self::Lifted> {
265 tcx.lift(&self.expected).and_then(|expected| {
266 tcx.lift(&self.found).map(|found| {
267 ty::error::ExpectedFound {
276 impl<'a, 'tcx> Lift<'tcx> for type_variable::Default<'a> {
277 type Lifted = type_variable::Default<'tcx>;
278 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
279 tcx.lift(&self.ty).map(|ty| {
280 type_variable::Default {
282 origin_span: self.origin_span,
289 impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
290 type Lifted = ty::error::TypeError<'tcx>;
291 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
292 use ty::error::TypeError::*;
295 Mismatch => Mismatch,
296 UnsafetyMismatch(x) => UnsafetyMismatch(x),
297 AbiMismatch(x) => AbiMismatch(x),
298 Mutability => Mutability,
299 TupleSize(x) => TupleSize(x),
300 FixedArraySize(x) => FixedArraySize(x),
301 ArgCount => ArgCount,
302 RegionsDoesNotOutlive(a, b) => {
303 return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b))
305 RegionsNotSame(a, b) => {
306 return tcx.lift(&(a, b)).map(|(a, b)| RegionsNotSame(a, b))
308 RegionsNoOverlap(a, b) => {
309 return tcx.lift(&(a, b)).map(|(a, b)| RegionsNoOverlap(a, b))
311 RegionsInsufficientlyPolymorphic(a, b, ref c) => {
313 return tcx.lift(&b).map(|b| RegionsInsufficientlyPolymorphic(a, b, c))
315 RegionsOverlyPolymorphic(a, b, ref c) => {
317 return tcx.lift(&b).map(|b| RegionsOverlyPolymorphic(a, b, c))
319 IntMismatch(x) => IntMismatch(x),
320 FloatMismatch(x) => FloatMismatch(x),
321 Traits(x) => Traits(x),
322 VariadicMismatch(x) => VariadicMismatch(x),
323 CyclicTy => CyclicTy,
324 ProjectionNameMismatched(x) => ProjectionNameMismatched(x),
325 ProjectionBoundsLength(x) => ProjectionBoundsLength(x),
327 Sorts(ref x) => return tcx.lift(x).map(Sorts),
328 TyParamDefaultMismatch(ref x) => {
329 return tcx.lift(x).map(TyParamDefaultMismatch)
331 ExistentialMismatch(ref x) => return tcx.lift(x).map(ExistentialMismatch)
336 ///////////////////////////////////////////////////////////////////////////
337 // TypeFoldable implementations.
339 // Ideally, each type should invoke `folder.fold_foo(self)` and
340 // nothing else. In some cases, though, we haven't gotten around to
341 // adding methods on the `folder` yet, and thus the folding is
342 // hard-coded here. This is less-flexible, because folders cannot
343 // override the behavior, but there are a lot of random types and one
344 // can easily refactor the folding into the TypeFolder trait as
347 macro_rules! CopyImpls {
350 impl<'tcx> TypeFoldable<'tcx> for $ty {
351 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _: &mut F) -> $ty {
355 fn super_visit_with<F: TypeVisitor<'tcx>>(&self, _: &mut F) -> bool {
363 CopyImpls! { (), hir::Unsafety, abi::Abi }
365 impl<'tcx, T:TypeFoldable<'tcx>, U:TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
366 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> (T, U) {
367 (self.0.fold_with(folder), self.1.fold_with(folder))
370 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
371 self.0.visit_with(visitor) || self.1.visit_with(visitor)
375 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Option<T> {
376 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
377 self.as_ref().map(|t| t.fold_with(folder))
380 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
381 self.iter().any(|t| t.visit_with(visitor))
385 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
386 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
387 Rc::new((**self).fold_with(folder))
390 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
391 (**self).visit_with(visitor)
395 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
396 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
397 let content: T = (**self).fold_with(folder);
401 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
402 (**self).visit_with(visitor)
406 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
407 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
408 self.iter().map(|t| t.fold_with(folder)).collect()
411 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
412 self.iter().any(|t| t.visit_with(visitor))
416 impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
417 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
418 ty::Binder(self.0.fold_with(folder))
421 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
422 folder.fold_binder(self)
425 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
426 self.0.visit_with(visitor)
429 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
430 visitor.visit_binder(self)
434 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Slice<ty::ExistentialPredicate<'tcx>> {
435 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
436 let v = self.iter().map(|p| p.fold_with(folder)).collect::<AccumulateVec<[_; 8]>>();
437 folder.tcx().intern_existential_predicates(&v)
440 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
441 self.iter().any(|p| p.visit_with(visitor))
445 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialPredicate<'tcx> {
446 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
447 use ty::ExistentialPredicate::*;
449 Trait(ref tr) => Trait(tr.fold_with(folder)),
450 Projection(ref p) => Projection(p.fold_with(folder)),
451 AutoTrait(did) => AutoTrait(did),
455 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
457 ty::ExistentialPredicate::Trait(ref tr) => tr.visit_with(visitor),
458 ty::ExistentialPredicate::Projection(ref p) => p.visit_with(visitor),
459 ty::ExistentialPredicate::AutoTrait(_) => false,
464 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Slice<Ty<'tcx>> {
465 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
466 let v = self.iter().map(|t| t.fold_with(folder)).collect::<AccumulateVec<[_; 8]>>();
467 folder.tcx().intern_type_list(&v)
470 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
471 self.iter().any(|t| t.visit_with(visitor))
475 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
476 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
477 let sty = match self.sty {
478 ty::TyRawPtr(tm) => ty::TyRawPtr(tm.fold_with(folder)),
479 ty::TyArray(typ, sz) => ty::TyArray(typ.fold_with(folder), sz),
480 ty::TySlice(typ) => ty::TySlice(typ.fold_with(folder)),
481 ty::TyAdt(tid, substs) => ty::TyAdt(tid, substs.fold_with(folder)),
482 ty::TyDynamic(ref trait_ty, ref region) =>
483 ty::TyDynamic(trait_ty.fold_with(folder), region.fold_with(folder)),
484 ty::TyTuple(ts, defaulted) => ty::TyTuple(ts.fold_with(folder), defaulted),
485 ty::TyFnDef(def_id, substs, f) => {
487 substs.fold_with(folder),
490 ty::TyFnPtr(f) => ty::TyFnPtr(f.fold_with(folder)),
491 ty::TyRef(ref r, tm) => {
492 ty::TyRef(r.fold_with(folder), tm.fold_with(folder))
494 ty::TyClosure(did, substs) => ty::TyClosure(did, substs.fold_with(folder)),
495 ty::TyProjection(ref data) => ty::TyProjection(data.fold_with(folder)),
496 ty::TyAnon(did, substs) => ty::TyAnon(did, substs.fold_with(folder)),
497 ty::TyBool | ty::TyChar | ty::TyStr | ty::TyInt(_) |
498 ty::TyUint(_) | ty::TyFloat(_) | ty::TyError | ty::TyInfer(_) |
499 ty::TyParam(..) | ty::TyNever => return self
505 folder.tcx().mk_ty(sty)
509 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
510 folder.fold_ty(*self)
513 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
515 ty::TyRawPtr(ref tm) => tm.visit_with(visitor),
516 ty::TyArray(typ, _sz) => typ.visit_with(visitor),
517 ty::TySlice(typ) => typ.visit_with(visitor),
518 ty::TyAdt(_, substs) => substs.visit_with(visitor),
519 ty::TyDynamic(ref trait_ty, ref reg) =>
520 trait_ty.visit_with(visitor) || reg.visit_with(visitor),
521 ty::TyTuple(ts, _) => ts.visit_with(visitor),
522 ty::TyFnDef(_, substs, ref f) => {
523 substs.visit_with(visitor) || f.visit_with(visitor)
525 ty::TyFnPtr(ref f) => f.visit_with(visitor),
526 ty::TyRef(r, ref tm) => r.visit_with(visitor) || tm.visit_with(visitor),
527 ty::TyClosure(_did, ref substs) => substs.visit_with(visitor),
528 ty::TyProjection(ref data) => data.visit_with(visitor),
529 ty::TyAnon(_, ref substs) => substs.visit_with(visitor),
530 ty::TyBool | ty::TyChar | ty::TyStr | ty::TyInt(_) |
531 ty::TyUint(_) | ty::TyFloat(_) | ty::TyError | ty::TyInfer(_) |
532 ty::TyParam(..) | ty::TyNever => false,
536 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
537 visitor.visit_ty(self)
541 impl<'tcx> TypeFoldable<'tcx> for ty::TypeAndMut<'tcx> {
542 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
543 ty::TypeAndMut { ty: self.ty.fold_with(folder), mutbl: self.mutbl }
546 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
550 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
551 self.ty.visit_with(visitor)
555 impl<'tcx> TypeFoldable<'tcx> for ty::FnSig<'tcx> {
556 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
557 let inputs_and_output = self.inputs_and_output.fold_with(folder);
559 inputs_and_output: folder.tcx().intern_type_list(&inputs_and_output),
560 variadic: self.variadic,
561 unsafety: self.unsafety,
566 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
567 folder.fold_fn_sig(self)
570 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
571 self.inputs().iter().any(|i| i.visit_with(visitor)) ||
572 self.output().visit_with(visitor)
576 impl<'tcx> TypeFoldable<'tcx> for ty::TraitRef<'tcx> {
577 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
580 substs: self.substs.fold_with(folder),
584 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
585 self.substs.visit_with(visitor)
588 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
589 visitor.visit_trait_ref(*self)
593 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialTraitRef<'tcx> {
594 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
595 ty::ExistentialTraitRef {
597 substs: self.substs.fold_with(folder),
601 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
602 self.substs.visit_with(visitor)
606 impl<'tcx> TypeFoldable<'tcx> for ty::ImplHeader<'tcx> {
607 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
609 impl_def_id: self.impl_def_id,
610 self_ty: self.self_ty.fold_with(folder),
611 trait_ref: self.trait_ref.map(|t| t.fold_with(folder)),
612 predicates: self.predicates.iter().map(|p| p.fold_with(folder)).collect(),
616 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
617 folder.fold_impl_header(self)
620 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
621 self.self_ty.visit_with(visitor) ||
622 self.trait_ref.map(|r| r.visit_with(visitor)).unwrap_or(false) ||
623 self.predicates.iter().any(|p| p.visit_with(visitor))
627 impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Region {
628 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self {
632 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
633 folder.fold_region(*self)
636 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
640 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
641 visitor.visit_region(*self)
645 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureSubsts<'tcx> {
646 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
648 substs: self.substs.fold_with(folder),
652 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
653 self.substs.visit_with(visitor)
657 impl<'tcx> TypeFoldable<'tcx> for ty::ItemSubsts<'tcx> {
658 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
660 substs: self.substs.fold_with(folder),
664 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
665 self.substs.visit_with(visitor)
669 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::AutoBorrow<'tcx> {
670 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
672 ty::adjustment::AutoBorrow::Ref(ref r, m) => {
673 ty::adjustment::AutoBorrow::Ref(r.fold_with(folder), m)
675 ty::adjustment::AutoBorrow::RawPtr(m) => ty::adjustment::AutoBorrow::RawPtr(m)
679 fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
680 folder.fold_autoref(self)
683 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
685 ty::adjustment::AutoBorrow::Ref(r, _m) => r.visit_with(visitor),
686 ty::adjustment::AutoBorrow::RawPtr(_m) => false,
691 impl<'tcx> TypeFoldable<'tcx> for ty::GenericPredicates<'tcx> {
692 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
693 ty::GenericPredicates {
695 predicates: self.predicates.fold_with(folder),
699 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
700 self.predicates.visit_with(visitor)
704 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
705 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
707 ty::Predicate::Trait(ref a) =>
708 ty::Predicate::Trait(a.fold_with(folder)),
709 ty::Predicate::Equate(ref binder) =>
710 ty::Predicate::Equate(binder.fold_with(folder)),
711 ty::Predicate::Subtype(ref binder) =>
712 ty::Predicate::Subtype(binder.fold_with(folder)),
713 ty::Predicate::RegionOutlives(ref binder) =>
714 ty::Predicate::RegionOutlives(binder.fold_with(folder)),
715 ty::Predicate::TypeOutlives(ref binder) =>
716 ty::Predicate::TypeOutlives(binder.fold_with(folder)),
717 ty::Predicate::Projection(ref binder) =>
718 ty::Predicate::Projection(binder.fold_with(folder)),
719 ty::Predicate::WellFormed(data) =>
720 ty::Predicate::WellFormed(data.fold_with(folder)),
721 ty::Predicate::ClosureKind(closure_def_id, kind) =>
722 ty::Predicate::ClosureKind(closure_def_id, kind),
723 ty::Predicate::ObjectSafe(trait_def_id) =>
724 ty::Predicate::ObjectSafe(trait_def_id),
728 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
730 ty::Predicate::Trait(ref a) => a.visit_with(visitor),
731 ty::Predicate::Equate(ref binder) => binder.visit_with(visitor),
732 ty::Predicate::Subtype(ref binder) => binder.visit_with(visitor),
733 ty::Predicate::RegionOutlives(ref binder) => binder.visit_with(visitor),
734 ty::Predicate::TypeOutlives(ref binder) => binder.visit_with(visitor),
735 ty::Predicate::Projection(ref binder) => binder.visit_with(visitor),
736 ty::Predicate::WellFormed(data) => data.visit_with(visitor),
737 ty::Predicate::ClosureKind(_closure_def_id, _kind) => false,
738 ty::Predicate::ObjectSafe(_trait_def_id) => false,
743 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionPredicate<'tcx> {
744 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
745 ty::ProjectionPredicate {
746 projection_ty: self.projection_ty.fold_with(folder),
747 ty: self.ty.fold_with(folder),
751 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
752 self.projection_ty.visit_with(visitor) || self.ty.visit_with(visitor)
756 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialProjection<'tcx> {
757 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
758 ty::ExistentialProjection {
759 trait_ref: self.trait_ref.fold_with(folder),
760 item_name: self.item_name,
761 ty: self.ty.fold_with(folder),
765 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
766 self.trait_ref.visit_with(visitor) || self.ty.visit_with(visitor)
770 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionTy<'tcx> {
771 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
773 trait_ref: self.trait_ref.fold_with(folder),
774 item_name: self.item_name,
778 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
779 self.trait_ref.visit_with(visitor)
783 impl<'tcx> TypeFoldable<'tcx> for ty::InstantiatedPredicates<'tcx> {
784 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
785 ty::InstantiatedPredicates {
786 predicates: self.predicates.fold_with(folder),
790 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
791 self.predicates.visit_with(visitor)
795 impl<'tcx> TypeFoldable<'tcx> for ty::EquatePredicate<'tcx> {
796 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
797 ty::EquatePredicate(self.0.fold_with(folder), self.1.fold_with(folder))
800 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
801 self.0.visit_with(visitor) || self.1.visit_with(visitor)
805 impl<'tcx> TypeFoldable<'tcx> for ty::SubtypePredicate<'tcx> {
806 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
807 ty::SubtypePredicate {
808 a_is_expected: self.a_is_expected,
809 a: self.a.fold_with(folder),
810 b: self.b.fold_with(folder)
814 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
815 self.a.visit_with(visitor) || self.b.visit_with(visitor)
819 impl<'tcx> TypeFoldable<'tcx> for ty::TraitPredicate<'tcx> {
820 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
822 trait_ref: self.trait_ref.fold_with(folder)
826 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
827 self.trait_ref.visit_with(visitor)
831 impl<'tcx,T,U> TypeFoldable<'tcx> for ty::OutlivesPredicate<T,U>
832 where T : TypeFoldable<'tcx>,
833 U : TypeFoldable<'tcx>,
835 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
836 ty::OutlivesPredicate(self.0.fold_with(folder),
837 self.1.fold_with(folder))
840 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
841 self.0.visit_with(visitor) || self.1.visit_with(visitor)
845 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureUpvar<'tcx> {
846 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
850 ty: self.ty.fold_with(folder),
854 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
855 self.ty.visit_with(visitor)
859 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::error::ExpectedFound<T> {
860 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
861 ty::error::ExpectedFound {
862 expected: self.expected.fold_with(folder),
863 found: self.found.fold_with(folder),
867 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
868 self.expected.visit_with(visitor) || self.found.visit_with(visitor)
872 impl<'tcx, T: TypeFoldable<'tcx>, I: Idx> TypeFoldable<'tcx> for IndexVec<I, T> {
873 fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
874 self.iter().map(|x| x.fold_with(folder)).collect()
877 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
878 self.iter().any(|t| t.visit_with(visitor))