mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-22 16:30:04 +03:00
Fix an off-by-one modelling
This commit is contained in:
parent
63abc4096d
commit
fc52e9582e
@ -7390,31 +7390,22 @@ pub(crate) struct ListIndex(
|
|||||||
impl ListIndex {
|
impl ListIndex {
|
||||||
pub fn from_pattern_index(index: usize, arity: ListArity) -> Self {
|
pub fn from_pattern_index(index: usize, arity: ListArity) -> Self {
|
||||||
match arity {
|
match arity {
|
||||||
ListArity::Exact(_) => ListIndex::nth_head(index as _),
|
ListArity::Exact(_) => Self(index as _),
|
||||||
ListArity::Slice(head, tail) => {
|
ListArity::Slice(head, tail) => {
|
||||||
if index < head {
|
if index < head {
|
||||||
ListIndex::nth_head(index as _)
|
Self(index as _)
|
||||||
} else {
|
} else {
|
||||||
// Slice(2, 6)
|
// Slice(head=2, tail=5)
|
||||||
//
|
//
|
||||||
// s t ... w y z x q
|
// s t ... w y z x q
|
||||||
// 0 1 2 3 4 5 6 index
|
// 0 1 2 3 4 5 6 index
|
||||||
// 0 1 2 3 4 (index - head)
|
// 0 1 2 3 4 (index - head)
|
||||||
// 4 3 2 1 0 tail - (index - head)
|
// 5 4 3 2 1 (tail - (index - head))
|
||||||
ListIndex::nth_tail((tail - (index - head)) as _)
|
Self(-((tail - (index - head)) as i64))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn nth_head(offset: u64) -> Self {
|
|
||||||
Self(offset as _)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn nth_tail(offset: u64) -> Self {
|
|
||||||
let offset = offset as i64;
|
|
||||||
Self(-1 - offset)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) type Store<'a> = (Symbol, Layout<'a>, Expr<'a>);
|
pub(crate) type Store<'a> = (Symbol, Layout<'a>, Expr<'a>);
|
||||||
@ -7447,7 +7438,7 @@ pub(crate) fn build_list_index_probe<'a>(
|
|||||||
arguments: env.arena.alloc([list_sym]),
|
arguments: env.arena.alloc([list_sym]),
|
||||||
});
|
});
|
||||||
|
|
||||||
let offset = (list_index + 1).abs();
|
let offset = list_index.abs();
|
||||||
let offset_sym = env.unique_symbol();
|
let offset_sym = env.unique_symbol();
|
||||||
let offset_expr = Expr::Literal(Literal::Int((offset as i128).to_ne_bytes()));
|
let offset_expr = Expr::Literal(Literal::Int((offset as i128).to_ne_bytes()));
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user