fix(builtins): numbers.range_step descending ranges and undefined on bad step - #801
Open
Anand Krishnamoorthi (anakrish) wants to merge 2 commits into
Open
fix(builtins): numbers.range_step descending ranges and undefined on bad step#801Anand Krishnamoorthi (anakrish) wants to merge 2 commits into
Anand Krishnamoorthi (anakrish) wants to merge 2 commits into
Conversation
Two semantic fixes for numbers.range_step: 1. Non-positive or non-integer step: previously returned an error even in non-strict mode; now returns undefined in non-strict mode (matches OPA behaviour) and only errors in strict mode. 2. Descending ranges: the previous implementation attempted to compute num_elements via i64 arithmetic and bail if it overflowed. This failed for large integers because as_i64() returns None for bignums. Replace with a direction-aware step: negate the caller-supplied positive step when v1 > v2 so the loop walks downward naturally, removing the overflow-prone element-count pre-computation. - Rework the step guard to check non-integer/non-positive first and short-circuit with undefined when not strict - Compute step = incr or -incr based on v1 <= v2 comparison - Use Vec::new() instead of with_capacity (element count no longer pre-computed, still bounded by enforce_limit) - Add YAML regression test: ascending, step-2, descending, single- element, non-integer-step-undefined, zero-step-undefined Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
OPA v1.19.1 automatically negates the step when start > stop to produce a descending range. Our implementation matches this. Update builtins.md to document it rather than leaving the description blank. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Two semantic fixes for
numbers.range_step:1. Non-positive or non-integer step in non-strict mode
Previously the function returned an error for a non-positive/non-integer step even when
strict = false. OPA returns undefined in that case. The fix checks the step validity first and returnsValue::Undefinedwhen not strict.2. Descending ranges and large-integer support
The previous implementation pre-computed
num_elementsviai64arithmetic and calledbail!whenas_i64()returnedNone(i.e., for bignum integers). This made descending ranges with large integers unusable.Replace with a direction-aware step: negate the caller-supplied positive step when
v1 > v2so the loop walks downward naturally. This removes the overflow-prone element-count pre-computation entirely, andVec::new()+enforce_limit()bounds growth as before.Tests
New
tests/interpreter/cases/builtins/numbers/range_step.yamlcovering: ascending, step-2, descending, single-element, non-integer-step-undefined (non-strict), zero-step-undefined (non-strict).Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com