Widen only the variables a loop can write when its scope converges - #6421
Merged
Conversation
Collaborator
|
You've opened the pull request against the latest branch 2.3.x. PHPStan 2.3 is not going to be released for months. If your code is relevant on 2.2.x and you want it to be released sooner, please rebase your pull request and change its target to 2.2.x. |
A variable the loop never writes enters every iteration with its value
from before the loop, so its type differs between convergence passes
only through narrowing by the loop's conditions. Generalizing it lost
its type for nothing: `while ($xi < $xn) { $xi += 0.1; }` widened an
`int<1, max>` $xn to `int`.
The convergence of while, do-while, for and foreach loops now unions the
types of such variables instead. The variables a loop can write are
collected from its AST, from the arguments the pass's variable flow
marks as passed by reference, and from references created before the
loop. Variable variables, extract(), parse_str(), eval and include keep
widening every variable.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014LVEGd9G9w8j64EZQ7rysC
ondrejmirtes
force-pushed
the
loop-generalize-written-variables
branch
from
September 11, 2026 15:30
362aaef to
56ccf25
Compare
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.
Closes phpstan/phpstan#12666
When a loop's scope converges,
MutatingScope::generalizeWith()widened every variable whose type differed between two passes. A variable the loop never writes differs only through narrowing by the loop's condition. Inwhile ($xi < $xn) { $xi += 0.1; }the first pass narrows$xntoint<2, max>, the next pass cannot once$xiis a float, and generalizing the two turnedint<1, max>intoint.Such a variable enters every iteration with its value from before the loop, so the convergence passes of while, do-while, for and foreach loops now union its types instead of generalizing them.
LoopWrittenVariableNamescollects the variables a loop can write:static,global,unsetand by-reference closure uses, cached on the loop node. Variable variables,extract(),parse_str(),evalandincludemake the set unknown, and every variable is widened as before.generalizeWith().$ref = &$xn;followed by$ref = $xn + 1;inside the loop still widens$xn.The goto and closure by-reference fixpoints and the post-loop generalization in
ForHandlerare unchanged. The PR targets 2.3.x because the variable flow it reads exists only there.Tests
nsrt/bug-12666.phpis the sample from the issue.nsrt/loop-generalize-written-variables.phpcovers do-while, for, foreach, a constant array with unsealed keys and a variable passed by value inside the loop. It also covers the writes that must still widen: an increment, destructuring, a write through a reference, andarray_push()in the body and in a for update.Both files fail without the fix. The variants file also fails when the by-reference names from the flow are dropped, and when the reference scan is dropped.
Verification
make phpstan: no errors.phpcsand lint on the changed files: clean.🤖 Generated with Claude Code
https://claude.ai/code/session_014LVEGd9G9w8j64EZQ7rysC