Skip to content

Support type arguments on template types bound by classes with consistent templates - #7258

Open
ondrejmirtes wants to merge 7 commits into
2.3.xfrom
template-type-constructors
Open

ondrejmirtes wants to merge 7 commits into
2.3.xfrom
template-type-constructors

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Type arguments on a template type, like Scala's or Haskell's higher-kinded types, limited to classes that declare @phpstan-consistent-templates.

/**
 * @template TSeq of \Ds\Sequence<int>
 * @param TSeq $seq
 * @return TSeq<string>
 */
function stringify(\Ds\Sequence $seq): \Ds\Sequence
{
	return $seq->map(static fn (int $i): string => (string) $i);
}

stringify(new \Ds\Vector([1, 2])); // Ds\Vector<string>

Commits, one change each:

  1. Keep static<…> on the class when a subclass cannot hold its type arguments. static<U> resolved on a subclass that does not forward its templates no longer produces a wrong generic object for the subclass. Type arguments it leaves out come from the ancestor's active template map. Ported to turbo-ext.

  2. Add @phpstan-consistent-templates (also reads @psalm-consistent-templates). Every class below a class or interface with the tag has to declare the same template types and pass them on in the same order, so a class together with other type arguments is still a type. ConsistentTemplatesRule checks this behind bleeding edge (generics.inconsistentTemplates).

  3. Support type arguments on a template type bound by a class with consistent templates. TColl<X> is a new late-resolvable TemplateTypeApplication, which resolves to the class TColl is inferred as, carrying X. Without the tag, bleeding edge reports it (generics.inconsistentTemplates); without bleeding edge it resolves to the bound with the arguments, as before. Ported to turbo-ext.

  4. Make static<…> called on a template type of a consistent class an application of that template type. $coll->map(…) on a TColl with @return static<U> gives TColl<U>, so the caller gets back the class it passed in. Ported to turbo-ext.

  5. Return static<…> from Ds\Sequence methods. Sequence gets the tag. map, merge, filter, reversed, slice and sorted return static/static<…>. Vector and Deque keep their own declarations. ext-ds v2 has only final classes, so it is unchanged.

  6. Look up the members of a generic object on its class without type arguments. GenericStaticType::getStaticObjectType() gave the object a class reflection with the type arguments already applied, so a method's conditional return type resolved too early and merged with the native type: $c->sync()->objects() with @return static<false> and @return (T is true ? P : R) gave P|R instead of R. This also happens on 2.3.x without the rest of this PR. Ported to turbo-ext.

  7. Type new $class(…) on a class-string<K> as K<…> when the class K is bound by has consistent templates, with the arguments inferred from the constructor like those of new Foo(…). A K<T> is a K. Ported to turbo-ext.

Closes phpstan/phpstan#4971
Closes phpstan/phpstan#12646

Both need @phpstan-consistent-templates on the bound class (IFoo, Communicator); with it, their reproducers have no errors.

Amp thread: https://ampcode.com/threads/T-01a22307-fb39-7133-a55b-a5cc3cda81e7

…ments

`static<U>` called on a subclass becomes the subclass with the type
arguments passed on to its own templates, but a subclass that does not
pass them, like `IntVec implements Coll<int>`, holds only ints whatever U
is: `$intVec->map(fn (int $i): string => …)` was IntVec. Such a type now
stays `Coll<string>`, which is all that is known. The class's own
templates stand for what the subclass passes to them, so `static<T>` on
IntVec is still IntVec, while `static<V, K>` on
`IntStringPair extends Pair<int, string>` is `Pair<string, int>`.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22307-fb39-7133-a55b-a5cc3cda81e7
A class or an interface with @phpstan-consistent-templates, or
@psalm-consistent-templates, makes the classes below it declare the same
template types, pass them on in the order they declare them and bound
them at most as tightly, so that the class of an object with other type
arguments is a type. The rule checking them reports
generics.inconsistentTemplates under bleeding edge
(checkConsistentTemplates).

Amp-Thread-ID: https://ampcode.com/threads/T-01a22307-fb39-7133-a55b-a5cc3cda81e7
…stent templates

`@template TColl of Coll` with `@param TColl<int> $c` and
`@return TColl<string>` is the class TColl is with other type arguments
when Coll has @phpstan-consistent-templates: called with a Vec<int>, the
function returns a Vec<string>. The template is inferred from the class of
the argument, together with the templates in the type arguments, and the
type arguments are carried down to that class the way `static<…>` does.
Until TColl is known, `TColl<string>` is a Coll<string> that only another
`TColl<…>` is known to be, so returning any Coll<string> is reported.

Without the tag, the type stays the bound with the type arguments, which
bleeding edge reports (generics.inconsistentTemplates).

Ported to turbo-ext.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22307-fb39-7133-a55b-a5cc3cda81e7
…uments

GenericStaticType::getStaticObjectType() gives the GenericObjectType a class
reflection that already carries the type arguments. Methods found on it had
their template types resolved before the call, so a conditional return type
merged with the native one too early: (false is true ? P : R)|P.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22307-fb39-7133-a55b-a5cc3cda81e7
…ass with consistent templates as an application of that template type

new $class($v) with class-string<K> $class, K of IFoo and IFoo<T> with
@phpstan-consistent-templates is K<T>, its arguments inferred from the
constructor like those of new Foo($v). A K<T> is a K.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22307-fb39-7133-a55b-a5cc3cda81e7
@ondrejmirtes
ondrejmirtes force-pushed the template-type-constructors branch from 0a76b1a to 84cc9a4 Compare December 2, 2026 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow template type as mainType in GenericObjectType When combining templated class-string and Generics, the concrete class is missing.

1 participant