Skip to content

Commit 4aeb6ef

Browse files
nikomatsakiscsmoe
andcommitted
replace use of DebruijnIndex in for_each_free_region
Co-authored-by: csmoe <[email protected]>
1 parent 34c9ae7 commit 4aeb6ef

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/librustc/ty/fold.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -243,27 +243,47 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
243243
where F: FnMut(ty::Region<'tcx>),
244244
T: TypeFoldable<'tcx>,
245245
{
246-
value.visit_with(&mut RegionVisitor { current_depth: 0, callback });
246+
value.visit_with(&mut RegionVisitor {
247+
outer_index: ty::DebruijnIndex::INNERMOST,
248+
callback
249+
});
247250

248251
struct RegionVisitor<F> {
249-
current_depth: u32,
252+
/// The index of a binder *just outside* the things we have
253+
/// traversed. If we encounter a bound region bound by this
254+
/// binder or one outer to it, it appears free. Example:
255+
///
256+
/// ```
257+
/// for<'a> fn(for<'b> fn(), T)
258+
/// ^ ^ ^ ^
259+
/// | | | | here, would be shifted in 1
260+
/// | | | here, would be shifted in 2
261+
/// | | here, would be INNTERMOST shifted in by 1
262+
/// | here, initially, binder would be INNERMOST
263+
/// ```
264+
///
265+
/// You see that, initially, *any* bound value is free,
266+
/// because we've not traversed any binders. As we pass
267+
/// through a binder, we shift the `outer_index` by 1 to
268+
/// account for the new binder that encloses us.
269+
outer_index: ty::DebruijnIndex,
250270
callback: F,
251271
}
252272

253273
impl<'tcx, F> TypeVisitor<'tcx> for RegionVisitor<F>
254274
where F : FnMut(ty::Region<'tcx>)
255275
{
256276
fn visit_binder<T: TypeFoldable<'tcx>>(&mut self, t: &Binder<T>) -> bool {
257-
self.current_depth += 1;
277+
self.outer_index.shift_in(1);
258278
t.skip_binder().visit_with(self);
259-
self.current_depth -= 1;
279+
self.outer_index.shift_out(1);
260280

261281
false // keep visiting
262282
}
263283

264284
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
265285
match *r {
266-
ty::ReLateBound(debruijn, _) if debruijn.depth <= self.current_depth => {
286+
ty::ReLateBound(debruijn, _) if debruijn < self.outer_index => {
267287
/* ignore bound regions */
268288
}
269289
_ => (self.callback)(r),

0 commit comments

Comments
 (0)