Repository navigation
Reject keys-superset merge of constant arrays whose shared integer keys are in a different order unless both are non-lists - #7108
Conversation
…ys are in a different order unless both are non-lists
- `ConstantArrayType::legacyIsKeysSupersetOf()` now returns false when the
integer keys shared by both arrays appear in a different relative order,
unless both arrays are definitely not lists. `mergeWith()` keeps `$this`'s
key order and infers list-ness from it, so `[1 => 0, 0 => 0] | [0 => 1, 1 => 0]`
used to become the list `array{0|1, 0}` and `array_is_list()` was reported
as always true.
- String keys are not checked: their position does not affect list-ness, and
checking them stopped lossless optional-key collapsing (TypeCombinatorTest,
bug-13637).
- Ported the same check to the native mirror in turbo-ext/src/ConstantArrayType.cpp.
- Also checked: identical values in a different order, a superset with an extra
key, if/else scope merging and offset-by-offset building. All of them now keep
both shapes.
VincentLanglet
left a comment
There was a problem hiding this comment.
Compare your strategy with https://github.com/phpstan/phpstan-src/pull/7000/changes
… narrowing in bug-15679 test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I compared the two approaches. I kept this PR's approach and added one commit that brings in #7000's test scenarios. I did not port #7000's How the two approaches differ
This PR is more precise: the narrowing in each Why I didn't port the
|
Summary
Take two literal arrays with the same integer keys in a different order whose values differ at a single key, for example
$b ? [1 => 0, 0 => 0] : [0 => 1, 1 => 0]. Their union was merged into one shape in the list's key order (array{0|1, 0}). The non-list array was lost andarray_is_list()was reported as always true. Such arrays are no longer merged, so the union staysarray{1, 0}|array{1: 0, 0: 0}.Changes
src/Type/Constant/ConstantArrayType.php:legacyIsKeysSupersetOf()rejects the pair when the shared integer keys are in a different relative order, unless both arrays are definitely not lists.turbo-ext/src/ConstantArrayType.cpp: the same change in the native mirror. The extension build passes with the strict warning flags,tests/smoke.phppasses, and the full suite passes with the extension loaded.[1=>0,0=>0,2=>0] | [0=>0,1=>1]), if/else scope merging, and offset-by-offset building of the arrays.Root cause
TypeCombinator::reduceArrays()merges two constant arrays whenisKeysSupersetOf()holds. That check ignored key order.mergeWith()builds the result in$this's key order and infersisListfrom that shape, so the other array's (non-)list-ness was lost. List-ness depends only on the order of integer keys, so only those are checked. Where a string key sits does not affect list-ness, and checking it would stop lossless optional-key collapsing (TypeCombinatorTest::testSpecificUnionConstantArray). When both arrays are definitely not lists, the merge cannot get list-ness wrong, so it is still allowed (bug-13637). String-keyed arrays in a different order still merge, as they do for PHPDoc shapes (#11600, #12725).Test
tests/PHPStan/Analyser/nsrt/bug-15679.phpcontains the issue's two snippets. It checks both union types and thatarray_is_list()isbool. It fails without the fix, both with and without the turbo extension.Fixes phpstan/phpstan#15679
🤖 Generated with Claude Code