Repository navigation
Keep single-key shapes with disjoint values apart in a union - #7207
Merged
Merged
Conversation
0e9fdb5 merged shapes differing in the value at one key, so array{'title'}|array{'body'} became array{'body'|'title'}. That holds the same arrays, but functions and extensions that turn values into keys can no longer tell which value went with which shape: array_fill_keys() and array_flip() returned non-empty-array{body?: 1, title?: 1}. Two single-key shapes now stay a tagged union when their values are disjoint and only a union holds both, as before 0e9fdb5. Values that overlap or merge into one type still merge, and shapes with more keys are unchanged. bug-11858 gets its less precise type back: {A} now merges with {A, B} into array{0: 'A', 1?: 'B'} first, and appending to a list with optional keys is not exact. Amp-Thread-ID: https://ampcode.com/threads/T-01a21c4b-3106-74b8-add3-927f10530c87
ondrejmirtes
force-pushed
the
keep-disjoint-single-key-shapes-apart
branch
from
November 27, 2026 09:47
a817b44 to
2b1abef
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.
Since 0e9fdb5,
array{'title'}|array{'body'}is merged intoarray{'body'|'title'}. That describes the same arrays, but functions and extensions that turn values into keys lose which value belonged to which shape:array_fill_keys($keys, 1)becamenon-empty-array{body?: 1, title?: 1}, and phpstan-laravel'sFormRequest::safe()and phpstan-dba'spdo-union-result.phpchanged in the integration tests.ArrayAlgebra::tryUnionConstantArray()now keeps two single-key shapes apart when their values are disjoint and only a union holds both, the rule from before 0e9fdb5. Overlapping values and values that merge into one type (int<0, 5>|int<6, 10>) still merge; shapes with more keys are unchanged. The updated test expectations go back to their values before 0e9fdb5.Known trade-off: bug-11858 gets its less precise type back, because
{A}now merges with{A, B}intoarray{0: 'A', 1?: 'B'}and appending to a list with optional keys is not exact.Closes phpstan/phpstan#15864
Amp thread: https://ampcode.com/threads/T-01a21c4b-3106-74b8-add3-927f10530c87