Skip to content

Keep the shapes of loop-built nested arrays in exact unions and widening - #7225

Merged
ondrejmirtes merged 2 commits into
2.3.xfrom
fix-15860-nested-loop-arrays
Nov 27, 2026
Merged

ondrejmirtes merged 2 commits into
2.3.xfrom
fix-15860-nested-loop-arrays

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Closes phpstan/phpstan#15860

Since 0e9fdb5 (exact shape unions), arrays built in a loop that appends to a different key in each branch became mixed or degraded to oversized arrays with *ERROR* values. That broke openemr/openemr (interface/forms/CAMOS/notegen.php) in the integration tests.

Two causes, one commit each:

  1. Union shapes into one only when it holds the other exactly. tryUnionConstantArray() took a shape that isSuperTypeOf() the other as their union. For shapes without bleeding edge, isSuperTypeOf() lets a shape hold one with more keys, so at a key holding shapes array{}|array{string} held array{0: string, 1?: string} and the union dropped the lists of two. A loop appending to such keys then grew one key per pass, hit the pass limit and made the variable mixed. That is what happens in openemr's getFormData(). A shape is now their union only when merging the other into it leaves it the same, and the list-length path asks the same of its values.
  2. Fold the shapes in tagged unions and nested general arrays when widening. The exact union keeps a loop value's variants apart, but ArrayWidening folded only bare shapes. So values merged into a general array, which are such unions, degraded to oversized arrays. Unions of arrays are now split into their members, and the general arrays among the types merge first (GeneralArrayWidening), as the union after the widening merges them anyway.

The issue snippet now infers a tagged union of 7 variants, each with one category key as non-empty-list<string> (the else branch makes every iteration append somewhere). openemr's getFormData() infers the same type as before 0e9fdb5.

nsrt/bug-15860.php (the issue snippet, two variants and openemr's getFormData()) also runs in NoBleedingEdgeTypeInferenceTest, because the bug in (1) only shows without bleeding edge, where shapes are not sealed.

tryUnionConstantArray() took a shape that isSuperTypeOf() the other as
their union, but isSuperTypeOf() lets a shape without sealedness hold one
with more keys. At a key holding shapes, array{}|array{string} held
array{0: string, 1?: string}, so the union of
array{l1: array{0: string, 1?: string}, l2: array{}|array{string}} and
array{l1: array{}|array{string}, l2: array{}|array{0: string, 1?: string}}
lost the lists of two at l1. A loop appending to such keys then grew one
key per pass and ran out of passes, its arrays becoming mixed.

A shape is now their union only when merging the other into it leaves it
the same. The lists of one length range ask the same of their values.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21c4a-ecc4-75e3-bdd6-8fd2c9f2fe01
The exact union keeps the variants of a loop value apart, as
array{l1: non-empty-list<string>, l2: list<string>}|array{l1: list<string>, l2: non-empty-list<string>},
but ArrayWidening folded only bare shapes, so the values merged into a
general array, which are such unions, degraded to oversized arrays.

The unions of arrays among the types to widen are now split into their
members, and the general arrays among them merge first, as the union
after the widening merges them - the shapes nested in them fold with each
other instead of each degrading on its own.

Amp-Thread-ID: https://ampcode.com/threads/T-01a21c4a-ecc4-75e3-bdd6-8fd2c9f2fe01
@ondrejmirtes
ondrejmirtes merged commit d030b2b into 2.3.x Nov 27, 2026
878 of 917 checks passed
@ondrejmirtes
ondrejmirtes deleted the fix-15860-nested-loop-arrays branch November 27, 2026 16:12
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.

Nested arrays built in an if/elseif loop become mixed or *ERROR* (since phpstan-src 0e9fdb53e, #7195)

1 participant