Skip to content

at: Python-style negative indexing#183

Merged
danieljohnmorris merged 2 commits into
mainfrom
fix/at-negative-index
May 12, 2026
Merged

at: Python-style negative indexing#183
danieljohnmorris merged 2 commits into
mainfrom
fix/at-negative-index

Conversation

@danieljohnmorris
Copy link
Copy Markdown
Collaborator

Follow-up to PR #161. Negative index wraps from end: at xs -1 returns last, at xs -2 second-last, etc. Integer-only guard preserved (at xs -0.5 still errors). The verbose at xs -(len xs) 1 workaround becomes at xs -1. 17 new cross-engine tests; old 'negative errors' tests replaced per the design change.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 11, 2026

Codecov Report

❌ Patch coverage is 91.89189% with 9 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/vm/mod.rs 85.93% 9 Missing ⚠️

📢 Thoughts on this report? Let us know!

Follow-up to PR #161 at builtin. Negative index wraps from end: at xs -1 returns last element, at xs -2 second-last, etc. Integer-only guard preserved. The at xs -(len xs) 1 workaround now becomes the much terser at xs -1.
@danieljohnmorris danieljohnmorris force-pushed the fix/at-negative-index branch from 0282254 to 779a9b2 Compare May 11, 2026 23:58
@danieljohnmorris danieljohnmorris merged commit 346b1a9 into main May 12, 2026
4 of 5 checks passed
@danieljohnmorris danieljohnmorris deleted the fix/at-negative-index branch May 12, 2026 00:02
danieljohnmorris added a commit that referenced this pull request May 14, 2026
resolve_slice_bound, resolve_take_count, resolve_drop_count: small
inline helpers that take (raw_i64, len_usize) and return the resolved
prefix position. Negative inputs count from the end and clamp at -len,
matching the semantics PR #183 already established for `at xs i`.

Splitting these out of the engine call sites lets the tree-walker, VM,
and Cranelift JIT helpers all dispatch through the same logic — no
chance of one backend silently diverging on a `-len` edge case.

8 unit tests covering positive, -1, -len, beyond-len, len=0 boundaries.
danieljohnmorris added a commit that referenced this pull request May 14, 2026
slc accepts negative start and end (each resolved against the input
length), matching `at xs -1` from PR #183. take and drop accept
negative counts: `take -k xs` keeps all but the last k, `drop -k xs`
keeps only the last k. Both equivalent to Python's xs[:-k] and xs[-k:].

slc also picks up the same fract!=0 integer-validation check that
take and drop already had — previously slc silently cast non-integer
floats to usize. Brings the three slice builtins to parity with each
other and with at.

Routes through the resolve_* helpers in builtins.rs so the rule is
defined once and reused by VM + JIT.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant