Skip to content

Add TemplateDeclarationTypeNode for "T of Foo" in generic arguments - #352

Merged
ondrejmirtes merged 1 commit into
2.5.xfrom
template-declaration-type-node
Nov 28, 2026
Merged

ondrejmirtes merged 1 commit into
2.5.xfrom
template-declaration-type-node

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

A generic type argument may now declare a template with an upper bound, like class-string-map<T of Foo, T> or array<K of key-of<Config>, Config[K]>. It is read into a new TemplateDeclarationTypeNode (name + bound) inside an ordinary GenericTypeNode.

This is an alternative to #346. Instead of a node specific to class-string-map, the parser reads the declaration in the arguments of every generic type and leaves which types give it a meaning to the tool resolving the types (in PHPStan, TypeNodeResolver). So array<T of Foo> parses, and PHPStan reports it where it means nothing.

  • T as Foo is read the same way and printed as T of Foo, like a @template tag.
  • The bound is required: class-string-map<T, V> and array<T, V> are read exactly as before, because the parser cannot tell a declaration from a class named T.
  • covariant of / contravariant of stay a variance followed by a type named of.
  • The grammar in doc/grammars/types.pp3 describes it, so GrammarSyncTest and FuzzyTest agree with the parser.

Refs #281

A generic type argument may now declare a template with an upper bound,
like class-string-map<T of Foo, T> or array<K of key-of<Config>, Config[K]>.
It is read into a TemplateDeclarationTypeNode holding the name and the
bound, for every generic type: which types give it a meaning is up to the
tool resolving the types. "T as Foo" is read the same way and printed
as "T of Foo", like a @template tag.

The bound is required, so class-string-map<T, V> and array<T, V> are
read as before.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22165-e448-7588-8b56-bc4dd19cc3a4
@ondrejmirtes
ondrejmirtes merged commit d1272b7 into 2.5.x Nov 28, 2026
65 of 68 checks passed
@ondrejmirtes
ondrejmirtes deleted the template-declaration-type-node branch November 28, 2026 13:21
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.

1 participant