Repository navigation
Do not record or keep conditional expressions that stop loops from converging - #7226
Merged
Merged
Conversation
…he assigned variable After `$c = $c->parent`, the expression `$c->parent` names the parent of the new `$c`, but the assignment still recorded "if $c is Node then $c->parent is Node", so `if ($c !== null)` narrowed the next node's parent to non-null. In `for ($c = $this; $c !== null; $c = $c->parent)` the holder appeared and disappeared on alternate passes, so the loop never converged, reached the iteration limit and every variable it writes became mixed. Amp-Thread-ID: https://ampcode.com/threads/T-01a21c26-db20-7349-9bc4-d608e06a112b
…ng a loop scope generalizeWith() widened changing variables but kept every holder of the new pass, so a holder whose types grew by one literal per pass (`if $save_item_flag=0 then $items_c is 1|2|...|n` in nested while loops with counters) kept the loop changing until the iteration limit, and every variable it writes became mixed. Intersecting with the previous pass's holders drops the ones still changing; the set can only shrink, so it converges. Amp-Thread-ID: https://ampcode.com/threads/T-01a21c26-db20-7349-9bc4-d608e06a112b
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.
Since #7183, two ordinary loop forms reach the iteration limit, and every variable they write becomes
mixed. The phpstan/phpstan integration tests caught this on PMMP and openemr. Each form has a different cause, so there are two commits.1. An assignment projected its narrowing onto an expression that reads the assigned variable. This is unsound even without a loop:
After the assignment,
$c->parentis the parent of the new$c, but the holder "if $c is Node then $c->parent is Node" was still recorded. Infor ($currentList = $this; $currentList !== null; $currentList = $currentList->parentList)this holder appeared and disappeared on alternate passes, so the loop never converged.AssignHandler::isExprSafeToProjectThroughVariable()now rejects any expression that reads the assigned variable. Before, it rejected only the variable itself.2. Loop widening kept holders that changed on every pass. In nested
whileloops with counters, the variable types were stable, but a holder grew by one literal per pass (if $save_item_flag=0 then $items_c is 1|2|…|n).generalizeWith()widens changing variables but kept every holder of the new pass. It now keeps only the holders both passes derived (ScopeOps::intersectConditionalExpressions()). A holder that is still changing is dropped, as a changing variable is widened. The set can only shrink, so the loop converges.Both changes are mirrored in
turbo-ext/src/AssignHandler.cppandMutatingScope.cpp.Results, from
nsrt/bug-15850.php(the two playground reproducers from the issue, verbatim, plus the straight-line case):HandlerList::getListenerList():$handlerListsisnon-empty-list<HandlerList<contravariant TEvent of Event>>; it wasmixed.$items_cisint<0, max>; it wasmixed.Not covered: the third trigger from the issue comment, an extension type whose
equals()comparesuniqid()keys, still reaches the limit.Tests:
make testspasses with and without the turbo extension loaded,make phpstanis clean, andturbo-ext/tests/smoke.phpandwalk-trace.php(on the new fixture) match.Closes phpstan/phpstan#15850