Skip to content

@return static<...> on a template-typed receiver collapses to the template and drops the new type arguments #13997

Done

Description

@alies-dev

https://psalm.dev/r/4deffd8a2e

What Psalm reports / does

Base::rebuild() is declared @return static<NewK, NewV>.

  • On a concrete receiver Base<int, string>, rebuild('x', 1.5) is Base<'x', float(1.5)>&static, as expected.
  • On a receiver typed as a template C of Base<int, string>, the same call is Trace: $r: C:fn-templated as Base<int, string>. static is replaced by C and the new type arguments are dropped.

As a result, returnsTheTemplate() returns Base<'x', float> as @return C and Psalm reports nothing.

What you expected

On a template receiver, static<NewK, NewV> should keep the new type arguments, for example Base<'x', float(1.5)> (the template's bound class with the substituted arguments). Then returnsTheTemplate() reports InvalidReturnStatement. Collapsing to C is only correct when the arguments equal the receiver's own.

Psalm version, PHP version

psalm.dev dev-master@e141f40d4, PHP 8.3; also Psalm 7.7.0 and local dev-master, PHP 8.5.

Related

#13988 is the same static<...> family with a different trigger: there the arguments are applied by position to a subclass with a different template list.

Impact

psalm/psalm-plugin-laravel works around it for Laravel's Collection::groupBy() / keyBy() (@return static<...> in Laravel's docblocks): psalm/psalm-plugin-laravel#2332. Without that, a wrong @return C on a template-typed collection goes unreported.

Activity

  1. psalm-github-bot commented on Nov 16, 2026

    @psalm-github-bot

    I found these snippets:

    https://psalm.dev/r/4deffd8a2e
    <?php
    
    /**
     * @template K of array-key
     * @template V
     */
    abstract class Base
    {
        /**
         * @template NewK of array-key
         * @template NewV
         * @param NewK $k
         * @param NewV $v
         * @return static<NewK, NewV>
         */
        abstract public function rebuild(mixed $k, mixed $v): static;
    }
    
    /**
     * @template C of Base<int, string>
     * @param C $b
     * @return C
     */
    function returnsTheTemplate(Base $b): Base
    {
        return $b->rebuild('x', 1.5); // no issue reported
    }
    
    /** @param Base<int, string> $b */
    function concrete(Base $b): void
    {
        $r = $b->rebuild('x', 1.5);
        /** @psalm-trace $r */
    }
    
    /**
     * @template C of Base<int, string>
     * @param C $b
     */
    function templated(Base $b): void
    {
        $r = $b->rebuild('x', 1.5);
        /** @psalm-trace $r */
    }
    Psalm output (using commit e141f40):
    
    INFO: Trace - 33:0 - $r: Base<'x', float(1.5)>&static
    
    INFO: Trace - 43:0 - $r: C:fn-templated as Base<int, string>
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions