Repository navigation
Support type arguments on template types bound by classes with consistent templates - #7258
Open
ondrejmirtes wants to merge 7 commits into
Open
ondrejmirtes wants to merge 7 commits into
ondrejmirtes wants to merge 7 commits into
Conversation
ondrejmirtes
force-pushed
the
template-type-constructors
branch
from
November 30, 2026 08:21
c18412e to
1482996
Compare
ondrejmirtes
force-pushed
the
template-type-constructors
branch
2 times, most recently
from
November 30, 2026 10:12
78ebe8b to
0a76b1a
Compare
…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
…lication of that template type 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
force-pushed
the
template-type-constructors
branch
from
December 2, 2026 13:08
0a76b1a to
84cc9a4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type arguments on a template type, like Scala's or Haskell's higher-kinded types, limited to classes that declare
@phpstan-consistent-templates.Commits, one change each:
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.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.ConsistentTemplatesRulechecks this behind bleeding edge (generics.inconsistentTemplates).Support type arguments on a template type bound by a class with consistent templates.
TColl<X>is a new late-resolvableTemplateTypeApplication, which resolves to the classTCollis inferred as, carryingX. 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.Make
static<…>called on a template type of a consistent class an application of that template type.$coll->map(…)on aTCollwith@return static<U>givesTColl<U>, so the caller gets back the class it passed in. Ported to turbo-ext.Return
static<…>fromDs\Sequencemethods.Sequencegets the tag.map,merge,filter,reversed,sliceandsortedreturnstatic/static<…>.VectorandDequekeep their own declarations. ext-ds v2 has only final classes, so it is unchanged.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)gaveP|Rinstead ofR. This also happens on 2.3.x without the rest of this PR. Ported to turbo-ext.Type
new $class(…)on aclass-string<K>asK<…>when the classKis bound by has consistent templates, with the arguments inferred from the constructor like those ofnew Foo(…). AK<T>is aK. Ported to turbo-ext.Closes phpstan/phpstan#4971
Closes phpstan/phpstan#12646
Both need
@phpstan-consistent-templateson the bound class (IFoo,Communicator); with it, their reproducers have no errors.Amp thread: https://ampcode.com/threads/T-01a22307-fb39-7133-a55b-a5cc3cda81e7