Repository navigation
fix(rvm): use arithmetic negation when folding constants - #935
Draft
Anand Krishnamoorthi (anakrish) wants to merge 1 commit into
Draft
Anand Krishnamoorthi (anakrish) wants to merge 1 commit into
Anand Krishnamoorthi (anakrish) wants to merge 1 commit into
Conversation
Number::neg implements bitwise complement. Fold unary numeric constants with subtraction from zero instead, and cover interpreter/RVM decision parity across collection forms, execution modes, strictness, and Program JSON round trips. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 84271ec2-b466-4cfe-bf58-848f4dceb842
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Constant folding bypasses unary-minus source-span validation for negative fractions, allowing p := [-(-1.5)] to compile even though the interpreter rejects it.
1 open issue
What changed in this PR
Corrects unary numeric constant folding in the RVM compiler by using arithmetic negation instead of bitwise complement. This keeps the change focused on constant folding, separate from #930’s definedness and matching work.
Changes:
- Fold unary numeric constants using subtraction from zero.
- Add six regression cases comparing interpreter results across both VM modes, strictness settings, and Program JSON round trips.
| File | Description |
|---|---|
tests/rvm/compiler.rs |
Adds unary-negation regression coverage for numbers and collections. |
src/languages/rego/compiler/expressions/collection_literals.rs |
Replaces bitwise complement with arithmetic subtraction during constant folding. |
Review effort: Max
| value: Value::Number(n), | ||
| .. | ||
| } => Some(Value::Number(n.neg()?)), | ||
| } => Some(Value::Number( |
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.

Summary
Use arithmetic subtraction from zero when folding unary numeric constants in the RVM compiler.
Number::neg()implements bitwise complement, not arithmetic negation.For example:
The interpreter returns
[-1], but the unfixed RVM compiler folds this to[-2].Validation
cargo xtask ci-debug, andcargo xtask pre-commitpass using CI-pinned Rust 1.95.0.