From 637cc2c4f2ae0dbfe8b661fc1cdf4dcbdf077ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Mirtes?= Date: Sat, 28 Nov 2026 13:40:18 +0000 Subject: [PATCH] Read a template declared by the key of an array shape's unsealed type array{a: int, ...|int, Config[K]>} declares K in the key of whatever the shape leaves over, the way array does. A declared template is always a key, so the value has to follow it: ... is an error. Amp-Thread-ID: https://ampcode.com/threads/T-01a22165-e448-7588-8b56-bc4dd19cc3a4 --- doc/grammars/types.pp3 | 13 +++++-- src/Parser/TypeParser.php | 14 ++++++-- tests/PHPStan/Parser/TypeParserTest.php | 46 +++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/doc/grammars/types.pp3 b/doc/grammars/types.pp3 index 67b43e6c..6ae3e30a 100644 --- a/doc/grammars/types.pp3 +++ b/doc/grammars/types.pp3 @@ -421,8 +421,17 @@ ArrayShapeUnsealed * What the keys and the values of whatever is left over are written as. */ ArrayShapeUnsealedType - : AngleBracketOpen() Trivia() Type() Trivia() - ( Trivia() Type() Trivia() )? + : AngleBracketOpen() Trivia() ArrayShapeUnsealedArguments() + ; + +/** + * The key may declare a template the value is written with, as in + * ..., Config[K]>; a declared template is always a key, so + * the value has to follow it. + */ +ArrayShapeUnsealedArguments + : TemplateDeclaration() Trivia() Trivia() Type() Trivia() + | !TemplateDeclarationStart() Type() Trivia() ( Trivia() Type() Trivia() )? ; /** diff --git a/src/Parser/TypeParser.php b/src/Parser/TypeParser.php index 60312093..5ab82d80 100644 --- a/src/Parser/TypeParser.php +++ b/src/Parser/TypeParser.php @@ -1060,11 +1060,21 @@ private function parseArrayShapeUnsealedType(TokenIterator $tokens): Ast\Type\Ar $tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET); $tokens->skipNewLineTokensAndConsumeComments(); - $valueType = $this->parse($tokens); + // a template may be declared by the key, as in ..., Config[K]>, + // so a declaration has to be followed by the value type + $templateDeclaration = $this->tryParseTemplateDeclaration($tokens); + $valueType = $templateDeclaration ?? $this->parse($tokens); $tokens->skipNewLineTokensAndConsumeComments(); $keyType = null; - if ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) { + if ($templateDeclaration !== null) { + $tokens->consumeTokenType(Lexer::TOKEN_COMMA); + $tokens->skipNewLineTokensAndConsumeComments(); + + $keyType = $valueType; + $valueType = $this->parse($tokens); + $tokens->skipNewLineTokensAndConsumeComments(); + } elseif ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) { $tokens->skipNewLineTokensAndConsumeComments(); $keyType = $valueType; diff --git a/tests/PHPStan/Parser/TypeParserTest.php b/tests/PHPStan/Parser/TypeParserTest.php index 7ab07439..7af5587f 100644 --- a/tests/PHPStan/Parser/TypeParserTest.php +++ b/tests/PHPStan/Parser/TypeParserTest.php @@ -1067,6 +1067,52 @@ public function provideParseData(): array ArrayShapeNode::KIND_NON_EMPTY_LIST, ), ], + [ + 'array{a: int, ...|int, Config[K]>}', + ArrayShapeNode::createUnsealed( + [ + new ArrayShapeItemNode( + new IdentifierTypeNode('a'), + false, + new IdentifierTypeNode('int'), + ), + ], + new ArrayShapeUnsealedTypeNode( + new OffsetAccessTypeNode(new IdentifierTypeNode('Config'), new IdentifierTypeNode('K')), + new TemplateDeclarationTypeNode('K', new UnionTypeNode([ + new GenericTypeNode( + new IdentifierTypeNode('key-of'), + [new IdentifierTypeNode('Config')], + [GenericTypeNode::VARIANCE_INVARIANT], + ), + new IdentifierTypeNode('int'), + ])), + ), + ArrayShapeNode::KIND_ARRAY, + ), + ], + [ + 'array{...}', + new ParserException( + '>', + Lexer::TOKEN_CLOSE_ANGLE_BRACKET, + 18, + Lexer::TOKEN_COMMA, + null, + null, + ), + ], + [ + 'array{...}', + ArrayShapeNode::createUnsealed( + [], + new ArrayShapeUnsealedTypeNode( + new IdentifierTypeNode('string'), + new IdentifierTypeNode('of'), + ), + ArrayShapeNode::KIND_ARRAY, + ), + ], [ 'array{...}', ArrayShapeNode::createUnsealed(