@@ -382,6 +382,15 @@ impl<'tcx> TyCtxt<'tcx> {
382
382
}
383
383
}
384
384
385
+ pub fn collect_late_bound_regions < T > ( & self , value : & Binder < T > )
386
+ -> FnvHashSet < ty:: BoundRegion >
387
+ where T : TypeFoldable < ' tcx >
388
+ {
389
+ let mut collector = LateBoundRegionsCollector :: new ( ) ;
390
+ value. skip_binder ( ) . visit_with ( & mut collector) ;
391
+ collector. regions
392
+ }
393
+
385
394
/// Replace any late-bound regions bound in `value` with `'static`. Useful in trans but also
386
395
/// method lookup and a few other places where precise region relationships are not required.
387
396
pub fn erase_late_bound_regions < T > ( & self , value : & Binder < T > ) -> T
@@ -626,3 +635,39 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
626
635
false
627
636
}
628
637
}
638
+
639
+ /// Collects all the late-bound regions it finds into a hash set.
640
+ struct LateBoundRegionsCollector {
641
+ current_depth : u32 ,
642
+ regions : FnvHashSet < ty:: BoundRegion > ,
643
+ }
644
+
645
+ impl LateBoundRegionsCollector {
646
+ fn new ( ) -> Self {
647
+ LateBoundRegionsCollector {
648
+ current_depth : 1 ,
649
+ regions : FnvHashSet ( ) ,
650
+ }
651
+ }
652
+ }
653
+
654
+ impl < ' tcx > TypeVisitor < ' tcx > for LateBoundRegionsCollector {
655
+ fn enter_region_binder ( & mut self ) {
656
+ self . current_depth += 1 ;
657
+ }
658
+
659
+ fn exit_region_binder ( & mut self ) {
660
+ self . current_depth -= 1 ;
661
+ }
662
+
663
+ fn visit_region ( & mut self , r : ty:: Region ) -> bool {
664
+ match r {
665
+ ty:: ReLateBound ( debruijn, br) if debruijn. depth == self . current_depth => {
666
+ self . regions . insert ( br) ;
667
+ }
668
+ _ => { }
669
+ }
670
+ true
671
+ }
672
+ }
673
+
0 commit comments