Skip to content

Let callbacks see values added to a type variable after its construction - #14551

Merged
orklah merged 1 commit into
vimeo:masterfrom
shu-chen-dot-ca:fix-13841-array-reduce-widened-template
Nov 23, 2026
Merged

orklah merged 1 commit into
vimeo:masterfrom
shu-chen-dot-ca:fix-13841-array-reduce-widened-template

Conversation

@shu-chen-dot-ca

Copy link
Copy Markdown
Contributor

A callback over the elements of a widened class type variable (new Coll([1]), then $c->add("x")) only sees the construction-site bound, so folding or mapping the list reports IncompatibleTypeParameters and drops the added values.

Before

<?php
/** @template T */
final class Coll {
    /** @param list<T> $items */
    public function __construct(private array $items) {}

    /** @param T $item */
    public function add($item): void { $this->items[] = $item; }

    /** @return list<T> */
    public function all(): array { return $this->items; }
}

$c = new Coll([1]);
$c->add("x");
$last = array_reduce($c->all(), fn($carry, $i) => $i, null); // IncompatibleTypeParameters: Type 'x' should be a subtype of 1
$mapped = array_map(fn($i) => $i, $c->all());
// $last: 1|null, $mapped: list<1>

After

$last = array_reduce($c->all(), fn($carry, $i) => $i, null); // no issue
// $last: 'x'|1|null, $mapped: list<'x'|1>

Untyped closure params and the template gate now resolve the variable through every value that reached it, as read sites already do since 7cc951c. A typed callable param is left live only when the callback takes the construction-site values but none of the later ones, so each too-narrow callback is reported once:

  • new Coll([new A()]); $c->add(new B()); $c->each(function (A $a): void {}); stays one Type B should be a subtype of A at the method name, as on master;
  • new Coll([1]); $c->add("x"); $c->each(function (int $i): void {}); is now Type 'x' should be a subtype of int at the callback argument (col 14) instead of ... of 1 at the method name (col 9);
  • a callback that takes none of the construction-site values stays an InvalidArgument, and one that takes every value (object, int|string) is no longer reported.

HHVM agrees (two new type_variables fixtures), and the type-variable rule in the hack-conformance skill and docs/annotating_code/type_variables.md are updated to match.

Ran locally: lint, phpcs, self-analysis, HackConformanceTest and the PHPUnit suite, without 4 tests that fail on unchanged master in my container too (testGeneratorRejectsMissingRequiredExtensionBeforeWriting, taintPropertyPassingObjectWithDifferentValue x2, the str_contains/__DIR__ case).

Before After
before after

Fixes #13841

… variable

Untyped closure params, callable params and the template gate resolved a
type variable through its construction-site bounds only, so after
`$c = new Coll([1]); $c->add("x");` folding `$c->all()` with
array_reduce reported IncompatibleTypeParameters and lost 'x'.

Fixes #13841
@github-actions

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 751 71.5 $\color{gray}{\textsf{+0.7\%}}$ 25 908 53 462 682
✅ symfony 3 600 170.6 $\color{gray}{\textsf{-0.6\%}}$ 50 611 98 405 1 528
✅ doctrine-orm 381 13.1 $\color{gray}{\textsf{-0.5\%}}$ 3 481 6 143 0
✅ psl 617 16.2 $\color{gray}{\textsf{+0.2\%}}$ 1 375 5 615 1
✅ phpunit 455 19.6 $\color{gray}{\textsf{+0.6\%}}$ 1 731 3 410 18
✅ php-parser 226 13.3 $\color{gray}{\textsf{+0.7\%}}$ 17 132 21 025 0
✅ tempest 895 35.8 $\color{gray}{\textsf{+0.4\%}}$ 5 047 10 528 252
✅ amphp-amp 68 1.9 $\color{gray}{\textsf{+1.1\%}}$ 29 49 0
✅ webmozarts-assert 273 4.6 $\color{gray}{\textsf{-1.1\%}}$ 577 2 720 0
✅ guzzle-promises 44 1.6 155 434 2
✅ monolog 135 5.6 $\color{gray}{\textsf{-0.7\%}}$ 913 2 479 10
✅ drupal 4 841 243.3 $\color{gray}{\textsf{+1.3\%}}$ 97 591 169 293 583
Total 13 286 597.1 $\color{gray}{\textsf{+0.5\%}}$ 204 550 373 563 3 076
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 +20 % 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. Thresholds apply per project, not to the total.
  • 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.

head 4758ff3 · base e63cd9f (merge-base) · workflow run

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

orklah commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Thanks!

@orklah
orklah merged commit 3deccba into vimeo:master Nov 23, 2026
72 of 73 checks passed
@shu-chen-dot-ca
shu-chen-dot-ca deleted the fix-13841-array-reduce-widened-template branch November 23, 2026 21:09
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.

array_reduce over a list from a widened class type variable reports IncompatibleTypeParameters

2 participants