Keep unions of general array types separate#5506
Draft
ondrejmirtes wants to merge 2 commits into2.1.xfrom
Draft
Conversation
Previously `TypeCombinator::processArrayTypes` collapsed multiple general array members into a single `ArrayType` with unioned keys and values, so `array<int>|array<string>` was indistinguishable from `array<int|string>` (phpstan/phpstan#8963). Under `BleedingEdgeToggle::isBleedingEdge()` the members are now kept as distinct union branches; the outer `union()` loop handles subsumption via `compareTypesInUnion`. The `array{} | non-empty- array<X>` -> `array<X>` simplification is preserved by falling back to the old collapse path whenever an empty constant array is present.
Drops the BleedingEdgeToggle gate so CI exercises the new path on all configs. Revert before merging.
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.
Summary
TypeCombinator::processArrayTypesno longer collapses multiple general array members into a singleArrayTypewith unioned keys and values, soarray<int>|array<string>stays distinct fromarray<int|string>(fixes Union of arrays is not array of unions phpstan#8963). Subsumption (array<int>|array<int|string>→array<int|string>) is handled by the existing outerunion()loop viacompareTypesInUnion.array{} | non-empty-array<X>→array<X>simplification is preserved by falling back to the old collapse path whenever an empty constant array is present — that merge is lossless and not expressible as a keep-separate result.is_string($list[0])onlist<int>|list<string>narrows tolist<string>(withhasOffsetValue(0, string)).array<mixed~'foo'>|non-empty-array<mixed>where the old collapse was lossless; those need a follow-up heuristic before removing the WIP gate.TMP WIP commit (
8c3554c54c) runs the new path for everyone, not just bleeding edge, so CI exercises it. Revert before merge.Test plan
tests/PHPStan/Analyser/nsrt/array-union-keep-separate.phpadded — covers the issue repro, subsumption cases, narrowing via offset access, constant + general mixing, iterationNodeScopeResolverTestgreen except 10 known Bucket B filesLegacyNodeScopeResolverTest, coreTypeunit tests greenTypeCombinatorTesthas 18 describe-output failures under the WIP (no bleedingEdge); expected, to be re-baselinedarray<X>|non-empty-array<Y>) before removing WIP gate🤖 Generated with Claude Code