Skip to content

Reuse assignment targets parsed for the for tag - #4872

Merged
fabpot merged 1 commit into
twigphp:3.xfrom
fabpot:reuse-for-assignment-targets
Jul 25, 2026
Merged

Reuse assignment targets parsed for the for tag#4872
fabpot merged 1 commit into
twigphp:3.xfrom
fabpot:reuse-for-assignment-targets

Conversation

@fabpot

@fabpot fabpot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

ForTokenParser rebuilds the loop targets returned by parseAssignmentExpression() into new AssignContextVariable instances, copying only the name and line number. But the parsed targets are already AssignContextVariable nodes with exactly those values, so the rebuild is a no-op left over from older Twig versions where for-targets were parsed as general expressions and needed normalizing.

Reusing the parsed nodes directly removes dead code, and makes the parser more robust: any metadata attached to the targets during parsing (now or in the future) is preserved instead of being silently dropped.

@upsun-dispatch

Copy link
Copy Markdown

📋 PR Summary

).Slreation actually preverifies the actPR's description a no. removno free anfixI no is- targjust, correctly identifying the removed rebuild as a behavior-preserving no-op op. The parsed targets from parseAssignmentExpression() are AssignContextVariable nodes carrying the same name attribute and template line the the rebuilt nodes copied.

Changes
Layer / File(s) Summary
for-tag parsing
src/TokenParser/ForTokenParser.php Removes the two lines that reconstructed AssignContextVariable nodes for the key and value loop targets, reusing the nodes already produced by parseAssignmentExpression() instead.

@fabpot
fabpot merged commit 293d055 into twigphp:3.x Jul 25, 2026
54 checks passed
@fabpot
fabpot deleted the reuse-for-assignment-targets branch July 25, 2026 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant