Skip to content

Commit 9c5a450

Browse files
nikomatsakiscsmoe
andcommitted
port fold_regions and friends to use debruijn indices directly
They no longer talk about plain integers. Co-authored-by: csmoe <[email protected]>
1 parent 69ab6f2 commit 9c5a450

File tree

4 files changed

+125
-50
lines changed

4 files changed

+125
-50
lines changed

src/librustc/infer/higher_ranked/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ fn fold_regions_in<'a, 'gcx, 'tcx, T, F>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
473473
_ => true
474474
});
475475

476-
fldr(region, ty::DebruijnIndex::new(current_depth))
476+
fldr(region, current_depth)
477477
})
478478
}
479479

@@ -734,7 +734,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
734734
// trait checking, and all of the skolemized regions
735735
// appear inside predicates, which always have
736736
// binders, so this assert is satisfied.
737-
assert!(current_depth > 1);
737+
assert!(current_depth > ty::DebruijnIndex::INNERMOST);
738738

739739
// since leak-check passed, this skolemized region
740740
// should only have incoming edges from variables
@@ -750,7 +750,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
750750
r, br);
751751

752752
self.tcx.mk_region(ty::ReLateBound(
753-
ty::DebruijnIndex::new(current_depth - 1), br.clone()))
753+
current_depth.shifted_out(1),
754+
br.clone(),
755+
))
754756
}
755757
}
756758
});

src/librustc/ty/fold.rs

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
6666
fn has_regions_escaping_depth(&self, depth: u32) -> bool {
6767
self.visit_with(&mut HasEscapingRegionsVisitor { depth: depth })
6868
}
69+
70+
/// True if `self` has any late-bound regions that are either
71+
/// bound by `binder` or bound by some binder outside of `binder`.
72+
/// If `binder` is `ty::DebruijnIndex::INNERMOST`, this indicates whether
73+
/// there are any late-bound regions that appear free.
74+
fn has_regions_bound_by_or_escaping(&self, binder: ty::DebruijnIndex) -> bool {
75+
self.has_regions_escaping_depth(binder.depth - 1)
76+
}
77+
6978
fn has_escaping_regions(&self) -> bool {
7079
self.has_regions_escaping_depth(0)
7180
}
@@ -207,7 +216,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
207216
{
208217
let mut have_bound_regions = false;
209218
self.fold_regions(value, &mut have_bound_regions, |r, d| {
210-
region_set.insert(self.mk_region(r.from_depth(d)));
219+
region_set.insert(self.mk_region(r.shifted_out_to_binder(d)));
211220
r
212221
});
213222
have_bound_regions
@@ -216,13 +225,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
216225
/// Folds the escaping and free regions in `value` using `f`, and
217226
/// sets `skipped_regions` to true if any late-bound region was found
218227
/// and skipped.
219-
pub fn fold_regions<T,F>(self,
228+
pub fn fold_regions<T>(
229+
self,
220230
value: &T,
221231
skipped_regions: &mut bool,
222-
mut f: F)
223-
-> T
224-
where F : FnMut(ty::Region<'tcx>, u32) -> ty::Region<'tcx>,
225-
T : TypeFoldable<'tcx>,
232+
mut f: impl FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
233+
) -> T
234+
where
235+
T : TypeFoldable<'tcx>,
226236
{
227237
value.fold_with(&mut RegionFolder::new(self, skipped_regions, &mut f))
228238
}
@@ -277,21 +287,32 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
277287
pub struct RegionFolder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
278288
tcx: TyCtxt<'a, 'gcx, 'tcx>,
279289
skipped_regions: &'a mut bool,
280-
current_depth: u32,
281-
fld_r: &'a mut (dyn FnMut(ty::Region<'tcx>, u32) -> ty::Region<'tcx> + 'a),
290+
291+
/// Stores the index of a binder *just outside* the stuff we have
292+
/// visited. So this begins as INNERMOST; when we pass through a
293+
/// binder, it is incremented (via `shift_in`).
294+
current_index: ty::DebruijnIndex,
295+
296+
/// Callback invokes for each free region. The `DebruijnIndex`
297+
/// points to the binder *just outside* the ones we have passed
298+
/// through.
299+
fold_region_fn: &'a mut (dyn FnMut(
300+
ty::Region<'tcx>,
301+
ty::DebruijnIndex,
302+
) -> ty::Region<'tcx> + 'a),
282303
}
283304

284305
impl<'a, 'gcx, 'tcx> RegionFolder<'a, 'gcx, 'tcx> {
285-
pub fn new<F>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
286-
skipped_regions: &'a mut bool,
287-
fld_r: &'a mut F) -> RegionFolder<'a, 'gcx, 'tcx>
288-
where F : FnMut(ty::Region<'tcx>, u32) -> ty::Region<'tcx>
289-
{
306+
pub fn new(
307+
tcx: TyCtxt<'a, 'gcx, 'tcx>,
308+
skipped_regions: &'a mut bool,
309+
fold_region_fn: &'a mut dyn FnMut(ty::Region<'tcx>, ty::DebruijnIndex) -> ty::Region<'tcx>,
310+
) -> RegionFolder<'a, 'gcx, 'tcx> {
290311
RegionFolder {
291312
tcx,
292313
skipped_regions,
293-
current_depth: 1,
294-
fld_r,
314+
current_index: ty::DebruijnIndex::INNERMOST,
315+
fold_region_fn,
295316
}
296317
}
297318
}
@@ -300,24 +321,24 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionFolder<'a, 'gcx, 'tcx> {
300321
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.tcx }
301322

302323
fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T> {
303-
self.current_depth += 1;
324+
self.current_index.shift_in(1);
304325
let t = t.super_fold_with(self);
305-
self.current_depth -= 1;
326+
self.current_index.shift_out(1);
306327
t
307328
}
308329

309330
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
310331
match *r {
311-
ty::ReLateBound(debruijn, _) if debruijn.depth < self.current_depth => {
312-
debug!("RegionFolder.fold_region({:?}) skipped bound region (current depth={})",
313-
r, self.current_depth);
332+
ty::ReLateBound(debruijn, _) if debruijn < self.current_index => {
333+
debug!("RegionFolder.fold_region({:?}) skipped bound region (current index={:?})",
334+
r, self.current_index);
314335
*self.skipped_regions = true;
315336
r
316337
}
317338
_ => {
318-
debug!("RegionFolder.fold_region({:?}) folding free region (current_depth={})",
319-
r, self.current_depth);
320-
(self.fld_r)(r, self.current_depth)
339+
debug!("RegionFolder.fold_region({:?}) folding free region (current_index={:?})",
340+
r, self.current_index);
341+
(self.fold_region_fn)(r, self.current_index)
321342
}
322343
}
323344
}
@@ -330,7 +351,11 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionFolder<'a, 'gcx, 'tcx> {
330351

331352
struct RegionReplacer<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
332353
tcx: TyCtxt<'a, 'gcx, 'tcx>,
333-
current_depth: u32,
354+
355+
/// As with `RegionFolder`, represents the index of a binder *just outside*
356+
/// the ones we have visited.
357+
current_index: ty::DebruijnIndex,
358+
334359
fld_r: &'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a),
335360
map: BTreeMap<ty::BoundRegion, ty::Region<'tcx>>
336361
}
@@ -372,20 +397,22 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
372397
}).0
373398
}
374399

375-
/// Flattens two binding levels into one. So `for<'a> for<'b> Foo`
400+
/// Flattens multiple binding levels into one. So `for<'a> for<'b> Foo`
376401
/// becomes `for<'a,'b> Foo`.
377402
pub fn flatten_late_bound_regions<T>(self, bound2_value: &Binder<Binder<T>>)
378403
-> Binder<T>
379404
where T: TypeFoldable<'tcx>
380405
{
381406
let bound0_value = bound2_value.skip_binder().skip_binder();
382-
let value = self.fold_regions(bound0_value, &mut false,
383-
|region, current_depth| {
407+
let value = self.fold_regions(bound0_value, &mut false, |region, current_depth| {
384408
match *region {
385-
ty::ReLateBound(debruijn, br) if debruijn.depth >= current_depth => {
386-
// should be true if no escaping regions from bound2_value
387-
assert!(debruijn.depth - current_depth <= 1);
388-
self.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(current_depth), br))
409+
ty::ReLateBound(debruijn, br) => {
410+
// We assume no regions bound *outside* of the
411+
// binders in `bound2_value` (nmatsakis added in
412+
// the course of this PR; seems like a reasonable
413+
// sanity check though).
414+
assert!(debruijn == current_depth);
415+
self.mk_region(ty::ReLateBound(current_depth, br))
389416
}
390417
_ => {
391418
region
@@ -446,7 +473,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
446473
let mut counter = 0;
447474
Binder::bind(self.replace_late_bound_regions(sig, |_| {
448475
counter += 1;
449-
self.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(1), ty::BrAnon(counter)))
476+
self.mk_region(ty::ReLateBound(ty::DebruijnIndex::INNERMOST, ty::BrAnon(counter)))
450477
}).0)
451478
}
452479
}
@@ -458,7 +485,7 @@ impl<'a, 'gcx, 'tcx> RegionReplacer<'a, 'gcx, 'tcx> {
458485
{
459486
RegionReplacer {
460487
tcx,
461-
current_depth: 1,
488+
current_index: ty::DebruijnIndex::INNERMOST,
462489
fld_r,
463490
map: BTreeMap::default()
464491
}
@@ -469,14 +496,14 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionReplacer<'a, 'gcx, 'tcx> {
469496
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.tcx }
470497

471498
fn fold_binder<T: TypeFoldable<'tcx>>(&mut self, t: &ty::Binder<T>) -> ty::Binder<T> {
472-
self.current_depth += 1;
499+
self.current_index.shift_in(1);
473500
let t = t.super_fold_with(self);
474-
self.current_depth -= 1;
501+
self.current_index.shift_out(1);
475502
t
476503
}
477504

478505
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
479-
if !t.has_regions_escaping_depth(self.current_depth-1) {
506+
if !t.has_regions_bound_by_or_escaping(self.current_index) {
480507
return t;
481508
}
482509

@@ -485,14 +512,15 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionReplacer<'a, 'gcx, 'tcx> {
485512

486513
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
487514
match *r {
488-
ty::ReLateBound(debruijn, br) if debruijn.depth == self.current_depth => {
515+
ty::ReLateBound(debruijn, br) if debruijn == self.current_index => {
489516
let fld_r = &mut self.fld_r;
490517
let region = *self.map.entry(br).or_insert_with(|| fld_r(br));
491518
if let ty::ReLateBound(debruijn1, br) = *region {
492519
// If the callback returns a late-bound region,
493-
// that region should always use depth 1. Then we
494-
// adjust it to the correct depth.
495-
assert_eq!(debruijn1.depth, 1);
520+
// that region should always use the INNERMOST
521+
// debruijn index. Then we adjust it to the
522+
// correct depth.
523+
assert_eq!(debruijn1, ty::DebruijnIndex::INNERMOST);
496524
self.tcx.mk_region(ty::ReLateBound(debruijn, br))
497525
} else {
498526
region

src/librustc/ty/sty.rs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,8 @@ impl<'a, 'tcx, 'gcx> PolyExistentialProjection<'tcx> {
12591259
}
12601260

12611261
impl DebruijnIndex {
1262+
pub const INNERMOST: DebruijnIndex = DebruijnIndex { depth: 1 };
1263+
12621264
pub fn new(depth: u32) -> DebruijnIndex {
12631265
assert!(depth > 0);
12641266
DebruijnIndex { depth: depth }
@@ -1296,6 +1298,30 @@ impl DebruijnIndex {
12961298
pub fn shift_out(&mut self, amount: u32) {
12971299
*self = self.shifted_out(amount);
12981300
}
1301+
1302+
/// Adjusts any Debruijn Indices so as to make `to_binder` the
1303+
/// innermost binder. That is, if we have something bound at `to_binder`,
1304+
/// it will now be bound at INNERMOST. This is an appropriate thing to do
1305+
/// when moving a region out from inside binders:
1306+
///
1307+
/// ```
1308+
/// for<'a> fn(for<'b> for<'c> fn(&'a u32), _)
1309+
/// // Binder: D3 D2 D1 ^^
1310+
/// ```
1311+
///
1312+
/// Here, the region `'a` would have the debruijn index D3,
1313+
/// because it is the bound 3 binders out. However, if we wanted
1314+
/// to refer to that region `'a` in the second argument (the `_`),
1315+
/// those two binders would not be in scope. In that case, we
1316+
/// might invoke `shift_out_to_binder(D3)`. This would adjust the
1317+
/// debruijn index of `'a` to D1 (the innermost binder).
1318+
///
1319+
/// If we invoke `shift_out_to_binder` and the region is in fact
1320+
/// bound by one of the binders we are shifting out of, that is an
1321+
/// error (and should fail an assertion failure).
1322+
pub fn shifted_out_to_binder(self, to_binder: DebruijnIndex) -> Self {
1323+
self.shifted_out(to_binder.depth - Self::INNERMOST.depth)
1324+
}
12991325
}
13001326

13011327
/// Region utilities
@@ -1314,12 +1340,32 @@ impl RegionKind {
13141340
}
13151341
}
13161342

1317-
/// Returns the depth of `self` from the (1-based) binding level `depth`
1318-
pub fn from_depth(&self, depth: u32) -> RegionKind {
1343+
/// Adjusts any Debruijn Indices so as to make `to_binder` the
1344+
/// innermost binder. That is, if we have something bound at `to_binder`,
1345+
/// it will now be bound at INNERMOST. This is an appropriate thing to do
1346+
/// when moving a region out from inside binders:
1347+
///
1348+
/// ```
1349+
/// for<'a> fn(for<'b> for<'c> fn(&'a u32), _)
1350+
/// // Binder: D3 D2 D1 ^^
1351+
/// ```
1352+
///
1353+
/// Here, the region `'a` would have the debruijn index D3,
1354+
/// because it is the bound 3 binders out. However, if we wanted
1355+
/// to refer to that region `'a` in the second argument (the `_`),
1356+
/// those two binders would not be in scope. In that case, we
1357+
/// might invoke `shift_out_to_binder(D3)`. This would adjust the
1358+
/// debruijn index of `'a` to D1 (the innermost binder).
1359+
///
1360+
/// If we invoke `shift_out_to_binder` and the region is in fact
1361+
/// bound by one of the binders we are shifting out of, that is an
1362+
/// error (and should fail an assertion failure).
1363+
pub fn shifted_out_to_binder(&self, to_binder: ty::DebruijnIndex) -> RegionKind {
13191364
match *self {
1320-
ty::ReLateBound(debruijn, r) => ty::ReLateBound(DebruijnIndex {
1321-
depth: debruijn.depth - (depth - 1)
1322-
}, r),
1365+
ty::ReLateBound(debruijn, r) => ty::ReLateBound(
1366+
debruijn.shifted_out_to_binder(to_binder),
1367+
r,
1368+
),
13231369
r => r
13241370
}
13251371
}

src/librustc_typeck/check/generator_interior.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ pub fn resolve_interior<'a, 'gcx, 'tcx>(fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
125125
let mut counter = 0;
126126
let type_list = fcx.tcx.fold_regions(&type_list, &mut false, |_, current_depth| {
127127
counter += 1;
128-
fcx.tcx.mk_region(ty::ReLateBound(ty::DebruijnIndex::new(current_depth),
129-
ty::BrAnon(counter)))
128+
fcx.tcx.mk_region(ty::ReLateBound(current_depth, ty::BrAnon(counter)))
130129
});
131130

132131
let witness = fcx.tcx.mk_generator_witness(ty::Binder::bind(type_list));

0 commit comments

Comments
 (0)