Skip to content

Do not record or keep conditional expressions that stop loops from converging - #7226

Merged
ondrejmirtes merged 2 commits into
2.3.xfrom
loop-convergence-15850
Nov 27, 2026
Merged

ondrejmirtes merged 2 commits into
2.3.xfrom
loop-convergence-15850

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

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:

$c = $c->parent;
if ($c !== null) {
	$c->parent; // Node, now Node|null
}

After the assignment, $c->parent is the parent of the new $c, but the holder "if $c is Node then $c->parent is Node" was still recorded. In for ($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 while loops 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.cpp and MutatingScope.cpp.

Results, from nsrt/bug-15850.php (the two playground reproducers from the issue, verbatim, plus the straight-line case):

  • PMMP HandlerList::getListenerList(): $handlerLists is non-empty-list<HandlerList<contravariant TEvent of Event>>; it was mixed.
  • openemr nested counters: $items_c is int<0, max>; it was mixed.

Not covered: the third trigger from the issue comment, an extension type whose equals() compares uniqid() keys, still reaches the limit.

Tests: make tests passes with and without the turbo extension loaded, make phpstan is clean, and turbo-ext/tests/smoke.php and walk-trace.php (on the new fixture) match.

Closes phpstan/phpstan#15850

…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
@ondrejmirtes ondrejmirtes changed the title Fix loops that stopped converging since loop widening against the entry Do not record or keep conditional expressions that stop loops from converging Nov 27, 2026
@ondrejmirtes
ondrejmirtes merged commit f936452 into 2.3.x Nov 27, 2026
879 of 917 checks passed
@ondrejmirtes
ondrejmirtes deleted the loop-convergence-15850 branch November 27, 2026 14:08
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.

Loops hit the iteration limit and their variables become mixed (since phpstan-src #7183)

1 participant