Skip to content

Fix array unions, offset types and array_is_list() narrowing found while separating array unions - #7206

Merged
ondrejmirtes merged 7 commits into
2.3.xfrom
array-union-fixes
Nov 27, 2026
Merged

ondrejmirtes merged 7 commits into
2.3.xfrom
array-union-fixes

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Fixes found while trying to keep unions of general array types separate (follow-up to #7195, replacing #5506). The policy change itself is not part of this PR; it needs more work on performance first.

  • Keep the extra keys of an unsealed shape when merging it into a general array. array{id: mixed, ...<int|string, mixed>} merged with array<string, X> became array<string, mixed> and lost the int keys the shape may hold. The bug-7976 test now expects an error, and it is a true positive: Arr's iterator comes from the generic-less ArrayObject, so the inner arrays can get int keys.
  • Compare a subtracted mixed array key as written. array<mixed~'foo', mixed> compared its key through getIterableKeyType(), so it claimed to hold every array, also ones with the 'foo' key.
  • Intersect an offset value type of never to never. hasOffsetValue('a', *NEVER*) describes no array.
  • Generalize the shapes of a loop value together with its general arrays. When the previous pass already holds a general array, the shapes of both passes are generalized with it. This changes no result yet; without it, a union that keeps a shape next to a general array collapses the shapes into array<'count'|'path', *ERROR*> (PHPStan's own IgnoredErrorHelper hit this).
  • Let a member that tryUnion() made meet the members before it. (Baz&hasMethod(x))|(Foo~Baz&hasMethod(x)) now becomes Foo&hasMethod(x).
  • Let non-empty-array hold an intersection that is non-empty as a whole, as array<int>&hasOffsetValue('a', int).
  • Narrow an array that array_is_list() rejects to a non-empty one: the empty array is a list, so array{a?: string} becomes array{a: string} in the false branch.

The C++ mirrors of ArrayType, HasOffsetValueType, NonEmptyArrayType and MutatingScope are updated; the test suite passes with and without the extension.

…al array

mergeIntoGeneralArray() took only the known keys and values of a shape,
so array{id: mixed, ...<int|string, mixed>} merged with array<string, X>
became array<string, mixed> and lost the int keys the shape may hold.

Amp-Thread-ID: https://ampcode.com/threads/T-01a214a7-48c4-741b-a5d4-b003e2d9a39d
array<mixed~'foo', mixed> compared its key through getIterableKeyType(),
which turns the subtracted mixed into every array key, so it claimed to
hold every array, also ones with the 'foo' key. Against another general
array its key is now compared as written.

Amp-Thread-ID: https://ampcode.com/threads/T-01a214a7-48c4-741b-a5d4-b003e2d9a39d
hasOffsetValue('a', *NEVER*) describes no array, so intersecting an
array with it gives never instead of an array with an impossible offset.

Amp-Thread-ID: https://ampcode.com/threads/T-01a214a7-48c4-741b-a5d4-b003e2d9a39d
When the previous pass already holds a general array, the shapes of both
passes are now generalized with the general arrays. Comparing the shapes
of the two passes on their own only collapsed them into a general array
of their keys and values, as array<'count'|'path', *ERROR*>, once a union
keeps a shape next to a general array.

Amp-Thread-ID: https://ampcode.com/threads/T-01a214a7-48c4-741b-a5d4-b003e2d9a39d
When two members of a union merge into a new type, the scan now starts
over, so the new member is also compared with the members before it.
(Baz&hasMethod(x))|(Foo~Baz&hasMethod(x)) now becomes Foo&hasMethod(x).

Amp-Thread-ID: https://ampcode.com/threads/T-01a214a7-48c4-741b-a5d4-b003e2d9a39d
An intersection as array<int>&hasOffsetValue('a', int) is a non-empty
array although none of its members is one alone, so asking the members
one by one answered maybe.

Amp-Thread-ID: https://ampcode.com/threads/T-01a214a7-48c4-741b-a5d4-b003e2d9a39d
The empty array is a list, so an array that is not a list holds
something. In the false branch of array_is_list(), array<K, V> now
becomes non-empty-array<K, V> and array{a?: string} becomes
array{a: string}.

Amp-Thread-ID: https://ampcode.com/threads/T-01a214a7-48c4-741b-a5d4-b003e2d9a39d
@ondrejmirtes
ondrejmirtes merged commit 0143e40 into 2.3.x Nov 27, 2026
423 of 424 checks passed
@ondrejmirtes
ondrejmirtes deleted the array-union-fixes branch November 27, 2026 09: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.

1 participant