Skip to content

Commit 8535a4a

Browse files
committed
add helper for_each_free_region that iterates over free regions
1 parent 1f06ba4 commit 8535a4a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/librustc/ty/fold.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,43 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
218218
{
219219
value.fold_with(&mut RegionFolder::new(self, skipped_regions, &mut f))
220220
}
221+
222+
pub fn for_each_free_region<T,F>(self,
223+
value: &T,
224+
callback: F)
225+
where F: FnMut(ty::Region<'tcx>),
226+
T: TypeFoldable<'tcx>,
227+
{
228+
value.visit_with(&mut RegionVisitor { current_depth: 0, callback });
229+
230+
struct RegionVisitor<F> {
231+
current_depth: u32,
232+
callback: F,
233+
}
234+
235+
impl<'tcx, F> TypeVisitor<'tcx> for RegionVisitor<F>
236+
where F : FnMut(ty::Region<'tcx>)
237+
{
238+
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> bool {
239+
self.current_depth += 1;
240+
t.skip_binder().visit_with(self);
241+
self.current_depth -= 1;
242+
243+
false // keep visiting
244+
}
245+
246+
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
247+
match *r {
248+
ty::ReLateBound(debruijn, _) if debruijn.depth < self.current_depth => {
249+
/* ignore bound regions */
250+
}
251+
_ => (self.callback)(r),
252+
}
253+
254+
false // keep visiting
255+
}
256+
}
257+
}
221258
}
222259

223260
/// Folds over the substructure of a type, visiting its component

0 commit comments

Comments
 (0)