Skip to content

fix(rvm): use arithmetic negation when folding constants - #935

Draft
Anand Krishnamoorthi (anakrish) wants to merge 1 commit into
microsoft:mainfrom
anakrish:fix/rvm-unary-constant-folding
Draft

Anand Krishnamoorthi (anakrish) wants to merge 1 commit into
microsoft:mainfrom
anakrish:fix/rvm-unary-constant-folding

Conversation

@anakrish

@anakrish Anand Krishnamoorthi (anakrish) commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

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:

package test
p := [-(1)]

The interpreter returns [-1], but the unfixed RVM compiler folds this to [-2].

Validation

  • Regression coverage compares interpreter and RVM decisions for unary constants in collections across execution modes, strictness settings, and Program JSON round trips.
  • All 36 compiler tests, cargo xtask ci-debug, and cargo xtask pre-commit pass using CI-pinned Rust 1.95.0.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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(
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.

2 participants