Skip to content

Commit c8b2d18

Browse files
committed
add LateBoundRegionsCollector
1 parent 3dd88f6 commit c8b2d18

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/librustc/ty/fold.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,15 @@ impl<'tcx> TyCtxt<'tcx> {
382382
}
383383
}
384384

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+
385394
/// Replace any late-bound regions bound in `value` with `'static`. Useful in trans but also
386395
/// method lookup and a few other places where precise region relationships are not required.
387396
pub fn erase_late_bound_regions<T>(&self, value: &Binder<T>) -> T
@@ -626,3 +635,39 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
626635
false
627636
}
628637
}
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

Comments
 (0)