introduce shifted_in, shifted_out and friends

Co-authored-by: csmoe <35686186+csmoe@users.noreply.github.com>
This commit is contained in:
Niko Matsakis 2018-05-25 09:06:54 -04:00
parent a2c4d4e2f0
commit 69ab6f2995
4 changed files with 37 additions and 7 deletions

View File

@ -71,6 +71,7 @@
#![feature(test)]
#![feature(in_band_lifetimes)]
#![feature(macro_at_most_once_rep)]
#![feature(inclusive_range_methods)]
#![recursion_limit="512"]

View File

@ -123,11 +123,11 @@ impl Region {
fn shifted(self, amount: u32) -> Region {
match self {
Region::LateBound(depth, id, origin) => {
Region::LateBound(depth.shifted(amount), id, origin)
Region::LateBound(debruijn, id, origin) => {
Region::LateBound(debruijn.shifted_in(amount), id, origin)
}
Region::LateBoundAnon(depth, index) => {
Region::LateBoundAnon(depth.shifted(amount), index)
Region::LateBoundAnon(debruijn, index) => {
Region::LateBoundAnon(debruijn.shifted_in(amount), index)
}
_ => self,
}

View File

@ -515,7 +515,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for RegionReplacer<'a, 'gcx, 'tcx> {
pub fn shift_region(region: ty::RegionKind, amount: u32) -> ty::RegionKind {
match region {
ty::ReLateBound(debruijn, br) => {
ty::ReLateBound(debruijn.shifted(amount), br)
ty::ReLateBound(debruijn.shifted_in(amount), br)
}
_ => {
region
@ -531,7 +531,7 @@ pub fn shift_region_ref<'a, 'gcx, 'tcx>(
{
match region {
&ty::ReLateBound(debruijn, br) if amount > 0 => {
tcx.mk_region(ty::ReLateBound(debruijn.shifted(amount), br))
tcx.mk_region(ty::ReLateBound(debruijn.shifted_in(amount), br))
}
_ => {
region

View File

@ -1264,9 +1264,38 @@ impl DebruijnIndex {
DebruijnIndex { depth: depth }
}
pub fn shifted(&self, amount: u32) -> DebruijnIndex {
/// Returns the resulting index when this value is moved into
/// `amount` number of new binders. So e.g. if you had
///
/// for<'a> fn(&'a x)
///
/// and you wanted to change to
///
/// for<'a> fn(for<'b> fn(&'a x))
///
/// you would need to shift the index for `'a` into 1 new binder.
#[must_use]
pub fn shifted_in(self, amount: u32) -> DebruijnIndex {
DebruijnIndex { depth: self.depth + amount }
}
/// Update this index in place by shifting it "in" through
/// `amount` number of binders.
pub fn shift_in(&mut self, amount: u32) {
*self = self.shifted_in(amount);
}
/// Returns the resulting index when this value is moved out from
/// `amount` number of new binders.
#[must_use]
pub fn shifted_out(self, amount: u32) -> DebruijnIndex {
DebruijnIndex { depth: self.depth - amount }
}
/// Update in place by shifting out from `amount` binders.
pub fn shift_out(&mut self, amount: u32) {
*self = self.shifted_out(amount);
}
}
/// Region utilities