@@ -243,27 +243,47 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
243
243
where F : FnMut ( ty:: Region < ' tcx > ) ,
244
244
T : TypeFoldable < ' tcx > ,
245
245
{
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
+ } ) ;
247
250
248
251
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 ,
250
270
callback : F ,
251
271
}
252
272
253
273
impl < ' tcx , F > TypeVisitor < ' tcx > for RegionVisitor < F >
254
274
where F : FnMut ( ty:: Region < ' tcx > )
255
275
{
256
276
fn visit_binder < T : TypeFoldable < ' tcx > > ( & mut self , t : & Binder < T > ) -> bool {
257
- self . current_depth += 1 ;
277
+ self . outer_index . shift_in ( 1 ) ;
258
278
t. skip_binder ( ) . visit_with ( self ) ;
259
- self . current_depth -= 1 ;
279
+ self . outer_index . shift_out ( 1 ) ;
260
280
261
281
false // keep visiting
262
282
}
263
283
264
284
fn visit_region ( & mut self , r : ty:: Region < ' tcx > ) -> bool {
265
285
match * r {
266
- ty:: ReLateBound ( debruijn, _) if debruijn. depth <= self . current_depth => {
286
+ ty:: ReLateBound ( debruijn, _) if debruijn < self . outer_index => {
267
287
/* ignore bound regions */
268
288
}
269
289
_ => ( self . callback ) ( r) ,
0 commit comments