Skip to content

Print undeclared var names - #9015

Merged
anderseknert merged 2 commits into
open-policy-agent:mainfrom
sspaink:print-undeclared-var-names
Aug 13, 2026
Merged

Print undeclared var names#9015
anderseknert merged 2 commits into
open-policy-agent:mainfrom
sspaink:print-undeclared-var-names

Conversation

@sspaink

@sspaink sspaink commented Aug 13, 2026

Copy link
Copy Markdown
Member

fix: #5624

print() operands are safety-checked in a pass that runs after local vars have been renamed to generated ones, so some x; print(x) reported var __local0__ is undeclared instead of naming x. This maps the names back through RewrittenVars, as the template-string rewriter already does for the same error.

Local var rewriting runs before print calls are rewritten, so a
diagnostic about a print operand named the generated var (`var
__local0__ is undeclared`) rather than the one the author wrote. Map
generated names back through RewrittenVars, as the template-string
rewriter already does, and skip the redundant second walk of nested
bodies that reported these errors twice.

Fixes: open-policy-agent#5624
Signed-off-by: Sebastian Spaink <sebastianspaink@gmail.com>
Pin the positions, codes and messages in the compilecases corpus so that
other Rego implementations can consume them. Reaching these diagnostics
needs print calls left intact, so add an optional print_statements case
option alongside strict and experimental_keywords.

Signed-off-by: Sebastian Spaink <sebastianspaink@gmail.com>

@anderseknert anderseknert left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This has been annoying for too long hacking on Regal, so this is great news. Thank you!

@anderseknert
anderseknert merged commit 8b1b42e into open-policy-agent:main Aug 13, 2026
44 checks passed
@github-actions

Copy link
Copy Markdown
Benchmark Comparison (`e7c18bbc721841a54fbf67613c48f6423aed05dc` vs `8b1b42ee113bfe8bb91609eec951cd3d79c387a3`)
benchmark delta
BuildEqIndex/100 -1.00%
ParseModuleRulesBase/1000 +3.74%
ParseManyAnnotations +6.24%
ExprAppendText/simple_expr -4.00%
ObjectLookup/5 +7.00%
ObjectLookup/50 +10.97%
ObjectLookup/5000 +6.10%
ObjectGet/existing_interned_key -6.78%
ObjectGet/existing_float_key +6.17%
ObjectFind/5_50 +2.23%
ObjectFind/5_500 +5.10%
ObjectFind/500_500 +1.26%
ObjectCreationAndLookup/5 +5.48%
SetUnion/5000 -9.58%
ObjectCopy -2.55%
RefCopyNonGround/mixed/Copy -2.04%
TermHashing/100 -3.72%
TermHashing/1000 -4.34%
ObjectStringInterfaces/5000/json.Marshal -9.15%
ObjectConstruction/shuffled_keys/50 +3.06%
ObjectConstruction/shuffled_keys/500 +3.41%
ObjectConstruction/shuffled_keys/50000 -1.26%
ObjectConstruction/shuffled_keys/500000 -3.06%
ObjectConstruction/increasing_keys/500 -1.24%
ObjectConstruction/increasing_keys/5000 -3.91%
ObjectConstruction/increasing_keys/50000 -2.77%
ArrayString/500/String() -1.53%
SetString/500/String() -2.43%
InterfaceToValueInt/non-interned_int_value -7.44%
EscapeTemplateStringStringPart/{unes{caped_*_100 -2.80%
TemplateStringToString -5.21%
NoNodeTypeAllocatesOnAppend/and,_explicit -3.88%
NoASTTypeAllocatesOnAppendToBufferOfStringLength/string_with_escapes -2.49%

This comment was automatically generated by the benchmarks workflow.

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.

Var name lost in error message when using print and some without assignment

2 participants