at: Python-style negative indexing#183
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is
📢 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.
0282254 to
779a9b2
Compare
6 tasks
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.
This was referenced May 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to PR #161. Negative index wraps from end:
at xs -1returns last,at xs -2second-last, etc. Integer-only guard preserved (at xs -0.5still errors). The verboseat xs -(len xs) 1workaround becomesat xs -1. 17 new cross-engine tests; old 'negative errors' tests replaced per the design change.