diff options
author | Nadrieril | 2019-05-09 16:32:30 +0200 |
---|---|---|
committer | Nadrieril | 2019-05-09 16:32:30 +0200 |
commit | 7538e29275720407ac172bb05cdbc028d95ff921 (patch) | |
tree | 79ad968a7ef6e36e5a4e7fc19df281c46ac68f6c /dhall_syntax | |
parent | 2020d41874f7681ba948a40d8e8f8993d651a81c (diff) |
Make shift fallible and improve shift ergonomics
Diffstat (limited to '')
-rw-r--r-- | dhall_syntax/src/core/expr.rs | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/dhall_syntax/src/core/expr.rs b/dhall_syntax/src/core/expr.rs index 4bfd224..1a33ed1 100644 --- a/dhall_syntax/src/core/expr.rs +++ b/dhall_syntax/src/core/expr.rs @@ -442,22 +442,26 @@ pub fn rc<E>(x: Expr<X, E>) -> SubExpr<X, E> { /// Add an isize to an usize /// Panics on over/underflow -fn add_ui(u: usize, i: isize) -> usize { - if i < 0 { - u.checked_sub(i.checked_neg().unwrap() as usize).unwrap() +fn add_ui(u: usize, i: isize) -> Option<usize> { + Some(if i < 0 { + u.checked_sub(i.checked_neg()? as usize)? } else { - u.checked_add(i as usize).unwrap() - } + u.checked_add(i as usize)? + }) } impl<Label: PartialEq + Clone> V<Label> { - pub fn shift(&self, delta: isize, var: &V<Label>) -> Self { + pub fn shift(&self, delta: isize, var: &V<Label>) -> Option<Self> { let V(x, n) = var; let V(y, m) = self; - if x == y && n <= m { - V(y.clone(), add_ui(*m, delta)) + Some(if x == y && n <= m { + V(y.clone(), add_ui(*m, delta)?) } else { V(y.clone(), *m) - } + }) + } + + pub fn over_binder(&self, x: &Label) -> Option<Self> { + self.shift(-1, &V(x.clone(), 0)) } } |