Skip to content

Do not record nested loop heads walked from an unreachable entry - #7193

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
fix/unreachable-nested-loop-heads
Nov 26, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
fix/unreachable-nested-loop-heads

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Since #7171, a loop nested in a converging loop starts from the head it converged to in the enclosing loop's previous pass. When an earlier pass walked that nested loop in a branch the condition ruled out (for example isset($table[$k]) while $table is still array{}), its variables were narrowed to never and the types read from them were *ERROR*. That head was kept and joined into the reachable entry of a later pass, where *ERROR* swallowed the real types. Arrays built in such loops ended up with *ERROR* values or as mixed.

ConvergedLoopHeads now remembers the entry each loop was walked from and does not record a head when a defined variable of that entry is never. The turbo-ext mirror does the same.

  • The regression test is both snippets from the issue.
  • The full test suite passes with and without the extension. smoke, side-by-side, signature-parity and walk-trace (default corpus) are identical.
  • The bench corpus gives the same output in the same wall time as before.

Closes phpstan/phpstan#15841

Amp thread: https://ampcode.com/threads/T-01a217b0-4f5c-74a6-90c4-a6c351fad7e3

A nested loop walked in a branch an enclosing loop's earlier pass ruled
out (such as isset() of an offset of a still empty array) converged with
its variables narrowed to never and the types read from them *ERROR*.
Its head was kept and joined into the reachable entry of a later pass,
where *ERROR* swallowed the real types.

ConvergedLoopHeads now remembers the entry each loop was walked from and
does not record a head when a defined variable of that entry is never.
The native mirror does the same.

Amp-Thread-ID: https://ampcode.com/threads/T-01a217b0-4f5c-74a6-90c4-a6c351fad7e3
@ondrejmirtes
ondrejmirtes merged commit 65be556 into 2.3.x Nov 26, 2026
414 of 424 checks passed
@ondrejmirtes
ondrejmirtes deleted the fix/unreachable-nested-loop-heads branch November 26, 2026 13:18
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.

Arrays built in nested foreach loops get *ERROR* values or become mixed (since phpstan-src #7171)

1 participant