Repository navigation
Converge nested loops from their previous loop heads in enclosing loops' passes - #7171
Merged
Merged
Conversation
…ps' passes Loops nested in a top-level loop were walked only once in each of the enclosing loop's convergence passes, so a counter incremented by an inner loop stayed 0|1 and conditions depending on it never became true. Every loop now converges whenever it is walked, also inside an enclosing loop's passes (Bourdoncle's recursive iteration strategy, as used by IKOS and Astrée). To keep the cost close to walking them once, the head a nested loop converged to is kept between the enclosing loop's passes (ConvergedLoopHeads) and joined into its entry the next time, so a nested loop whose head is already stable converges in a single pass. That pass also stands in for the nested loop's final walk. - StatementContext: shouldConvergeLoops(), getLoopHeads(), enterLoopPass() and withLoopHeads(). Only pass walks record and read heads; top-level walks converge from their own entry. - WhileHandler, DoWhileHandler, ForHandler, ForeachHandler: converge when shouldConvergeLoops(), start nested loops from warmStart(), record the converged head. Foreach unrolling over constant arrays stays top-level. - turbo-ext: native ConvergedLoopHeads, the StatementContext slot, methods and direct entries, and the four loop handlers. - bug-10438: the expectation recorded the same bug; an inner foreach appends any number of strings, so list<string> is correct. Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
- ConvergedLoopHeads caches the warm entry it derived per loop and keeps the recorded head object when the new head equals it, so an enclosing loop's later passes bringing the same entry get the warm entry without joining it again. - A warm-started loop begins its convergence at the warm entry, skipping the first join of the initial body scope into it. - A loop whose joined entry equals the previous pass's keeps that entry instead of recomputing the same join after the convergence loop. - turbo-ext: the same in ConvergedLoopHeads and the four loop handlers. Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
ondrejmirtes
force-pushed
the
nested-loop-warm-start
branch
from
November 25, 2026 22:30
c3fb17b to
e9aecea
Compare
…track
Widening a variable between loop passes dropped every tracked expression on
it, such as $byHash[$hash] after $byHash[$hash]['list'][] = $x widened
$byHash. The next pass then no longer knew the offset exists, so the write
could start a new array{list: ...} and the shape lost its other keys:
array{block: Block, list: list<X>} -> array<'block'|'list', Block|list<X>>
with false "Cannot access an offset on Block|list<X>" after it. Nested loops
converging in the enclosing loop's passes hit this in shopware, matomo,
glpi, openemr, pmmp, phpstan-dba and phpstan-laravel.
An expression both passes track is now widened like any other; only one
that the other pass does not track is dropped, as before.
- bug-10438: $meta[$key] after $meta[$key] = [] is list<string> inside the
inner loop, no longer list<string>|string.
- bug-14598: $tmp[$id] = [] before the inner loop is no longer reported as
possibly missing after it.
- bug-12033: $this->cache[$key] built in a loop is no longer
array<...>|(ArrayAccess&PDOException).
- turbo-ext: the same in MutatingScope.cpp.
Amp-Thread-ID: https://ampcode.com/threads/T-01a17eac-1199-74a6-b3f7-dfbd8a084465
ondrejmirtes
force-pushed
the
nested-loop-warm-start
branch
from
November 26, 2026 05:28
e9aecea to
8e909f7
Compare
ondrejmirtes
added a commit
that referenced
this pull request
Nov 26, 2026
From https://github.com/phpstan/phpstan-src/actions/runs/45762120236, the push run of 40620d1 (#7171). Nested loops now converge inside their enclosing loop's passes, so the analysis benchmarks with nested loops and the NodeScopeResolver nested-foreach/nested-while cases do more work. Amp-Thread-ID: https://ampcode.com/threads/T-01a21657-7f83-77f3-b406-d5c68df62f37
This was referenced Nov 26, 2026
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.
Loops nested in a top-level loop were walked once per enclosing pass, so a counter an inner loop increments stayed
0|1and conditions depending on it never became true in the outer loop's passes.Every loop now converges whenever it is walked, also inside an enclosing loop's passes (Bourdoncle's recursive iteration strategy, as used by IKOS and Astrée). To avoid #6820's cost, the head each nested loop converged to is kept between the enclosing loop's passes (
ConvergedLoopHeads) and joined into its entry the next time, so a nested loop whose head is already stable converges in a single pass, and that pass stands in for its final walk. Top-level walks still converge from their own entry.Analysis time on generated nested-loop cases (seconds, single runs):
Converging nested loops inside the enclosing loop's passes exposed a widening bug that
2.3.xalready has at top level (integration tests failed for shopware, matomo, glpi, openemr, PMMP, phpstan-dba and phpstan-laravel). Widening a variable between loop passes dropped every tracked expression on it, such as$byHash[$hash]after$byHash[$hash]['list'][] = $x. The next pass then no longer knew the offset exists, the write could start a newarray{list: ...}, andarray{block: Block, list: list<X>}collapsed toarray<'block'|'list', Block|list<X>>with false "Cannot access an offset on Block|list". An expression both passes track is now widened like any other (separate commit; testnsrt/loop-generalize-nested-offset.phpfails on2.3.x). The issue bot found two more snippets this fixes: phpstan/phpstan#14598 (false "Offset 'A'|'B'|'C' might not exist") and phpstan/phpstan#12033, where the loop-built$this->cache[$key]wasarray<...>|(ArrayAccess&PDOException)and is nowarray<int<0, max>, ...>. That removes the "PDOException is not a list" and "null is not a list" tips the issue asks about, and the false "Cannot access offset int<0, max> on ...|PDOException" on line 51. The errors that remain are correct:flagsisarray<int, string>wherelist<string>is expected.turbo-ext mirrors the change (native
ConvergedLoopHeads,StatementContextslot and methods, the four loop handlers); smoke, signature parity, side-by-side, walk-trace and raw output identity pass.Closes phpstan/phpstan#15435
Closes phpstan/phpstan#15420
Closes phpstan/phpstan#10666
Closes phpstan/phpstan#14935
Closes phpstan/phpstan#14598
Closes phpstan/phpstan#12033