Skip to content

Commit 69ab6f2

Browse files
nikomatsakiscsmoe
andcommitted
introduce shifted_in, shifted_out and friends
Co-authored-by: csmoe <[email protected]>
1 parent a2c4d4e commit 69ab6f2

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#![feature(test)]
7272
#![feature(in_band_lifetimes)]
7373
#![feature(macro_at_most_once_rep)]
74+
#![feature(inclusive_range_methods)]
7475

7576
#![recursion_limit="512"]
7677

src/librustc/middle/resolve_lifetime.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ impl Region {
123123

124124
fn shifted(self, amount: u32) -> Region {
125125
match self {
126-
Region::LateBound(depth, id, origin) => {
127-
Region::LateBound(depth.shifted(amount), id, origin)
126+
Region::LateBound(debruijn, id, origin) => {
127+
Region::LateBound(debruijn.shifted_in(amount), id, origin)
128128
}
129-
Region::LateBoundAnon(depth, index) => {
130-
Region::LateBoundAnon(depth.shifted(amount), index)
129+
Region::LateBoundAnon(debruijn, index) => {
130+
Region::LateBoundAnon(debruijn.shifted_in(amount), index)
131131
}
132132
_ => self,
133133
}

src/librustc/ty/fold.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionReplacer<'a, 'gcx, 'tcx> {
515515
pub fn shift_region(region: ty::RegionKind, amount: u32) -> ty::RegionKind {
516516
match region {
517517
ty::ReLateBound(debruijn, br) => {
518-
ty::ReLateBound(debruijn.shifted(amount), br)
518+
ty::ReLateBound(debruijn.shifted_in(amount), br)
519519
}
520520
_ => {
521521
region
@@ -531,7 +531,7 @@ pub fn shift_region_ref<'a, 'gcx, 'tcx>(
531531
{
532532
match region {
533533
&ty::ReLateBound(debruijn, br) if amount > 0 => {
534-
tcx.mk_region(ty::ReLateBound(debruijn.shifted(amount), br))
534+
tcx.mk_region(ty::ReLateBound(debruijn.shifted_in(amount), br))
535535
}
536536
_ => {
537537
region

src/librustc/ty/sty.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1264,9 +1264,38 @@ impl DebruijnIndex {
12641264
DebruijnIndex { depth: depth }
12651265
}
12661266

1267-
pub fn shifted(&self, amount: u32) -> DebruijnIndex {
1267+
/// Returns the resulting index when this value is moved into
1268+
/// `amount` number of new binders. So e.g. if you had
1269+
///
1270+
/// for<'a> fn(&'a x)
1271+
///
1272+
/// and you wanted to change to
1273+
///
1274+
/// for<'a> fn(for<'b> fn(&'a x))
1275+
///
1276+
/// you would need to shift the index for `'a` into 1 new binder.
1277+
#[must_use]
1278+
pub fn shifted_in(self, amount: u32) -> DebruijnIndex {
12681279
DebruijnIndex { depth: self.depth + amount }
12691280
}
1281+
1282+
/// Update this index in place by shifting it "in" through
1283+
/// `amount` number of binders.
1284+
pub fn shift_in(&mut self, amount: u32) {
1285+
*self = self.shifted_in(amount);
1286+
}
1287+
1288+
/// Returns the resulting index when this value is moved out from
1289+
/// `amount` number of new binders.
1290+
#[must_use]
1291+
pub fn shifted_out(self, amount: u32) -> DebruijnIndex {
1292+
DebruijnIndex { depth: self.depth - amount }
1293+
}
1294+
1295+
/// Update in place by shifting out from `amount` binders.
1296+
pub fn shift_out(&mut self, amount: u32) {
1297+
*self = self.shifted_out(amount);
1298+
}
12701299
}
12711300

12721301
/// Region utilities

0 commit comments

Comments
 (0)