Skip to content

fix(arrays): appends stop growing a list shape at maxShapedArraySize - #14641

Merged
orklah merged 7 commits into
vimeo:masterfrom
jirkace:fix/9781-list-append-max-shape-size
Nov 23, 2026
Merged

orklah merged 7 commits into
vimeo:masterfrom
jirkace:fix/9781-list-append-max-shape-size

Conversation

@jirkace

@jirkace jirkace commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Appending to a list shape ($a[] = ...) ignores maxShapedArraySize: every append adds an item to the shape, so a long run of appends builds a shape of any size.

Before

<?php
$a = [];
$a[] = "x";
// ... 150 appends in all
$a[] = "x"; // $a: list{'x', 'x', ... 150 items}

After

$a[] = "x"; // $a: list{'x', 'x', ... 100 items, ...<'x'>}

Fixes #9781

…imeo#9781

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

jirkace commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • An append to a sealed list shape that already has maxShapedArraySize items (100 by default) no longer adds an item to the shape: the list keeps those items and gets the appended value as its rest (list{..., ...<'x'>}), as an append to a list with a rest type already did.
  • Lists shorter than the limit and keyed writes are unchanged.

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 772 73.7 $\color{gray}{\textsf{+0.5\%}}$ 25 784 53 058 743 0
✅ symfony 3 619 260.9 $\color{gray}{\textsf{+0.5\%}}$ 51 755 100 583 1 582 0
✅ doctrine-orm 380 14.7 $\color{gray}{\textsf{+2.0\%}}$ 3 458 6 215 0 0
✅ psl 618 18.0 $\color{gray}{\textsf{+0.8\%}}$ 1 390 5 613 1 0
✅ phpunit 456 19.9 $\color{gray}{\textsf{+0.2\%}}$ 1 735 3 418 18 0
✅ php-parser 226 13.5 $\color{gray}{\textsf{+0.1\%}}$ 17 116 21 187 0 0
✅ tempest 898 36.0 $\color{gray}{\textsf{-0.7\%}}$ 5 093 10 567 275 0
✅ amphp-amp 48 1.2 $\color{gray}{\textsf{+1.7\%}}$ 29 49 0 0
✅ webmozarts-assert 273 3.5 $\color{gray}{\textsf{-0.3\%}}$ 575 2 714 0 0
✅ guzzle-promises 46 1.3 156 436 2 0
✅ monolog 136 4.3 $\color{gray}{\textsf{+1.4\%}}$ 918 2 491 11 0
✅ drupal 4 844 241.2 $\color{gray}{\textsf{-1.7\%}}$ 97 854 $\color{red}{\textsf{+2}}$ $\color{green}{\textsf{-2}}$ 169 770 575 0
✅ wordpress 2 042 182.3 $\color{gray}{\textsf{+2.0\%}}$ 143 516 $\color{red}{\textsf{+1}}$ $\color{green}{\textsf{-1}}$ 678 190 461 1
Total 15 358 870.5 $\color{gray}{\textsf{+0.2\%}}$ 349 379 $\color{red}{\textsf{+3}}$ $\color{green}{\textsf{-3}}$ 1 054 291 3 668 1
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run on the same runner; ±10 % is noise. Above +10 % warns, never fails. re-run: a reversed second run was taken and the faster kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Its wall time warns above +5 % (±5 % is noise); the other thresholds apply per project.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.
✅ drupal: issues +2 −2

Memory 4 844 → 4 844 MB (+0.0 %) · Wall 245.4 → 241.2 s (−1.7 %)

Issue type added removed
MixedReturnTypeCoercion 2 2

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 −1

Memory 2 042 → 2 042 MB (+0.0 %) · Wall 178.7 → 182.3 s (+2.0 %)

Issue type added removed
PossiblyUndefinedArrayOffset 1 1

Added (1)

  • src/wp-includes/ID3/module.audio.mp3.php:335 PossiblyUndefinedArrayOffset (error): Possibly undefined array key $ExplodedOptions[1] on list{0: string, 1?: string, ...<string>}

Removed (1)

  • src/wp-includes/functions.php:6135 PossiblyUndefinedArrayOffset (error): Possibly undefined array key $server_parts[0] on array{0?: string, 1?: string, ...<int<0, max>, string>}

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 orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

jirkace and others added 3 commits November 23, 2026 14:48
…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>
@jirkace

jirkace commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@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 array{key: v}. The items and the rest type stay, and a list stays a list when the key is the next one (ArrayAssignmentAnalyzer::updateTypeWithKeyValues(), a single literal key; a union of literal keys is unchanged).

  • list{int, int, ...<int>} with $a[2] = 5 is list{int, int, 5, ...<int>}, and with $a[5] = 5 it is array{0: int, 1: int, 5: 5, ...<int<0, max>, int>}; $a[1] = 5 was already list{int, 5, ...<int>}.
  • Tests: ArrayAssignmentTest::testValidCode@writeUnderAKeyOfTheRestOfAShapeKeepsItsItems (the three writes above) and @writeUnderTheNextKeyOfACappedListKeepsItAList (your 101 appends, then $a[100] = 0, takesList($a) and $a[0]: no issue). Both fail on the previous head, the second with ArgumentTypeCoercion and a shape with every key optional.
  • Master is merged in. Self-analysis shows only the two local PoolEventLoopDriverTest MixedArgument errors, phpcs is clean. Not run: the full suite (CI runs it).

@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 23, 2026

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @jirkace!

@orklah
orklah merged commit c1364a8 into vimeo:master Nov 23, 2026
72 of 73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix The PR will be included in 'Fixes' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

maxShapedArraySize not working for list - Internal Psalm error on line 0 - Code too long

2 participants