]> git.proxmox.com Git - rustc.git/blob - src/librustc/ty/structural_impls.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc / ty / structural_impls.rs
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.
4 //
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.
10
11 use ty::subst::{self, VecPerParamSpace};
12 use traits;
13 use ty::{self, Lift, TraitRef, Ty, TyCtxt};
14 use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
15
16 use std::rc::Rc;
17 use syntax::abi;
18 use syntax::ptr::P;
19
20 use hir;
21
22 ///////////////////////////////////////////////////////////////////////////
23 // Lift implementations
24
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(&self, tcx: &TyCtxt<'tcx>) -> Option<Self::Lifted> {
28 tcx.lift(&self.0).and_then(|a| tcx.lift(&self.1).map(|b| (a, b)))
29 }
30 }
31
32 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for [T] {
33 type Lifted = Vec<T::Lifted>;
34 fn lift_to_tcx(&self, tcx: &TyCtxt<'tcx>) -> Option<Self::Lifted> {
35 // type annotation needed to inform `projection_must_outlive`
36 let mut result : Vec<<T as Lift<'tcx>>::Lifted>
37 = Vec::with_capacity(self.len());
38 for x in self {
39 if let Some(value) = tcx.lift(x) {
40 result.push(value);
41 } else {
42 return None;
43 }
44 }
45 Some(result)
46 }
47 }
48
49 impl<'tcx> Lift<'tcx> for ty::Region {
50 type Lifted = Self;
51 fn lift_to_tcx(&self, _: &TyCtxt<'tcx>) -> Option<ty::Region> {
52 Some(*self)
53 }
54 }
55
56 impl<'a, 'tcx> Lift<'tcx> for TraitRef<'a> {
57 type Lifted = TraitRef<'tcx>;
58 fn lift_to_tcx(&self, tcx: &TyCtxt<'tcx>) -> Option<TraitRef<'tcx>> {
59 tcx.lift(&self.substs).map(|substs| TraitRef {
60 def_id: self.def_id,
61 substs: substs
62 })
63 }
64 }
65
66 impl<'a, 'tcx> Lift<'tcx> for ty::TraitPredicate<'a> {
67 type Lifted = ty::TraitPredicate<'tcx>;
68 fn lift_to_tcx(&self, tcx: &TyCtxt<'tcx>) -> Option<ty::TraitPredicate<'tcx>> {
69 tcx.lift(&self.trait_ref).map(|trait_ref| ty::TraitPredicate {
70 trait_ref: trait_ref
71 })
72 }
73 }
74
75 impl<'a, 'tcx> Lift<'tcx> for ty::EquatePredicate<'a> {
76 type Lifted = ty::EquatePredicate<'tcx>;
77 fn lift_to_tcx(&self, tcx: &TyCtxt<'tcx>) -> Option<ty::EquatePredicate<'tcx>> {
78 tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::EquatePredicate(a, b))
79 }
80 }
81
82 impl<'tcx, A: Copy+Lift<'tcx>, B: Copy+Lift<'tcx>> Lift<'tcx> for ty::OutlivesPredicate<A, B> {
83 type Lifted = ty::OutlivesPredicate<A::Lifted, B::Lifted>;
84 fn lift_to_tcx(&self, tcx: &TyCtxt<'tcx>) -> Option<Self::Lifted> {
85 tcx.lift(&(self.0, self.1)).map(|(a, b)| ty::OutlivesPredicate(a, b))
86 }
87 }
88
89 impl<'a, 'tcx> Lift<'tcx> for ty::ProjectionPredicate<'a> {
90 type Lifted = ty::ProjectionPredicate<'tcx>;
91 fn lift_to_tcx(&self, tcx: &TyCtxt<'tcx>) -> Option<ty::ProjectionPredicate<'tcx>> {
92 tcx.lift(&(self.projection_ty.trait_ref, self.ty)).map(|(trait_ref, ty)| {
93 ty::ProjectionPredicate {
94 projection_ty: ty::ProjectionTy {
95 trait_ref: trait_ref,
96 item_name: self.projection_ty.item_name
97 },
98 ty: ty
99 }
100 })
101 }
102 }
103
104 impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for ty::Binder<T> {
105 type Lifted = ty::Binder<T::Lifted>;
106 fn lift_to_tcx(&self, tcx: &TyCtxt<'tcx>) -> Option<Self::Lifted> {
107 tcx.lift(&self.0).map(|x| ty::Binder(x))
108 }
109 }
110
111 ///////////////////////////////////////////////////////////////////////////
112 // TypeFoldable implementations.
113 //
114 // Ideally, each type should invoke `folder.fold_foo(self)` and
115 // nothing else. In some cases, though, we haven't gotten around to
116 // adding methods on the `folder` yet, and thus the folding is
117 // hard-coded here. This is less-flexible, because folders cannot
118 // override the behavior, but there are a lot of random types and one
119 // can easily refactor the folding into the TypeFolder trait as
120 // needed.
121
122 macro_rules! CopyImpls {
123 ($($ty:ty),+) => {
124 $(
125 impl<'tcx> TypeFoldable<'tcx> for $ty {
126 fn super_fold_with<F:TypeFolder<'tcx>>(&self, _: &mut F) -> $ty {
127 *self
128 }
129
130 fn super_visit_with<F: TypeVisitor<'tcx>>(&self, _: &mut F) -> bool {
131 false
132 }
133 }
134 )+
135 }
136 }
137
138 CopyImpls! { (), hir::Unsafety, abi::Abi }
139
140 impl<'tcx, T:TypeFoldable<'tcx>, U:TypeFoldable<'tcx>> TypeFoldable<'tcx> for (T, U) {
141 fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> (T, U) {
142 (self.0.fold_with(folder), self.1.fold_with(folder))
143 }
144
145 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
146 self.0.visit_with(visitor) || self.1.visit_with(visitor)
147 }
148 }
149
150 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Option<T> {
151 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
152 self.as_ref().map(|t| t.fold_with(folder))
153 }
154
155 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
156 self.iter().any(|t| t.visit_with(visitor))
157 }
158 }
159
160 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Rc<T> {
161 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
162 Rc::new((**self).fold_with(folder))
163 }
164
165 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
166 (**self).visit_with(visitor)
167 }
168 }
169
170 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Box<T> {
171 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
172 let content: T = (**self).fold_with(folder);
173 box content
174 }
175
176 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
177 (**self).visit_with(visitor)
178 }
179 }
180
181 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for Vec<T> {
182 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
183 self.iter().map(|t| t.fold_with(folder)).collect()
184 }
185
186 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
187 self.iter().any(|t| t.visit_with(visitor))
188 }
189 }
190
191 impl<'tcx, T:TypeFoldable<'tcx>> TypeFoldable<'tcx> for ty::Binder<T> {
192 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
193 ty::Binder(self.0.fold_with(folder))
194 }
195
196 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
197 folder.fold_binder(self)
198 }
199
200 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
201 self.0.visit_with(visitor)
202 }
203
204 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
205 visitor.visit_binder(self)
206 }
207 }
208
209 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for P<[T]> {
210 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
211 self.iter().map(|t| t.fold_with(folder)).collect()
212 }
213
214 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
215 self.iter().any(|t| t.visit_with(visitor))
216 }
217 }
218
219 impl<'tcx, T: TypeFoldable<'tcx>> TypeFoldable<'tcx> for VecPerParamSpace<T> {
220 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
221 self.map(|elem| elem.fold_with(folder))
222 }
223
224 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
225 self.iter().any(|elem| elem.visit_with(visitor))
226 }
227 }
228
229 impl<'tcx> TypeFoldable<'tcx> for ty::TraitTy<'tcx> {
230 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
231 ty::TraitTy {
232 principal: self.principal.fold_with(folder),
233 bounds: self.bounds.fold_with(folder),
234 }
235 }
236
237 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
238 self.principal.visit_with(visitor) || self.bounds.visit_with(visitor)
239 }
240 }
241
242 impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
243 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
244 let sty = match self.sty {
245 ty::TyBox(typ) => ty::TyBox(typ.fold_with(folder)),
246 ty::TyRawPtr(ref tm) => ty::TyRawPtr(tm.fold_with(folder)),
247 ty::TyArray(typ, sz) => ty::TyArray(typ.fold_with(folder), sz),
248 ty::TySlice(typ) => ty::TySlice(typ.fold_with(folder)),
249 ty::TyEnum(tid, ref substs) => {
250 let substs = substs.fold_with(folder);
251 ty::TyEnum(tid, folder.tcx().mk_substs(substs))
252 }
253 ty::TyTrait(ref trait_ty) => ty::TyTrait(trait_ty.fold_with(folder)),
254 ty::TyTuple(ref ts) => ty::TyTuple(ts.fold_with(folder)),
255 ty::TyFnDef(def_id, substs, ref f) => {
256 let substs = substs.fold_with(folder);
257 let bfn = f.fold_with(folder);
258 ty::TyFnDef(def_id,
259 folder.tcx().mk_substs(substs),
260 folder.tcx().mk_bare_fn(bfn))
261 }
262 ty::TyFnPtr(ref f) => {
263 let bfn = f.fold_with(folder);
264 ty::TyFnPtr(folder.tcx().mk_bare_fn(bfn))
265 }
266 ty::TyRef(r, ref tm) => {
267 let r = r.fold_with(folder);
268 ty::TyRef(folder.tcx().mk_region(r), tm.fold_with(folder))
269 }
270 ty::TyStruct(did, ref substs) => {
271 let substs = substs.fold_with(folder);
272 ty::TyStruct(did, folder.tcx().mk_substs(substs))
273 }
274 ty::TyClosure(did, ref substs) => {
275 ty::TyClosure(did, substs.fold_with(folder))
276 }
277 ty::TyProjection(ref data) => ty::TyProjection(data.fold_with(folder)),
278 ty::TyBool | ty::TyChar | ty::TyStr | ty::TyInt(_) |
279 ty::TyUint(_) | ty::TyFloat(_) | ty::TyError | ty::TyInfer(_) |
280 ty::TyParam(..) => self.sty.clone(),
281 };
282 folder.tcx().mk_ty(sty)
283 }
284
285 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
286 folder.fold_ty(*self)
287 }
288
289 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
290 match self.sty {
291 ty::TyBox(typ) => typ.visit_with(visitor),
292 ty::TyRawPtr(ref tm) => tm.visit_with(visitor),
293 ty::TyArray(typ, _sz) => typ.visit_with(visitor),
294 ty::TySlice(typ) => typ.visit_with(visitor),
295 ty::TyEnum(_tid, ref substs) => substs.visit_with(visitor),
296 ty::TyTrait(ref trait_ty) => trait_ty.visit_with(visitor),
297 ty::TyTuple(ref ts) => ts.visit_with(visitor),
298 ty::TyFnDef(_, substs, ref f) => {
299 substs.visit_with(visitor) || f.visit_with(visitor)
300 }
301 ty::TyFnPtr(ref f) => f.visit_with(visitor),
302 ty::TyRef(r, ref tm) => r.visit_with(visitor) || tm.visit_with(visitor),
303 ty::TyStruct(_did, ref substs) => substs.visit_with(visitor),
304 ty::TyClosure(_did, ref substs) => substs.visit_with(visitor),
305 ty::TyProjection(ref data) => data.visit_with(visitor),
306 ty::TyBool | ty::TyChar | ty::TyStr | ty::TyInt(_) |
307 ty::TyUint(_) | ty::TyFloat(_) | ty::TyError | ty::TyInfer(_) |
308 ty::TyParam(..) => false,
309 }
310 }
311
312 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
313 visitor.visit_ty(self)
314 }
315 }
316
317 impl<'tcx> TypeFoldable<'tcx> for ty::BareFnTy<'tcx> {
318 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
319 ty::BareFnTy { sig: self.sig.fold_with(folder),
320 abi: self.abi,
321 unsafety: self.unsafety }
322 }
323
324 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
325 folder.fold_bare_fn_ty(self)
326 }
327
328 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
329 self.sig.visit_with(visitor)
330 }
331 }
332
333 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureTy<'tcx> {
334 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
335 ty::ClosureTy {
336 sig: self.sig.fold_with(folder),
337 unsafety: self.unsafety,
338 abi: self.abi,
339 }
340 }
341
342 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
343 folder.fold_closure_ty(self)
344 }
345
346 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
347 self.sig.visit_with(visitor)
348 }
349 }
350
351 impl<'tcx> TypeFoldable<'tcx> for ty::TypeAndMut<'tcx> {
352 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
353 ty::TypeAndMut { ty: self.ty.fold_with(folder), mutbl: self.mutbl }
354 }
355
356 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
357 folder.fold_mt(self)
358 }
359
360 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
361 self.ty.visit_with(visitor)
362 }
363 }
364
365 impl<'tcx> TypeFoldable<'tcx> for ty::FnOutput<'tcx> {
366 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
367 match *self {
368 ty::FnConverging(ref ty) => ty::FnConverging(ty.fold_with(folder)),
369 ty::FnDiverging => ty::FnDiverging
370 }
371 }
372
373 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
374 folder.fold_output(self)
375 }
376
377 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
378 match *self {
379 ty::FnConverging(ref ty) => ty.visit_with(visitor),
380 ty::FnDiverging => false,
381 }
382 }
383 }
384
385 impl<'tcx> TypeFoldable<'tcx> for ty::FnSig<'tcx> {
386 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
387 ty::FnSig { inputs: self.inputs.fold_with(folder),
388 output: self.output.fold_with(folder),
389 variadic: self.variadic }
390 }
391
392 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
393 folder.fold_fn_sig(self)
394 }
395
396 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
397 self.inputs.visit_with(visitor) || self.output.visit_with(visitor)
398 }
399 }
400
401 impl<'tcx> TypeFoldable<'tcx> for ty::TraitRef<'tcx> {
402 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
403 let substs = self.substs.fold_with(folder);
404 ty::TraitRef {
405 def_id: self.def_id,
406 substs: folder.tcx().mk_substs(substs),
407 }
408 }
409
410 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
411 folder.fold_trait_ref(self)
412 }
413
414 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
415 self.substs.visit_with(visitor)
416 }
417 }
418
419 impl<'tcx> TypeFoldable<'tcx> for ty::ImplHeader<'tcx> {
420 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
421 ty::ImplHeader {
422 impl_def_id: self.impl_def_id,
423 self_ty: self.self_ty.fold_with(folder),
424 trait_ref: self.trait_ref.map(|t| t.fold_with(folder)),
425 predicates: self.predicates.iter().map(|p| p.fold_with(folder)).collect(),
426 }
427 }
428
429 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
430 folder.fold_impl_header(self)
431 }
432
433 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
434 self.self_ty.visit_with(visitor) ||
435 self.trait_ref.map(|r| r.visit_with(visitor)).unwrap_or(false) ||
436 self.predicates.iter().any(|p| p.visit_with(visitor))
437 }
438 }
439
440 impl<'tcx> TypeFoldable<'tcx> for ty::Region {
441 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
442 *self
443 }
444
445 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
446 folder.fold_region(*self)
447 }
448
449 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
450 false
451 }
452
453 fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
454 visitor.visit_region(*self)
455 }
456 }
457
458 impl<'tcx> TypeFoldable<'tcx> for subst::Substs<'tcx> {
459 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
460 subst::Substs { regions: self.regions.fold_with(folder),
461 types: self.types.fold_with(folder) }
462 }
463
464 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
465 folder.fold_substs(self)
466 }
467
468 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
469 self.types.visit_with(visitor) || self.regions.visit_with(visitor)
470 }
471 }
472
473 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureSubsts<'tcx> {
474 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
475 let func_substs = self.func_substs.fold_with(folder);
476 ty::ClosureSubsts {
477 func_substs: folder.tcx().mk_substs(func_substs),
478 upvar_tys: self.upvar_tys.fold_with(folder),
479 }
480 }
481
482 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
483 self.func_substs.visit_with(visitor) || self.upvar_tys.visit_with(visitor)
484 }
485 }
486
487 impl<'tcx> TypeFoldable<'tcx> for ty::ItemSubsts<'tcx> {
488 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
489 ty::ItemSubsts {
490 substs: self.substs.fold_with(folder),
491 }
492 }
493
494 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
495 self.substs.visit_with(visitor)
496 }
497 }
498
499 impl<'tcx> TypeFoldable<'tcx> for ty::adjustment::AutoRef<'tcx> {
500 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
501 match *self {
502 ty::adjustment::AutoPtr(r, m) => {
503 let r = r.fold_with(folder);
504 ty::adjustment::AutoPtr(folder.tcx().mk_region(r), m)
505 }
506 ty::adjustment::AutoUnsafe(m) => ty::adjustment::AutoUnsafe(m)
507 }
508 }
509
510 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
511 folder.fold_autoref(self)
512 }
513
514 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
515 match *self {
516 ty::adjustment::AutoPtr(r, _m) => r.visit_with(visitor),
517 ty::adjustment::AutoUnsafe(_m) => false,
518 }
519 }
520 }
521
522 impl<'tcx> TypeFoldable<'tcx> for ty::BuiltinBounds {
523 fn super_fold_with<F: TypeFolder<'tcx>>(&self, _folder: &mut F) -> Self {
524 *self
525 }
526
527 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool {
528 false
529 }
530 }
531
532 impl<'tcx> TypeFoldable<'tcx> for ty::ExistentialBounds<'tcx> {
533 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
534 ty::ExistentialBounds {
535 region_bound: self.region_bound.fold_with(folder),
536 builtin_bounds: self.builtin_bounds,
537 projection_bounds: self.projection_bounds.fold_with(folder),
538 }
539 }
540
541 fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
542 folder.fold_existential_bounds(self)
543 }
544
545 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
546 self.region_bound.visit_with(visitor) || self.projection_bounds.visit_with(visitor)
547 }
548 }
549
550 impl<'tcx> TypeFoldable<'tcx> for ty::TypeParameterDef<'tcx> {
551 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
552 ty::TypeParameterDef {
553 name: self.name,
554 def_id: self.def_id,
555 space: self.space,
556 index: self.index,
557 default: self.default.fold_with(folder),
558 default_def_id: self.default_def_id,
559 object_lifetime_default: self.object_lifetime_default.fold_with(folder),
560 }
561 }
562
563 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
564 self.default.visit_with(visitor) ||
565 self.object_lifetime_default.visit_with(visitor)
566 }
567 }
568
569 impl<'tcx> TypeFoldable<'tcx> for ty::ObjectLifetimeDefault {
570 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
571 match *self {
572 ty::ObjectLifetimeDefault::Ambiguous =>
573 ty::ObjectLifetimeDefault::Ambiguous,
574
575 ty::ObjectLifetimeDefault::BaseDefault =>
576 ty::ObjectLifetimeDefault::BaseDefault,
577
578 ty::ObjectLifetimeDefault::Specific(r) =>
579 ty::ObjectLifetimeDefault::Specific(r.fold_with(folder)),
580 }
581 }
582
583 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
584 match *self {
585 ty::ObjectLifetimeDefault::Specific(r) => r.visit_with(visitor),
586 _ => false,
587 }
588 }
589 }
590
591 impl<'tcx> TypeFoldable<'tcx> for ty::RegionParameterDef {
592 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
593 ty::RegionParameterDef {
594 name: self.name,
595 def_id: self.def_id,
596 space: self.space,
597 index: self.index,
598 bounds: self.bounds.fold_with(folder)
599 }
600 }
601
602 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
603 self.bounds.visit_with(visitor)
604 }
605 }
606
607 impl<'tcx> TypeFoldable<'tcx> for ty::Generics<'tcx> {
608 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
609 ty::Generics {
610 types: self.types.fold_with(folder),
611 regions: self.regions.fold_with(folder),
612 }
613 }
614
615 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
616 self.types.visit_with(visitor) || self.regions.visit_with(visitor)
617 }
618 }
619
620 impl<'tcx> TypeFoldable<'tcx> for ty::GenericPredicates<'tcx> {
621 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
622 ty::GenericPredicates {
623 predicates: self.predicates.fold_with(folder),
624 }
625 }
626
627 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
628 self.predicates.visit_with(visitor)
629 }
630 }
631
632 impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
633 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
634 match *self {
635 ty::Predicate::Trait(ref a) =>
636 ty::Predicate::Trait(a.fold_with(folder)),
637 ty::Predicate::Equate(ref binder) =>
638 ty::Predicate::Equate(binder.fold_with(folder)),
639 ty::Predicate::RegionOutlives(ref binder) =>
640 ty::Predicate::RegionOutlives(binder.fold_with(folder)),
641 ty::Predicate::TypeOutlives(ref binder) =>
642 ty::Predicate::TypeOutlives(binder.fold_with(folder)),
643 ty::Predicate::Projection(ref binder) =>
644 ty::Predicate::Projection(binder.fold_with(folder)),
645 ty::Predicate::WellFormed(data) =>
646 ty::Predicate::WellFormed(data.fold_with(folder)),
647 ty::Predicate::ObjectSafe(trait_def_id) =>
648 ty::Predicate::ObjectSafe(trait_def_id),
649 }
650 }
651
652 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
653 match *self {
654 ty::Predicate::Trait(ref a) => a.visit_with(visitor),
655 ty::Predicate::Equate(ref binder) => binder.visit_with(visitor),
656 ty::Predicate::RegionOutlives(ref binder) => binder.visit_with(visitor),
657 ty::Predicate::TypeOutlives(ref binder) => binder.visit_with(visitor),
658 ty::Predicate::Projection(ref binder) => binder.visit_with(visitor),
659 ty::Predicate::WellFormed(data) => data.visit_with(visitor),
660 ty::Predicate::ObjectSafe(_trait_def_id) => false,
661 }
662 }
663 }
664
665 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionPredicate<'tcx> {
666 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
667 ty::ProjectionPredicate {
668 projection_ty: self.projection_ty.fold_with(folder),
669 ty: self.ty.fold_with(folder),
670 }
671 }
672
673 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
674 self.projection_ty.visit_with(visitor) || self.ty.visit_with(visitor)
675 }
676 }
677
678 impl<'tcx> TypeFoldable<'tcx> for ty::ProjectionTy<'tcx> {
679 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
680 ty::ProjectionTy {
681 trait_ref: self.trait_ref.fold_with(folder),
682 item_name: self.item_name,
683 }
684 }
685
686 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
687 self.trait_ref.visit_with(visitor)
688 }
689 }
690
691 impl<'tcx> TypeFoldable<'tcx> for ty::InstantiatedPredicates<'tcx> {
692 fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
693 ty::InstantiatedPredicates {
694 predicates: self.predicates.fold_with(folder),
695 }
696 }
697
698 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
699 self.predicates.visit_with(visitor)
700 }
701 }
702
703 impl<'tcx> TypeFoldable<'tcx> for ty::EquatePredicate<'tcx> {
704 fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
705 ty::EquatePredicate(self.0.fold_with(folder),
706 self.1.fold_with(folder))
707 }
708
709 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
710 self.0.visit_with(visitor) || self.1.visit_with(visitor)
711 }
712 }
713
714 impl<'tcx> TypeFoldable<'tcx> for ty::TraitPredicate<'tcx> {
715 fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
716 ty::TraitPredicate {
717 trait_ref: self.trait_ref.fold_with(folder)
718 }
719 }
720
721 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
722 self.trait_ref.visit_with(visitor)
723 }
724 }
725
726 impl<'tcx,T,U> TypeFoldable<'tcx> for ty::OutlivesPredicate<T,U>
727 where T : TypeFoldable<'tcx>,
728 U : TypeFoldable<'tcx>,
729 {
730 fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
731 ty::OutlivesPredicate(self.0.fold_with(folder),
732 self.1.fold_with(folder))
733 }
734
735 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
736 self.0.visit_with(visitor) || self.1.visit_with(visitor)
737 }
738 }
739
740 impl<'tcx> TypeFoldable<'tcx> for ty::ClosureUpvar<'tcx> {
741 fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
742 ty::ClosureUpvar {
743 def: self.def,
744 span: self.span,
745 ty: self.ty.fold_with(folder),
746 }
747 }
748
749 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
750 self.ty.visit_with(visitor)
751 }
752 }
753
754 impl<'a, 'tcx> TypeFoldable<'tcx> for ty::ParameterEnvironment<'a, 'tcx> where 'tcx: 'a {
755 fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
756 ty::ParameterEnvironment {
757 tcx: self.tcx,
758 free_substs: self.free_substs.fold_with(folder),
759 implicit_region_bound: self.implicit_region_bound.fold_with(folder),
760 caller_bounds: self.caller_bounds.fold_with(folder),
761 selection_cache: traits::SelectionCache::new(),
762 evaluation_cache: traits::EvaluationCache::new(),
763 free_id_outlive: self.free_id_outlive,
764 }
765 }
766
767 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
768 self.free_substs.visit_with(visitor) ||
769 self.implicit_region_bound.visit_with(visitor) ||
770 self.caller_bounds.visit_with(visitor)
771 }
772 }
773
774 impl<'tcx> TypeFoldable<'tcx> for ty::TypeScheme<'tcx> {
775 fn super_fold_with<F:TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
776 ty::TypeScheme {
777 generics: self.generics.fold_with(folder),
778 ty: self.ty.fold_with(folder),
779 }
780 }
781
782 fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
783 self.generics.visit_with(visitor) || self.ty.visit_with(visitor)
784 }
785 }