Repository navigation
compiler: replace true expr when appending to empty body - #8299
Merged
anderseknert merged 1 commit intoFeb 6, 2026
Merged
Conversation
✅ Deploy Preview for openpolicyagent ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
srenatus
approved these changes
Feb 6, 2026
srenatus
left a comment
Member
There was a problem hiding this comment.
Solid choice. I guess one step further would be to just accept empty bodies. No conditions == true, actually putting a true there was a bit of a hack in the first place. 🤔
anderseknert
force-pushed
the
compiler-remove-true
branch
from
February 6, 2026 09:37
323657a to
4da87e4
Compare
Member
Author
|
@srenatus indeed. I guess there's some risk that external tools working with the AST would break, but a good next step would IMHO be to ensure that OPA handles nil bodies anywhere they may appear. Once we do, we could make it a parser option to leave empty bodies be, and have OPA use that internally anywhere the AST isn't exposed. And have external tools benefit should they choose to. |
anderseknert
force-pushed
the
compiler-remove-true
branch
from
February 6, 2026 09:55
4da87e4 to
90bc9e5
Compare
The OPA parser places a single boolean true term inside an expression
to populate empty rule bodies, so that e.g. `x := input.x` parses as:
```rego
x := input.x if {
true
}
```
When the compiler rewrites rules in its various stages, it would
previously ignore such "true expressions" and simply append what
it rewrote as the next expression:
```
x := __local1__ if {
true
__local1__ = input.x
}
```
This is of course redundant, as the only purpose of the `true` expr
was to mark the body as empty — which it no longer is! While fairly
harmless, this contributes noise to the compiler's output, which is
then passed onwards to topdown evaluation. Not a huge deal, but
measurable!
**BenchmarkRegalLintingItselfPrepareOnce**
```
502062750 ns/op 1900946509 B/op 45426050 allocs/op
496509250 ns/op 1893094357 B/op 45112660 allocs/op
```
With this change, the compiler now replaces an empty true expression with
a new one whenever we append to a body. Meaning `true` remains to mark
empty bodies, but not in non-empty ones.
This is more about elegance than performance though :)
Signed-off-by: Anders Eknert <anders.eknert@apple.com>
anderseknert
force-pushed
the
compiler-remove-true
branch
from
February 6, 2026 09:56
90bc9e5 to
0d32e83
Compare
anderseknert
enabled auto-merge (squash)
February 6, 2026 09:59
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.
The OPA parser places a single boolean true term inside an expression to populate empty rule bodies, so that e.g.
x := input.xparses as:When the compiler rewrites rules in its various stages, it would previously ignore such "true expressions" and simply append what it rewrote as the next expression:
This is of course redundant, as the only purpose of the
trueexpr was to mark the body as empty — which it no longer is! While fairly harmless, this contributes noise to the compiler's output, which is then passed onwards to topdown evaluation. Not a huge deal, but measurable!BenchmarkRegalLintingItselfPrepareOnce
With this change, the compiler now replaces an empty true expression with a new one whenever we append to a body. Meaning
trueremains to mark empty bodies, but not in non-empty ones.This is more about elegance than performance though :)