提交 f03c0366 编写于 作者: N Niko Matsakis

add "free region helpers"

上级 dddd4075
......@@ -253,13 +253,34 @@ pub fn fold_regions<T>(
value.fold_with(&mut RegionFolder::new(self, skipped_regions, &mut f))
}
pub fn for_each_free_region<T,F>(self,
value: &T,
callback: F)
where F: FnMut(ty::Region<'tcx>),
T: TypeFoldable<'tcx>,
{
value.visit_with(&mut RegionVisitor {
/// Invoke `callback` on every region appearing free in `value`.
pub fn for_each_free_region(
self,
value: &impl TypeFoldable<'tcx>,
mut callback: impl FnMut(ty::Region<'tcx>),
) {
self.any_free_region_meets(value, |r| {
callback(r);
false
});
}
/// True if `callback` returns true for every region appearing free in `value`.
pub fn all_free_regions_meet(
self,
value: &impl TypeFoldable<'tcx>,
mut callback: impl FnMut(ty::Region<'tcx>) -> bool,
) -> bool {
!self.any_free_region_meets(value, |r| !callback(r))
}
/// True if `callback` returns true for some region appearing free in `value`.
pub fn any_free_region_meets(
self,
value: &impl TypeFoldable<'tcx>,
callback: impl FnMut(ty::Region<'tcx>) -> bool,
) -> bool {
return value.visit_with(&mut RegionVisitor {
outer_index: ty::INNERMOST,
callback
});
......@@ -287,25 +308,22 @@ struct RegionVisitor<F> {
}
impl<'tcx, F> TypeVisitor<'tcx> for RegionVisitor<F>
where F : FnMut(ty::Region<'tcx>)
where F: FnMut(ty::Region<'tcx>) -> bool
{
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> bool {
self.outer_index.shift_in(1);
t.skip_binder().visit_with(self);
let result = t.skip_binder().visit_with(self);
self.outer_index.shift_out(1);
false // keep visiting
result
}
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
match *r {
ty::ReLateBound(debruijn, _) if debruijn < self.outer_index => {
/* ignore bound regions */
false // ignore bound regions, keep visiting
}
_ => (self.callback)(r),
}
false // keep visiting
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册