Skip to content

Reject keys-superset merge of constant arrays whose shared integer keys are in a different order unless both are non-lists - #7108

Open
phpstan-bot wants to merge 2 commits into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-qbjkw2l
Open

phpstan-bot wants to merge 2 commits into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-qbjkw2l

Conversation

@phpstan-bot

Copy link
Copy Markdown
Collaborator

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 and array_is_list() was reported as always true. Such arrays are no longer merged, so the union stays array{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.php passes, and the full suite passes with the extension loaded.
  • Checked and passing: identical values in a different order, a superset with an extra key ([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 when isKeysSupersetOf() holds. That check ignored key order. mergeWith() builds the result in $this's key order and infers isList from 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.php contains the issue's two snippets. It checks both union types and that array_is_list() is bool. It fails without the fix, both with and without the turbo extension.

Fixes phpstan/phpstan#15679

🤖 Generated with Claude Code

…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 VincentLanglet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

… narrowing in bug-15679 test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@phpstan-bot

Copy link
Copy Markdown
Collaborator Author

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 mergeWith() change.

How the two approaches differ

  • Read a merged array shape's list-ness in both key orders #7000 still merges the two arrays into one shape. Inside mergeWith() / legacyMergeWith() it reads that shape's list-ness in the other array's key order too, and gives maybe when the two readings disagree. The union stays array{0|1, 0}, but array_is_list() is now bool.
  • This PR doesn't merge them. legacyIsKeysSupersetOf() rejects the pair when the shared integer keys are in a different order, unless both arrays are definitely not lists. The union stays array{1, 0}|array{1: 0, 0: 0}.
Case #7000 This PR
[1=>0,0=>0] | [0=>1,1=>0] array{0|1, 0}, bool array{1, 0}|array{1: 0, 0: 0}, bool
Same pair, list first array{1: 0, 0: 0|1}, bool array{1, 0}|array{1: 0, 0: 0}, bool
Equal values, different order array{0, 0} or array{1: 0, 0: 0}, bool array{0, 0}|array{1: 0, 0: 0}, bool
if (array_is_list($x)) branches array{0|1, 0} in both array{1, 0} / array{1: 0, 0: 0}
Same key order unchanged (true / false) unchanged (true / false)
Values differ at two keys not merged not merged

This PR is more precise: the narrowing in each array_is_list() branch is exact. #7000 keeps one shape, so its claim about the values no longer matches its list-ness. #7000's own description says keeping both shapes "would be more precise" but held back, to avoid changing which unions merge. This PR makes that change only for differently ordered integer keys. String-keyed arrays merge as before (#11600, #12725), and so do pairs where both arrays are definitely not lists (bug-13637).

Why I didn't port the mergeWith() change

#7000's change would also cover callers that call mergeWith() without checking isKeysSupersetOf() first. I checked those in TypeCombinator:

  • Single-key merges: with one key there is no order to get wrong.
  • mergeConstantArraysWithSameKeys(): it groups arrays by a signature that includes key order, so the order always matches.
  • The !$preserveTaggedUnions branch: it only runs when a general array is also in the union. I checked it, and the result is generalized to array<int, int>, so list-ness is bool anyway.

So no path I found reaches mergeWith() with a wrong order. Adding #7000's re-reading on top would mean a second check in PHP and in the C++ mirror that isn't needed. I can add it if you'd like that extra safety.

Change

Commit a3cdde2ae (test only) extends tests/PHPStan/Analyser/nsrt/bug-15679.php with these cases:

  • the list-first order;
  • equal values in both orders;
  • values that differ at two keys;
  • same key order, both as a list and not as a list;
  • narrowing in both array_is_list() branches;
  • arrays built offset by offset in if/else.

With the pre-fix ConstantArrayType.php, 10 assertions fail, including *NEVER* in the else branch. With the fix the test passes. make tests passes (23,927 tests, 116 skipped) and make phpstan reports no errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Union of two literal arrays with the same keys in a different order becomes a list

2 participants