Repository navigation
Keep unions of general arrays told apart by their kinds separate - #7211
Merged
Merged
Conversation
ondrejmirtes
force-pushed
the
array-unions-separate
branch
4 times, most recently
from
November 27, 2026 12:00
ece17a4 to
5923213
Compare
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
force-pushed
the
array-unions-separate
branch
from
November 27, 2026 12:57
5923213 to
cad54b1
Compare
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.
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 stillarray<int|'foo', string>.This is the policy #5506 proposed, built on the union rules from #7195:
OverlappingArrayUnionRule(new, inunion()) merges each general array with the arrays it cannot be told apart from byTypeKind, 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(inwiden()only) bounds how many general arrays a join keeps apart. PastGENERAL_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.MissingTypehintCheckchecks 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()andwiden()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 ofarray<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