Repository navigation
Keep the shapes of loop-built nested arrays in exact unions and widening - #7225
Merged
Merged
Conversation
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
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.
Closes phpstan/phpstan#15860
Since 0e9fdb5 (exact shape unions), arrays built in a loop that appends to a different key in each branch became
mixedor 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:
tryUnionConstantArray()took a shape thatisSuperTypeOf()the other as their union. For shapes without bleeding edge,isSuperTypeOf()lets a shape hold one with more keys, so at a key holding shapesarray{}|array{string}heldarray{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 variablemixed. That is what happens in openemr'sgetFormData(). 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.ArrayWideningfolded 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>(theelsebranch makes every iteration append somewhere). openemr'sgetFormData()infers the same type as before 0e9fdb5.nsrt/bug-15860.php(the issue snippet, two variants and openemr'sgetFormData()) also runs inNoBleedingEdgeTypeInferenceTest, because the bug in (1) only shows without bleeding edge, where shapes are not sealed.