Repository navigation
Converge loops nested in a top-level loop's convergence passes instead of walking them once - #6820
Open
phpstan-bot wants to merge 1 commit into
Open
phpstan-bot wants to merge 1 commit into
phpstan-bot wants to merge 1 commit into
Conversation
…d of walking them once
- StatementContext: add shouldConvergeLoops() and enterLoopPass(). A top-level
loop's passes now run in a deep context whose loops still converge; the
passes of those nested loops run in a plain deep context, where loops are
walked once, so the cost stays polynomial in the nesting depth.
- ForeachHandler, WhileHandler, DoWhileHandler, ForHandler: converge when
shouldConvergeLoops() (was isTopLevel()) and walk passes with
enterLoopPass() (was enterDeep()). Foreach unrolling over constant arrays
stays top-level only.
- Previously a nested loop inside an outer pass ran a single iteration, so a
counter it increments stayed 0|1. A condition such as `$n > 1` was then
never true in the outer loop's passes, and an array appended under it
converged to array{}|array{string} instead of list<string>.
- Analogous cases fixed by the same change and covered by tests: nested
while/for/do-while inner loops, while/for/do-while outer loops, a flag set
inside the inner body, and three levels of nesting.
- turbo-ext: mirror StatementContext (new slot, methods, direct entries) and
the four loop handlers; regenerate generated/StatementContext.h.
- bug-10438: the expectation recorded the same bug; an inner foreach appends
any number of strings, so list<string> is correct.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Summary
An array appended inside a loop that also contains a nested loop came out as
array{}|array{string}instead oflist<string>. With bleeding edge,sortArray.noopthen reported a false positive onsort(). This change makes a nested loop converge during the outer loop's convergence passes, instead of running only one iteration there.Changes
src/Analyser/StatementContext.php: newshouldConvergeLoops()andenterLoopPass().src/Analyser/StmtHandler/{Foreach,While,DoWhile,For}Handler.php:shouldConvergeLoops()instead ofisTopLevel().enterLoopPass()instead ofenterDeep().turbo-ext/: native mirrors of the above.StatementContext.cpp: new slot, methods, andpt_statement_context_should_converge_loops()/pt_statement_context_enter_loop_pass().generated/StatementContext.hregenerated.tests/PHPStan/Analyser/nsrt/bug-10438.php: the old expectationarray<string, array{}|array{string}|string>recorded this same bug. The inner foreach appends any number of strings, soarray<string, list<string>|string>is correct.Root cause
Before this change, loops converge only at the top level. Inside an outer loop's convergence pass, every nested loop body was walked exactly once. In the issue's code,
$ntherefore came out of the inner loop as0|1during the passes, and$n > 1was never true. The outer loop converged with$keysstillarray{}. Only the final walk, which does converge the inner loop, saw$nasint<0, max>, and it appended once, givingarray{}|array{string}. This is not a 2.3 regression: 2.2.9 infers the same type.The pattern affects every loop kind on both sides. I tested inner
foreach/while/for/do-whileloops inside an outerforeach, and an innerforeachinside outerwhile/for/do-whileloops. All of them were broken and all are fixed by the same change.Performance
Self-analysis of
src/(interleaved A/B, user CPU, no turbo extension): 207s before, 221s after, about 6–7% slower. This is the cost of iterating nested loops to a fixpoint during the outer passes, where they used to run once. A variant where the nested loop's final walk did not converge its own nested loops was measured at ~219s, within noise, and was not adopted.Test
tests/PHPStan/Analyser/nsrt/bug-15435.php: the issue's reproducer verbatim, plus analogous cases:while,for, anddo-whileloops;while,for, anddo-whileloops;All nine assertions fail without the fix (
array{}|array{string}) and pass with it.SortWithoutEffectRuleTest::testBug15435: nosortArray.noopfalse positive onsort($keys). It fails without the fix.Full
make testspasses with and without the turbo extension loaded.turbo-ext/tests/walk-trace.phpis identical (676,444 trace lines).smoke.phpandsignature-parity.phpare OK, andmake phpstanis clean.Fixes phpstan/phpstan#15435
🤖 Generated with Claude Code