Skip to content

Keep unions of general arrays told apart by their kinds separate - #7211

Merged
ondrejmirtes merged 5 commits into
2.3.xfrom
array-unions-separate
Nov 27, 2026
Merged

ondrejmirtes merged 5 commits into
2.3.xfrom
array-unions-separate

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

With bleeding edge, union() no longer merges every general array with the other arrays into one general array of all their keys and values. Arrays whose keys or values are of kinds that share no value stay apart: array<int>|array<string> holds no array that has both ints and strings, so it stays a union. Arrays that cannot be told apart merge as before: array<int, string>|array{foo: string} is still array<int|'foo', string>.

This is the policy #5506 proposed, built on the union rules from #7195:

  • OverlappingArrayUnionRule (new, in union()) merges each general array with the arrays it cannot be told apart from by TypeKind, recursing into values that are arrays. Telling arrays apart only by kinds keeps unions small: there are only so many kinds, so members cannot multiply level by level in nested arrays, which is what made keeping every array apart explode.

  • GeneralArrayUnionRule (in widen() only) bounds how many general arrays a join keeps apart. Past GENERAL_ARRAYS_LIMIT, the two whose kinds differ the least merge, and the merged values widen too, so nested arrays are bounded the same way. Past the limit of constant array values, shapes fold as before but no longer merge into the general arrays next to them.

  • A few signatures written as unions of general arrays (string[]|int[] and the like) are now written as the one general array they always meant, because such unions keep their members now.

  • MissingTypehintCheck checks each member of a union on its own, so a union of template types such as (T1 of array)|T2 of array, which no longer merges, is not reported as an array without a value type.

Without bleeding edge, union() and widen() merge arrays as they did before (MergedGeneralArrayUnionRule, 2.3.x's rule under a new name), because keeping them apart reports new errors in projects that rely on the merged type. Analysing every nsrt file without bleeding edge gives the same output as 2.3.x.

With bleeding edge, error messages and inferred types show these arrays apart, for example array<int>|array<stdClass> instead of array<int|stdClass>. The test expectations changed accordingly.

Closes phpstan/phpstan#7759
Closes phpstan/phpstan#8963
Closes phpstan/phpstan#11176
Closes phpstan/phpstan#12195
Closes phpstan/phpstan#12434
Closes phpstan/phpstan#12533
Closes phpstan/phpstan#13394
Closes phpstan/phpstan#15431
Closes phpstan/phpstan#14797

@ondrejmirtes
ondrejmirtes force-pushed the array-unions-separate branch 4 times, most recently from ece17a4 to 5923213 Compare November 27, 2026 12:00
Signatures such as string[]|int[] meant an array of strings or ints, which
the union of general arrays merged into array<string|int>. Write that
array directly, so the signatures keep meaning the same once such unions
keep their members.

Amp-Thread-ID: https://ampcode.com/threads/T-01a214a7-48c4-741b-a5d4-b003e2d9a39d
union() no longer merges every general array with the other arrays into
one general array of all their keys and values. Arrays whose keys or
values are of kinds that share no value stay members of their own:
array<int>|array<string> holds no array of ints and strings, so it stays
a union. OverlappingArrayUnionRule merges each general array with the
arrays it cannot be told apart from, as array<int, string>|array{foo:
string} still becomes array<int|'foo', string>. Telling arrays apart only
by kinds keeps unions small: there are only so many kinds, so the members
cannot multiply level by level in nested arrays.

ShapeUnionRule leaves the general arrays alone, and GeneralArrayUnionRule
runs only in widen(), where values grow without bound.

Amp-Thread-ID: https://ampcode.com/threads/T-01a214a7-48c4-741b-a5d4-b003e2d9a39d
A join of more than GeneralArrayUnionRule::GENERAL_ARRAYS_LIMIT general
arrays told apart by their kinds merges the two whose kinds differ the
least, until it is within the limit, and the merged values widen too, so
the arrays nested in them are bounded the same way. Past the limit of
constant array values, the shapes fold as before but no longer merge into
the general arrays next to them.

Amp-Thread-ID: https://ampcode.com/threads/T-01a214a7-48c4-741b-a5d4-b003e2d9a39d
Without bleeding edge, union() and widen() merge every array with a
general one again, as before, through MergedGeneralArrayUnionRule.
Keeping them apart reports new errors in projects that rely on the
merged type, so it waits for the next major version.

Amp-Thread-ID: https://ampcode.com/threads/T-01a214a7-48c4-741b-a5d4-b003e2d9a39d
benchUnionGeneralArrayAndConstantArrays now keeps the shapes apart from
the general array with int keys, so it unions the shapes exactly instead
of absorbing them into one general array.

Amp-Thread-ID: https://ampcode.com/threads/T-01a214a7-48c4-741b-a5d4-b003e2d9a39d
@ondrejmirtes
ondrejmirtes merged commit 79cd194 into 2.3.x Nov 27, 2026
877 of 914 checks passed
@ondrejmirtes
ondrejmirtes deleted the array-unions-separate branch November 27, 2026 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment