Skip to content

Commit f03c036

Browse files
committed
add "free region helpers"
1 parent dddd407 commit f03c036

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

src/librustc/ty/fold.rs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,34 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
253253
value.fold_with(&mut RegionFolder::new(self, skipped_regions, &mut f))
254254
}
255255

256-
pub fn for_each_free_region<T,F>(self,
257-
value: &T,
258-
callback: F)
259-
where F: FnMut(ty::Region<'tcx>),
260-
T: TypeFoldable<'tcx>,
261-
{
262-
value.visit_with(&mut RegionVisitor {
256+
/// Invoke `callback` on every region appearing free in `value`.
257+
pub fn for_each_free_region(
258+
self,
259+
value: &impl TypeFoldable<'tcx>,
260+
mut callback: impl FnMut(ty::Region<'tcx>),
261+
) {
262+
self.any_free_region_meets(value, |r| {
263+
callback(r);
264+
false
265+
});
266+
}
267+
268+
/// True if `callback` returns true for every region appearing free in `value`.
269+
pub fn all_free_regions_meet(
270+
self,
271+
value: &impl TypeFoldable<'tcx>,
272+
mut callback: impl FnMut(ty::Region<'tcx>) -> bool,
273+
) -> bool {
274+
!self.any_free_region_meets(value, |r| !callback(r))
275+
}
276+
277+
/// True if `callback` returns true for some region appearing free in `value`.
278+
pub fn any_free_region_meets(
279+
self,
280+
value: &impl TypeFoldable<'tcx>,
281+
callback: impl FnMut(ty::Region<'tcx>) -> bool,
282+
) -> bool {
283+
return value.visit_with(&mut RegionVisitor {
263284
outer_index: ty::INNERMOST,
264285
callback
265286
});
@@ -287,25 +308,22 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
287308
}
288309

289310
impl<'tcx, F> TypeVisitor<'tcx> for RegionVisitor<F>
290-
where F : FnMut(ty::Region<'tcx>)
311+
where F: FnMut(ty::Region<'tcx>) -> bool
291312
{
292313
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> bool {
293314
self.outer_index.shift_in(1);
294-
t.skip_binder().visit_with(self);
315+
let result = t.skip_binder().visit_with(self);
295316
self.outer_index.shift_out(1);
296-
297-
false // keep visiting
317+
result
298318
}
299319

300320
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
301321
match *r {
302322
ty::ReLateBound(debruijn, _) if debruijn < self.outer_index => {
303-
/* ignore bound regions */
323+
false // ignore bound regions, keep visiting
304324
}
305325
_ => (self.callback)(r),
306326
}
307-
308-
false // keep visiting
309327
}
310328
}
311329
}

0 commit comments

Comments
 (0)