fix(codegen): alias UDT array-element loop-locals so field mutation writes back#50
Merged
Merged
Conversation
…rites back Pine array elements of a user-defined type are references, so a global-scope, non-`var`, loop-nested UDT VarDecl bound from a UDT-array-element lvalue (`z = arr.get(i)` / `.first` / `.last`) that is later field-mutated must ALIAS the element — the mutation has to write back into the array. Previously this mis-lowered to a value copy: a global `while` loop hoisted the local to a class member whose in-loop init became a value-copy assignment, and a global `for` loop kept it a true local but the function-local alias path no-ops at global scope (`_current_func_body` is None). Both silently dropped the field mutation, causing stale-array over-fire. `_register_udt_array_get_ref_locals` now detects exactly this shape (UDT-array- element RHS AND global field-mutation AND loop-nested), suppresses the hoisted class member, and emits a fresh per-iteration `UDT& z = arr[i];` reference — the same alias form the non-hoisted function-local path already produces. Strictly gated: read-only get-locals keep value-copy semantics and function-local get-locals are excluded, so all other output is byte-identical. Fixes data/draft/finnp17-atm (+ its byte-identical twin): weak -> strong, engine/TV trade-count delta collapses from a ~1300-trade over-fire to +5. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.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.
Summary
Pine array elements of a user-defined type are references. A global-scope, non-
var, loop-nested UDTVarDeclbound from a UDT-array-element lvalue (z = arr.get(i)/.first/.last) that is later field-mutated must alias the element so the mutation writes back into the array.Previously this mis-lowered to a value copy:
whileloop hoisted the local to a class member whose in-loop init became a value-copy assignment, andforloop kept it a true local but the function-local alias path no-ops at global scope (_current_func_bodyis None).Both silently dropped the field mutation, causing stale-array over-fire.
Fix
_register_udt_array_get_ref_localsdetects exactly this shape (UDT-array-element RHS AND global field-mutation AND loop-nested), suppresses the hoisted class member, and emits a fresh per-iterationUDT& z = arr[i];reference — the same alias form the non-hoisted function-local path already produces.Strictly gated: read-only get-locals keep value-copy semantics and function-local get-locals are excluded, so all other output is byte-identical.
Verification (independent R7 regression gate)
PYTHONHASHSEED=0).PYTHONHASHSEED=0, fix vs HEAD. Only the 2 finnp17 dirs differ; every corpus and standard strategy is byte-identical. The finnp17 diff is preciselyZone z = Zone{}/z = active_zones[i]→Zone& z = active_zones[i].🤖 Generated with Claude Code