Repository navigation
fix(arrays): appends stop growing a list shape at maxShapedArraySize - #14641
Conversation
…imeo#9781 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Behaviour change
|
✅ Performance: unchanged · Types: unchanged
How to read this table
✅ drupal: issues +2 −2Memory 4 844 → 4 844 MB (+0.0 %) · Wall 245.4 → 241.2 s (−1.7 %)
Changed in place (2) @@ 2 places MixedReturnTypeCoercion @@
core/modules/update/src/ProjectCoreCompatibility.php:228
core/modules/update/src/ProjectCoreCompatibility.php:264
- …specific than the inferred return type 'list<non-empty-list<string>|non-empty-mixed>'
+ …specific than the inferred return type 'list<list{0: string, 1?: string, ...<string>}|non-empty-mixed>'✅ wordpress: issues +1 −1Memory 2 042 → 2 042 MB (+0.0 %) · Wall 178.7 → 182.3 s (+2.0 %)
Added (1)
Removed (1)
head 511770d · base 71a650b (merge-base) · workflow run |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # tests/ArrayAssignmentTest.php
# Conflicts: # tests/ArrayAssignmentTest.php
orklah
left a comment
There was a problem hiding this comment.
Thanks! Capping appends at maxShapedArraySize is the right fix for #9781, and the test reproduces it (it fails on master, passes here).
One problem before merging: once a list is capped it has a rest type, and a write under a literal key past its items then turns the whole list into an array with every key optional. Code that is clean on master gets four new issues:
<?php
/** @param list<int> $l */
function takesList(array $l): void {}
function h1(): int {
$a = [];
$a[] = 1;
// ... 101 appends in all
$a[] = 1;
$a[100] = 0; // master: list{1, ..., 0}; this PR: array{0?: 1, 1?: 1, ..., 100?: 0|1, ...<int<0, max>, 1>}
takesList($a); // ArgumentTypeCoercion
return $a[0]; // PossiblyUndefinedArrayOffset, NullableReturnStatement
}The cause is older than this PR and shows on a small list too:
/** @param list{int, int, ...<int>} $a */
function s1(array $a): int {
$a[2] = 5; // array{0?: int, 1?: int, 2?: int, ...<int<0, max>, int>}
return $a[0]; // PossiblyUndefinedArrayOffset
}ArrayAssignmentAnalyzer::updateTypeWithKeyValues() combines the shape with array{2: ...} when the key isn't one of its items, which makes every key optional and drops list. Writing the key into a shape that has a rest type (keeping its items, and the list when the key is the next one) would fix both. Could you fix that here, with a test for each snippet above? A rebase will also rerun CI on a master where the unrelated failures (StubTest, DocumentationTest, ConfigTest, Config.php) are fixed.
…ms, and a list stays one under the next key vimeo#9781 Writing `$a[2] = 5` into `list{int, int, ...<int>}` combined the shape with `array{2: 5}`, which made every key optional and dropped the list: after the list was capped at maxShapedArraySize, a write under `$a[100]` gave a possibly empty array with `0?`. The key is now written into the shape, which keeps its items and rest type, and stays a list for the next key. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
@orklah, done: a write under a key the shape doesn't list, where the shape has a rest type, now writes the key into the shape instead of combining it with
|
Appending to a list shape (
$a[] = ...) ignoresmaxShapedArraySize: every append adds an item to the shape, so a long run of appends builds a shape of any size.Before
After
Fixes #9781