Skip to content

Fix: map static<...> return arguments through @extends - #14042

Open
alies-dev wants to merge 5 commits into
vimeo:masterfrom
alies-dev:alies-dev/return-static-.-arguments-are-applied-by-positio
Open

alies-dev wants to merge 5 commits into
vimeo:masterfrom
alies-dev:alies-dev/return-static-.-arguments-are-applied-by-positio

Conversation

@alies-dev

Copy link
Copy Markdown
Collaborator

Arguments of @return static<...> are applied by position to the called subclass instead of being mapped through its @extends.

Before

<?php
/**
 * @template TA
 * @template TB
 */
class Base {
    /** @return static<TA, int> */
    public function withIntB() {
        throw new \Exception();
    }
}

/**
 * @template TX
 * @template TY
 * @extends Base<TY, TX>
 */
class Swapped extends Base {}

/** @var Swapped<bool, string> $s */
$s = new Swapped();
$r = $s->withIntB(); // Swapped<string, int>&static

After

$r = $s->withIntB(); // Swapped<int, string>&static

Fixes #13988

- Arguments of `@return static<...>` belong to the declaring class's templates. Map them
  onto the templates of the class an instance method is called on (using the receiver's
  arguments for unmapped ones) and of an override's inherited declared return, through
  `template_extended_params`, instead of applying them by position.
@alies-dev alies-dev added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 16, 2026
@alies-dev alies-dev self-assigned this Nov 16, 2026
…tly vimeo#13988

- Project from the trait that declares the return type (its templates are the ones the
  `static<...>` arguments refer to, via the `@use` entries in template_extended_params),
  not from the class using it; also used for the overridden method in Methods.
- Follow an `@extends` mapping only through bare template arguments, so `Y&I` is not Y.
@alies-dev
alies-dev marked this pull request as ready for review November 16, 2026 23:44

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 8c16876. Configure here.

$projected[] = count($sources[$template_name] ?? []) === 1
? $sources[$template_name][0]
: $this->called_params[count($projected)]
?? new Union([new TTemplateParam($template_name, reset($type_map), $this->called_storage->name)]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Null called_params index access

Medium Severity

When project() cannot map a called-class template from a single @extends source, it indexes $this->called_params before the null coalescing fallback. Methods.php invokes projectType() without called_params, so PHP raises a TypeError during override return-type comparison for subclasses that declare extra templates.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 8c16876. Configure here.

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 765 71.2 $\color{gray}{\textsf{-0.1\%}}$ 25 932 53 272 578
✅ symfony 3 622 202.2 $\color{gray}{\textsf{-2.8\%}}$ 50 626 98 643 1 438
✅ doctrine-orm 393 15.7 $\color{gray}{\textsf{+0.4\%}}$ 3 480 6 164 0
✅ psl 632 19.0 $\color{gray}{\textsf{-0.1\%}}$ 1 305 5 461 1
✅ phpunit 471 15.8 $\color{gray}{\textsf{-1.3\%}}$ 1 742 3 452 17
✅ php-parser 236 10.0 $\color{gray}{\textsf{-0.3\%}}$ 17 133 21 035 0
✅ tempest 899 28.2 $\color{gray}{\textsf{-1.2\%}}$ 5 024 10 517 221
✅ amphp-amp 61 1.7 $\color{gray}{\textsf{-0.6\%}}$ 29 49 0
✅ webmozarts-assert 285 4.0 $\color{gray}{\textsf{+0.2\%}}$ 577 2 722 0
✅ guzzle-promises 55 1.8 $\color{gray}{\textsf{+1.1\%}}$ 158 435 2
✅ monolog 153 4.7 $\color{gray}{\textsf{-0.2\%}}$ 904 2 409 10
✅ drupal 4 819 179.8 $\color{gray}{\textsf{+0.2\%}}$ 97 556 169 424 508
Total 13 392 554.1 $\color{gray}{\textsf{-1.1\%}}$ 204 466 373 583 2 775
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 73e0b4b · base f836e87 (merge-base) · workflow run

@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.

Checked on b708973: the 10 cases asserting the fix fail on the base and pass here, the 3 guards pass on both, CI is green and the perf report shows no issue change.

Scratch probes beyond the tests also give the right types here and wrong ones on the base:

  • an implemented interface (@implements I<TY, TX>) and an interface extending an interface;
  • a multi-level abstract chain with a binding (Leaf2<int>);
  • a flipped static<TB, TA>;
  • an override in a three-level chain;
  • $this inside the subclass.

@extends Base<T, T> falls back to the receiver's argument, which is fine.

Bugbot's "TypeError on null called_params" is a false positive: null[$i] ?? ... doesn't throw.

One gap remains, out of this PR's scope (cluster of #14043): a first-class callable $x->m(...) still gives SameSw<mixed, mixed>, on the base as well.

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.

@return static<...> arguments are applied by position to the called subclass instead of projected through @extends

2 participants