Title: Read a merged array shape's list-ness in both key orders by gnutix · Pull Request #7000 · phpstan/phpstan-src · GitHub
Open Graph Title: Read a merged array shape's list-ness in both key orders by gnutix · Pull Request #7000 · phpstan/phpstan-src
X Title: Read a merged array shape's list-ness in both key orders by gnutix · Pull Request #7000 · phpstan/phpstan-src
Description: Closes phpstan/phpstan#15679 When two literal arrays with the same keys differ at a single key, TypeCombinator::union() merges them into one shape (ConstantArrayType::mergeWith()). The merged shape keeps $this's key order. To decide whether it is a list, mergeWith() ors the inputs' own list-ness with inferIsListFromShape(), which reads the merged keys in that order. When the other side has the same keys in another order, that reading only describes $this's arrays: [1 => 0, 0 => 0] merged into [0 => 1, 1 => 0] reads as keys 0, 1, so the union became a list (array_is_list() "will always evaluate to true"); the other way round, it reads as 1, 0, so the union became never a list ("will always evaluate to false"), and the else branch of if (array_is_list($x)) was *NEVER*. The fix reads the merged shape in the other side's order too: its keys first, then the rest. If both readings agree, nothing changes. If they disagree, the shape is maybe a list. When both inputs are lists, mergeWith() now skips the shape reading altogether, since or-ing it into yes cannot change anything. legacyMergeWith() (no bleeding edge) gets the same change. The union still merges into a single shape; this PR only fixes what that shape claims about list-ness. Keeping both shapes when the keys come in a different order would be more precise, but it would change which unions merge, well beyond list-ness (string keys included). That is the territory of phpstan/phpstan#11600, which this PR does not touch. Before / after Code Before After $x = $b ? [1 => 0, 0 => 0] : [0 => 1, 1 => 0]; array_is_list($x) true, "will always evaluate to true" bool $b ? [1 => 'a', 0 => 'b'] : [0 => 'c', 1 => 'a'] true, "will always evaluate to true" bool $x = $b ? [0 => 1, 1 => 0] : [1 => 0, 0 => 0]; array_is_list($x) false, "will always evaluate to false" bool same values: $b ? [1 => 0, 0 => 0] : [0 => 0, 1 => 0] true bool same values: $b ? [0 => 0, 1 => 0] : [1 => 0, 0 => 0] false bool else branch of if (array_is_list($x)), first row *NEVER* array{0|1, 0} values differing at two keys (not merged) bool bool (unchanged) same key order: $b ? [0 => 0, 1 => 0] : [0 => 1, 1 => 0] true true (unchanged) Tests: tests/PHPStan/Analyser/nsrt/bug-15679.php covers the rows above, and ConstantArrayTypeTest::testMergeWithRecomputesListnessOfSealedShape() gets five cases: both merge orders on both paths, and a key ahead of the shared one on the other side. Without the fix, six assertType() calls and all five unit cases fail. Turbo ConstantArrayType is shadowed, so turbo-ext/src/ConstantArrayType.cpp gets the same change (inferMergedIsListFromShape(), called from mergeWith() and legacyMergeWith()). Built with make build-turbo (GCC 15.3), no warnings. The checks below ran on 319b28d49; the branch was then rebased onto 48f825b2d, which only changes the benchmark tooling. turbo-ext/tests/smoke.php: ALL OK. turbo-ext/tests/signature-parity.php: OK. turbo-ext/bin/side-by-side.php: all methods paired. Full suite with the extension loaded and without it: 23,571 tests pass each way (2 deprecations and 89 skips, the same as on the base), PHP 8.5.12, 4 workers. Analysis output (--error-format=raw) byte-identical with the extension on and off: the self-analysis of src/ (build/phpstan.neon, 0 errors either way; user CPU 4m59s without, 2m58s with, so the extension was really active), and the issue's snippet plus bug-15679.php. 🤖 Generated with Claude Code (Opus 5.5)
Open Graph Description: Closes phpstan/phpstan#15679 When two literal arrays with the same keys differ at a single key, TypeCombinator::union() merges them into one shape (ConstantArrayType::mergeWith()). The merged shape...
X Description: Closes phpstan/phpstan#15679 When two literal arrays with the same keys differ at a single key, TypeCombinator::union() merges them into one shape (ConstantArrayType::mergeWith()). The merged shape...
Opengraph URL: https://github.com/phpstan/phpstan-src/pull/7000
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:592371ad-71b2-0b77-9f9c-73db04de92f2 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C858:1D865F:48D449:639420:6B007085 |
| html-safe-nonce | 6051ea575652d2fbe32ac6c25661eae77ba7564297cc9afb9521f4924598bf7d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDODU4OjFEODY1Rjo0OEQ0NDk6NjM5NDIwOjZCMDA3MDg1IiwidmlzaXRvcl9pZCI6IjYxMTgyOTQzNzc0OTI2MTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9d2196fbf900f4fb685d302f3b638a8bcfff3e611f25390573313d63f643f52c |
| hovercard-subject-tag | pull_request:5202124634 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/phpstan/phpstan-src/pull/7000/files |
| twitter:image | https://avatars.githubusercontent.com/u/310134?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/310134?s=400&v=4 |
| og:image:alt | Closes phpstan/phpstan#15679 When two literal arrays with the same keys differ at a single key, TypeCombinator::union() merges them into one shape (ConstantArrayType::mergeWith()). The merged shape... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6527e91c60c0b29d16606ff32ac5df8ecdc37a2a62205ea057a14d7762c870fa |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/phpstan/phpstan-src git https://github.com/phpstan/phpstan-src.git |
| octolytics-dimension-user_id | 16435710 |
| octolytics-dimension-user_login | phpstan |
| octolytics-dimension-repository_id | 219334214 |
| octolytics-dimension-repository_nwo | phpstan/phpstan-src |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 219334214 |
| octolytics-dimension-repository_network_root_nwo | phpstan/phpstan-src |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 46f1cd2ae3cd8ae1a81019b5d619742e76f13bbd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width