]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/ty/fold.rs
090d4eeb87437bcc7bf7cb783fd7c0d9881657ff
[rust.git] / src / librustc / middle / ty / fold.rs
1 // Copyright 2012-2013 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 //! Generalized type folding mechanism. The setup is a bit convoluted
12 //! but allows for convenient usage. Let T be an instance of some
13 //! "foldable type" (one which implements `TypeFoldable`) and F be an
14 //! instance of a "folder" (a type which implements `TypeFolder`). Then
15 //! the setup is intended to be:
16 //!
17 //!   T.fold_with(F) --calls--> F.fold_T(T) --calls--> T.super_fold_with(F)
18 //!
19 //! This way, when you define a new folder F, you can override
20 //! `fold_T()` to customize the behavior, and invoke `T.super_fold_with()`
21 //! to get the original behavior. Meanwhile, to actually fold
22 //! something, you can just write `T.fold_with(F)`, which is
23 //! convenient. (Note that `fold_with` will also transparently handle
24 //! things like a `Vec<T>` where T is foldable and so on.)
25 //!
26 //! In this ideal setup, the only function that actually *does*
27 //! anything is `T.super_fold_with()`, which traverses the type `T`.
28 //! Moreover, `T.super_fold_with()` should only ever call `T.fold_with()`.
29 //!
30 //! In some cases, we follow a degenerate pattern where we do not have
31 //! a `fold_T` method. Instead, `T.fold_with` traverses the structure directly.
32 //! This is suboptimal because the behavior cannot be overridden, but it's
33 //! much less work to implement. If you ever *do* need an override that
34 //! doesn't exist, it's not hard to convert the degenerate pattern into the
35 //! proper thing.
36 //!
37 //! A `TypeFoldable` T can also be visited by a `TypeVisitor` V using similar setup:
38 //!   T.visit_with(V) --calls--> V.visit_T(T) --calls--> T.super_visit_with(V).
39 //! These methods return true to indicate that the visitor has found what it is looking for
40 //! and does not need to visit anything else.
41
42 use middle::region;
43 use middle::subst;
44 use middle::ty::adjustment;
45 use middle::ty::{self, Binder, Ty, TyCtxt, TypeFlags};
46
47 use std::fmt;
48 use util::nodemap::{FnvHashMap, FnvHashSet};
49
50 /// The TypeFoldable trait is implemented for every type that can be folded.
51 /// Basically, every type that has a corresponding method in TypeFolder.
52 pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
53     fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self;
54     fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
55         self.super_fold_with(folder)
56     }
57
58     fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool;
59     fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
60         self.super_visit_with(visitor)
61     }
62
63     fn has_regions_escaping_depth(&self, depth: u32) -> bool {
64         self.visit_with(&mut HasEscapingRegionsVisitor { depth: depth })
65     }
66     fn has_escaping_regions(&self) -> bool {
67         self.has_regions_escaping_depth(0)
68     }
69
70     fn has_type_flags(&self, flags: TypeFlags) -> bool {
71         self.visit_with(&mut HasTypeFlagsVisitor { flags: flags })
72     }
73     fn has_projection_types(&self) -> bool {
74         self.has_type_flags(TypeFlags::HAS_PROJECTION)
75     }
76     fn references_error(&self) -> bool {
77         self.has_type_flags(TypeFlags::HAS_TY_ERR)
78     }
79     fn has_param_types(&self) -> bool {
80         self.has_type_flags(TypeFlags::HAS_PARAMS)
81     }
82     fn has_self_ty(&self) -> bool {
83         self.has_type_flags(TypeFlags::HAS_SELF)
84     }
85     fn has_infer_types(&self) -> bool {
86         self.has_type_flags(TypeFlags::HAS_TY_INFER)
87     }
88     fn needs_infer(&self) -> bool {
89         self.has_type_flags(TypeFlags::HAS_TY_INFER | TypeFlags::HAS_RE_INFER)
90     }
91     fn needs_subst(&self) -> bool {
92         self.has_type_flags(TypeFlags::NEEDS_SUBST)
93     }
94     fn has_closure_types(&self) -> bool {
95         self.has_type_flags(TypeFlags::HAS_TY_CLOSURE)
96     }
97     fn has_erasable_regions(&self) -> bool {
98         self.has_type_flags(TypeFlags::HAS_RE_EARLY_BOUND |
99                             TypeFlags::HAS_RE_INFER |
100                             TypeFlags::HAS_FREE_REGIONS)
101     }
102     /// Indicates whether this value references only 'global'
103     /// types/lifetimes that are the same regardless of what fn we are
104     /// in. This is used for caching. Errs on the side of returning
105     /// false.
106     fn is_global(&self) -> bool {
107         !self.has_type_flags(TypeFlags::HAS_LOCAL_NAMES)
108     }
109 }
110
111 /// The TypeFolder trait defines the actual *folding*. There is a
112 /// method defined for every foldable type. Each of these has a
113 /// default implementation that does an "identity" fold. Within each
114 /// identity fold, it should invoke `foo.fold_with(self)` to fold each
115 /// sub-item.
116 pub trait TypeFolder<'tcx> : Sized {
117     fn tcx<'a>(&'a self) -> &'a TyCtxt<'tcx>;
118
119     /// Invoked by the `super_*` routines when we enter a region
120     /// binding level (for example, when entering a function
121     /// signature). This is used by clients that want to track the
122     /// Debruijn index nesting level.
123     fn enter_region_binder(&mut self) { }
124
125     /// Invoked by the `super_*` routines when we exit a region
126     /// binding level. This is used by clients that want to
127     /// track the Debruijn index nesting level.
128     fn exit_region_binder(&mut self) { }
129
130     fn fold_binder<T>(&mut self, t: &Binder<T>) -> Binder<T>
131         where T : TypeFoldable<'tcx>
132     {
133         // FIXME(#20526) this should replace `enter_region_binder`/`exit_region_binder`.
134         t.super_fold_with(self)
135     }
136
137     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
138         t.super_fold_with(self)
139     }
140
141     fn fold_mt(&mut self, t: &ty::TypeAndMut<'tcx>) -> ty::TypeAndMut<'tcx> {
142         t.super_fold_with(self)
143     }
144
145     fn fold_trait_ref(&mut self, t: &ty::TraitRef<'tcx>) -> ty::TraitRef<'tcx> {
146         t.super_fold_with(self)
147     }
148
149     fn fold_impl_header(&mut self, imp: &ty::ImplHeader<'tcx>) -> ty::ImplHeader<'tcx> {
150         imp.super_fold_with(self)
151     }
152
153     fn fold_substs(&mut self,
154                    substs: &subst::Substs<'tcx>)
155                    -> subst::Substs<'tcx> {
156         substs.super_fold_with(self)
157     }
158
159     fn fold_fn_sig(&mut self,
160                    sig: &ty::FnSig<'tcx>)
161                    -> ty::FnSig<'tcx> {
162         sig.super_fold_with(self)
163     }
164
165     fn fold_output(&mut self,
166                       output: &ty::FnOutput<'tcx>)
167                       -> ty::FnOutput<'tcx> {
168         output.super_fold_with(self)
169     }
170
171     fn fold_bare_fn_ty(&mut self,
172                        fty: &ty::BareFnTy<'tcx>)
173                        -> ty::BareFnTy<'tcx>
174     {
175         fty.super_fold_with(self)
176     }
177
178     fn fold_closure_ty(&mut self,
179                        fty: &ty::ClosureTy<'tcx>)
180                        -> ty::ClosureTy<'tcx> {
181         fty.super_fold_with(self)
182     }
183
184     fn fold_region(&mut self, r: ty::Region) -> ty::Region {
185         r.super_fold_with(self)
186     }
187
188     fn fold_existential_bounds(&mut self, s: &ty::ExistentialBounds<'tcx>)
189                                -> ty::ExistentialBounds<'tcx> {
190         s.super_fold_with(self)
191     }
192
193     fn fold_autoref(&mut self, ar: &adjustment::AutoRef<'tcx>)
194                     -> adjustment::AutoRef<'tcx> {
195         ar.super_fold_with(self)
196     }
197 }
198
199 pub trait TypeVisitor<'tcx> : Sized {
200     fn enter_region_binder(&mut self) { }
201     fn exit_region_binder(&mut self) { }
202
203     fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
204         t.super_visit_with(self)
205     }
206
207     fn visit_region(&mut self, r: ty::Region) -> bool {
208         r.super_visit_with(self)
209     }
210 }
211
212 ///////////////////////////////////////////////////////////////////////////
213 // Some sample folders
214
215 pub struct BottomUpFolder<'a, 'tcx: 'a, F> where F: FnMut(Ty<'tcx>) -> Ty<'tcx> {
216     pub tcx: &'a TyCtxt<'tcx>,
217     pub fldop: F,
218 }
219
220 impl<'a, 'tcx, F> TypeFolder<'tcx> for BottomUpFolder<'a, 'tcx, F> where
221     F: FnMut(Ty<'tcx>) -> Ty<'tcx>,
222 {
223     fn tcx(&self) -> &TyCtxt<'tcx> { self.tcx }
224
225     fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
226         let t1 = ty.super_fold_with(self);
227         (self.fldop)(t1)
228     }
229 }
230
231 ///////////////////////////////////////////////////////////////////////////
232 // Region folder
233
234 impl<'tcx> TyCtxt<'tcx> {
235     /// Collects the free and escaping regions in `value` into `region_set`. Returns
236     /// whether any late-bound regions were skipped
237     pub fn collect_regions<T>(&self,
238         value: &T,
239         region_set: &mut FnvHashSet<ty::Region>)
240         -> bool
241         where T : TypeFoldable<'tcx>
242     {
243         let mut have_bound_regions = false;
244         self.fold_regions(value, &mut have_bound_regions,
245                           |r, d| { region_set.insert(r.from_depth(d)); r });
246         have_bound_regions
247     }
248
249     /// Folds the escaping and free regions in `value` using `f`, and
250     /// sets `skipped_regions` to true if any late-bound region was found
251     /// and skipped.
252     pub fn fold_regions<T,F>(&self,
253         value: &T,
254         skipped_regions: &mut bool,
255         mut f: F)
256         -> T
257         where F : FnMut(ty::Region, u32) -> ty::Region,
258               T : TypeFoldable<'tcx>,
259     {
260         value.fold_with(&mut RegionFolder::new(self, skipped_regions, &mut f))
261     }
262 }
263
264 /// Folds over the substructure of a type, visiting its component
265 /// types and all regions that occur *free* within it.
266 ///
267 /// That is, `Ty` can contain function or method types that bind
268 /// regions at the call site (`ReLateBound`), and occurrences of
269 /// regions (aka "lifetimes") that are bound within a type are not
270 /// visited by this folder; only regions that occur free will be
271 /// visited by `fld_r`.
272
273 pub struct RegionFolder<'a, 'tcx: 'a> {
274     tcx: &'a TyCtxt<'tcx>,
275     skipped_regions: &'a mut bool,
276     current_depth: u32,
277     fld_r: &'a mut (FnMut(ty::Region, u32) -> ty::Region + 'a),
278 }
279
280 impl<'a, 'tcx> RegionFolder<'a, 'tcx> {
281     pub fn new<F>(tcx: &'a TyCtxt<'tcx>,
282                   skipped_regions: &'a mut bool,
283                   fld_r: &'a mut F) -> RegionFolder<'a, 'tcx>
284         where F : FnMut(ty::Region, u32) -> ty::Region
285     {
286         RegionFolder {
287             tcx: tcx,
288             skipped_regions: skipped_regions,
289             current_depth: 1,
290             fld_r: fld_r,
291         }
292     }
293 }
294
295 impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx>
296 {
297     fn tcx(&self) -> &TyCtxt<'tcx> { self.tcx }
298
299     fn enter_region_binder(&mut self) {
300         self.current_depth += 1;
301     }
302
303     fn exit_region_binder(&mut self) {
304         self.current_depth -= 1;
305     }
306
307     fn fold_region(&mut self, r: ty::Region) -> ty::Region {
308         match r {
309             ty::ReLateBound(debruijn, _) if debruijn.depth < self.current_depth => {
310                 debug!("RegionFolder.fold_region({:?}) skipped bound region (current depth={})",
311                        r, self.current_depth);
312                 *self.skipped_regions = true;
313                 r
314             }
315             _ => {
316                 debug!("RegionFolder.fold_region({:?}) folding free region (current_depth={})",
317                        r, self.current_depth);
318                 (self.fld_r)(r, self.current_depth)
319             }
320         }
321     }
322 }
323
324 ///////////////////////////////////////////////////////////////////////////
325 // Late-bound region replacer
326
327 // Replaces the escaping regions in a type.
328
329 struct RegionReplacer<'a, 'tcx: 'a> {
330     tcx: &'a TyCtxt<'tcx>,
331     current_depth: u32,
332     fld_r: &'a mut (FnMut(ty::BoundRegion) -> ty::Region + 'a),
333     map: FnvHashMap<ty::BoundRegion, ty::Region>
334 }
335
336 impl<'tcx> TyCtxt<'tcx> {
337     pub fn replace_late_bound_regions<T,F>(&self,
338         value: &Binder<T>,
339         mut f: F)
340         -> (T, FnvHashMap<ty::BoundRegion, ty::Region>)
341         where F : FnMut(ty::BoundRegion) -> ty::Region,
342               T : TypeFoldable<'tcx>,
343     {
344         debug!("replace_late_bound_regions({:?})", value);
345         let mut replacer = RegionReplacer::new(self, &mut f);
346         let result = value.skip_binder().fold_with(&mut replacer);
347         (result, replacer.map)
348     }
349
350
351     /// Replace any late-bound regions bound in `value` with free variants attached to scope-id
352     /// `scope_id`.
353     pub fn liberate_late_bound_regions<T>(&self,
354         all_outlive_scope: region::CodeExtent,
355         value: &Binder<T>)
356         -> T
357         where T : TypeFoldable<'tcx>
358     {
359         self.replace_late_bound_regions(value, |br| {
360             ty::ReFree(ty::FreeRegion{scope: all_outlive_scope, bound_region: br})
361         }).0
362     }
363
364     /// Flattens two binding levels into one. So `for<'a> for<'b> Foo`
365     /// becomes `for<'a,'b> Foo`.
366     pub fn flatten_late_bound_regions<T>(&self, bound2_value: &Binder<Binder<T>>)
367                                          -> Binder<T>
368         where T: TypeFoldable<'tcx>
369     {
370         let bound0_value = bound2_value.skip_binder().skip_binder();
371         let value = self.fold_regions(bound0_value, &mut false,
372                                       |region, current_depth| {
373             match region {
374                 ty::ReLateBound(debruijn, br) if debruijn.depth >= current_depth => {
375                     // should be true if no escaping regions from bound2_value
376                     assert!(debruijn.depth - current_depth <= 1);
377                     ty::ReLateBound(ty::DebruijnIndex::new(current_depth), br)
378                 }
379                 _ => {
380                     region
381                 }
382             }
383         });
384         Binder(value)
385     }
386
387     pub fn no_late_bound_regions<T>(&self, value: &Binder<T>) -> Option<T>
388         where T : TypeFoldable<'tcx>
389     {
390         if value.0.has_escaping_regions() {
391             None
392         } else {
393             Some(value.0.clone())
394         }
395     }
396
397     /// Replace any late-bound regions bound in `value` with `'static`. Useful in trans but also
398     /// method lookup and a few other places where precise region relationships are not required.
399     pub fn erase_late_bound_regions<T>(&self, value: &Binder<T>) -> T
400         where T : TypeFoldable<'tcx>
401     {
402         self.replace_late_bound_regions(value, |_| ty::ReStatic).0
403     }
404
405     /// Rewrite any late-bound regions so that they are anonymous.  Region numbers are
406     /// assigned starting at 1 and increasing monotonically in the order traversed
407     /// by the fold operation.
408     ///
409     /// The chief purpose of this function is to canonicalize regions so that two
410     /// `FnSig`s or `TraitRef`s which are equivalent up to region naming will become
411     /// structurally identical.  For example, `for<'a, 'b> fn(&'a isize, &'b isize)` and
412     /// `for<'a, 'b> fn(&'b isize, &'a isize)` will become identical after anonymization.
413     pub fn anonymize_late_bound_regions<T>(&self, sig: &Binder<T>) -> Binder<T>
414         where T : TypeFoldable<'tcx>,
415     {
416         let mut counter = 0;
417         Binder(self.replace_late_bound_regions(sig, |_| {
418             counter += 1;
419             ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrAnon(counter))
420         }).0)
421     }
422 }
423
424 impl<'a, 'tcx> RegionReplacer<'a, 'tcx> {
425     fn new<F>(tcx: &'a TyCtxt<'tcx>, fld_r: &'a mut F) -> RegionReplacer<'a, 'tcx>
426         where F : FnMut(ty::BoundRegion) -> ty::Region
427     {
428         RegionReplacer {
429             tcx: tcx,
430             current_depth: 1,
431             fld_r: fld_r,
432             map: FnvHashMap()
433         }
434     }
435 }
436
437 impl<'a, 'tcx> TypeFolder<'tcx> for RegionReplacer<'a, 'tcx>
438 {
439     fn tcx(&self) -> &TyCtxt<'tcx> { self.tcx }
440
441     fn enter_region_binder(&mut self) {
442         self.current_depth += 1;
443     }
444
445     fn exit_region_binder(&mut self) {
446         self.current_depth -= 1;
447     }
448
449     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
450         if !t.has_regions_escaping_depth(self.current_depth-1) {
451             return t;
452         }
453
454         t.super_fold_with(self)
455     }
456
457     fn fold_region(&mut self, r: ty::Region) -> ty::Region {
458         match r {
459             ty::ReLateBound(debruijn, br) if debruijn.depth == self.current_depth => {
460                 debug!("RegionReplacer.fold_region({:?}) folding region (current_depth={})",
461                        r, self.current_depth);
462                 let fld_r = &mut self.fld_r;
463                 let region = *self.map.entry(br).or_insert_with(|| fld_r(br));
464                 if let ty::ReLateBound(debruijn1, br) = region {
465                     // If the callback returns a late-bound region,
466                     // that region should always use depth 1. Then we
467                     // adjust it to the correct depth.
468                     assert_eq!(debruijn1.depth, 1);
469                     ty::ReLateBound(debruijn, br)
470                 } else {
471                     region
472                 }
473             }
474             r => r
475         }
476     }
477 }
478
479 ///////////////////////////////////////////////////////////////////////////
480 // Region eraser
481
482 impl<'tcx> TyCtxt<'tcx> {
483     /// Returns an equivalent value with all free regions removed (note
484     /// that late-bound regions remain, because they are important for
485     /// subtyping, but they are anonymized and normalized as well)..
486     pub fn erase_regions<T>(&self, value: &T) -> T
487         where T : TypeFoldable<'tcx>
488     {
489         let value1 = value.fold_with(&mut RegionEraser(self));
490         debug!("erase_regions({:?}) = {:?}",
491                value, value1);
492         return value1;
493
494         struct RegionEraser<'a, 'tcx: 'a>(&'a TyCtxt<'tcx>);
495
496         impl<'a, 'tcx> TypeFolder<'tcx> for RegionEraser<'a, 'tcx> {
497             fn tcx(&self) -> &TyCtxt<'tcx> { self.0 }
498
499             fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
500                 match self.tcx().normalized_cache.borrow().get(&ty).cloned() {
501                     None => {}
502                     Some(u) => return u
503                 }
504
505                 let t_norm = ty.super_fold_with(self);
506                 self.tcx().normalized_cache.borrow_mut().insert(ty, t_norm);
507                 return t_norm;
508             }
509
510             fn fold_binder<T>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T>
511                 where T : TypeFoldable<'tcx>
512             {
513                 let u = self.tcx().anonymize_late_bound_regions(t);
514                 u.super_fold_with(self)
515             }
516
517             fn fold_region(&mut self, r: ty::Region) -> ty::Region {
518                 // because late-bound regions affect subtyping, we can't
519                 // erase the bound/free distinction, but we can replace
520                 // all free regions with 'static.
521                 //
522                 // Note that we *CAN* replace early-bound regions -- the
523                 // type system never "sees" those, they get substituted
524                 // away. In trans, they will always be erased to 'static
525                 // whenever a substitution occurs.
526                 match r {
527                     ty::ReLateBound(..) => r,
528                     _ => ty::ReStatic
529                 }
530             }
531
532             fn fold_substs(&mut self,
533                            substs: &subst::Substs<'tcx>)
534                            -> subst::Substs<'tcx> {
535                 subst::Substs { regions: subst::ErasedRegions,
536                                 types: substs.types.fold_with(self) }
537             }
538         }
539     }
540 }
541
542 ///////////////////////////////////////////////////////////////////////////
543 // Region shifter
544 //
545 // Shifts the De Bruijn indices on all escaping bound regions by a
546 // fixed amount. Useful in substitution or when otherwise introducing
547 // a binding level that is not intended to capture the existing bound
548 // regions. See comment on `shift_regions_through_binders` method in
549 // `subst.rs` for more details.
550
551 pub fn shift_region(region: ty::Region, amount: u32) -> ty::Region {
552     match region {
553         ty::ReLateBound(debruijn, br) => {
554             ty::ReLateBound(debruijn.shifted(amount), br)
555         }
556         _ => {
557             region
558         }
559     }
560 }
561
562 pub fn shift_regions<'tcx, T:TypeFoldable<'tcx>>(tcx: &TyCtxt<'tcx>,
563                                                  amount: u32, value: &T) -> T {
564     debug!("shift_regions(value={:?}, amount={})",
565            value, amount);
566
567     value.fold_with(&mut RegionFolder::new(tcx, &mut false, &mut |region, _current_depth| {
568         shift_region(region, amount)
569     }))
570 }
571
572 /// An "escaping region" is a bound region whose binder is not part of `t`.
573 ///
574 /// So, for example, consider a type like the following, which has two binders:
575 ///
576 ///    for<'a> fn(x: for<'b> fn(&'a isize, &'b isize))
577 ///    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ outer scope
578 ///                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~  inner scope
579 ///
580 /// This type has *bound regions* (`'a`, `'b`), but it does not have escaping regions, because the
581 /// binders of both `'a` and `'b` are part of the type itself. However, if we consider the *inner
582 /// fn type*, that type has an escaping region: `'a`.
583 ///
584 /// Note that what I'm calling an "escaping region" is often just called a "free region". However,
585 /// we already use the term "free region". It refers to the regions that we use to represent bound
586 /// regions on a fn definition while we are typechecking its body.
587 ///
588 /// To clarify, conceptually there is no particular difference between an "escaping" region and a
589 /// "free" region. However, there is a big difference in practice. Basically, when "entering" a
590 /// binding level, one is generally required to do some sort of processing to a bound region, such
591 /// as replacing it with a fresh/skolemized region, or making an entry in the environment to
592 /// represent the scope to which it is attached, etc. An escaping region represents a bound region
593 /// for which this processing has not yet been done.
594 struct HasEscapingRegionsVisitor {
595     depth: u32,
596 }
597
598 impl<'tcx> TypeVisitor<'tcx> for HasEscapingRegionsVisitor {
599     fn enter_region_binder(&mut self) {
600         self.depth += 1;
601     }
602
603     fn exit_region_binder(&mut self) {
604         self.depth -= 1;
605     }
606
607     fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
608         t.region_depth > self.depth
609     }
610
611     fn visit_region(&mut self, r: ty::Region) -> bool {
612         r.escapes_depth(self.depth)
613     }
614 }
615
616 struct HasTypeFlagsVisitor {
617     flags: ty::TypeFlags,
618 }
619
620 impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
621     fn visit_ty(&mut self, t: Ty) -> bool {
622         t.flags.get().intersects(self.flags)
623     }
624
625     fn visit_region(&mut self, r: ty::Region) -> bool {
626         if self.flags.intersects(ty::TypeFlags::HAS_LOCAL_NAMES) {
627             // does this represent a region that cannot be named
628             // in a global way? used in fulfillment caching.
629             match r {
630                 ty::ReStatic | ty::ReEmpty => {}
631                 _ => return true,
632             }
633         }
634         if self.flags.intersects(ty::TypeFlags::HAS_RE_INFER) {
635             match r {
636                 ty::ReVar(_) | ty::ReSkolemized(..) => { return true }
637                 _ => {}
638             }
639         }
640         false
641     }
642 }