Skip to content

Commit 132330c

Browse files
committed
Add parser support for constraint type
1 parent 322bd3d commit 132330c

File tree

5 files changed

+63
-7
lines changed

5 files changed

+63
-7
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Associated type with a constraint
3+
--FILE--
4+
<?php
5+
6+
interface I {
7+
type T : int|string;
8+
public function foo(T $param): T;
9+
}
10+
11+
class CS implements I {
12+
public function foo(string $param): string {
13+
return $param . '!';
14+
}
15+
}
16+
17+
class CI implements I {
18+
public function foo(int $param): int {
19+
return $param + 42;
20+
}
21+
}
22+
23+
$cs = new CS();
24+
var_dump($cs->foo("Hello"));
25+
26+
$ci = new CI();
27+
var_dump($ci->foo(5));
28+
29+
?>
30+
--EXPECT--
31+
string(6) "Hello!"
32+
int(47)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Associated type with a constraint that is not satisfied
3+
--FILE--
4+
<?php
5+
6+
interface I {
7+
type T : int|string;
8+
public function foo(T $param): T;
9+
}
10+
11+
class C implements I {
12+
public function foo(float $param): float {}
13+
}
14+
15+
?>
16+
--EXPECT--

Zend/zend_ast.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,10 +2403,6 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
24032403
APPEND_NODE_1("break");
24042404
case ZEND_AST_CONTINUE:
24052405
APPEND_NODE_1("continue");
2406-
case ZEND_AST_ASSOCIATED_TYPE:
2407-
smart_str_appends(str, "type ");
2408-
zend_ast_export_name(str, ast->child[0], 0, indent);
2409-
break;
24102406

24112407
/* 2 child nodes */
24122408
case ZEND_AST_DIM:
@@ -2729,6 +2725,16 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
27292725
smart_str_appends(str, ": ");
27302726
ast = ast->child[1];
27312727
goto tail_call;
2728+
case ZEND_AST_ASSOCIATED_TYPE:
2729+
smart_str_appends(str, "type ");
2730+
zend_ast_export_name(str, ast->child[0], 0, indent);
2731+
if (ast->child[1]) {
2732+
smart_str_appends(str, " : ");
2733+
smart_str_appends(str, " : ");
2734+
zend_ast_export_type(str, ast->child[1], indent);
2735+
}
2736+
smart_str_appendc(str, ';');
2737+
break;
27322738

27332739
/* 3 child nodes */
27342740
case ZEND_AST_METHOD_CALL:

Zend/zend_ast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ enum _zend_ast_kind {
9999
ZEND_AST_POST_DEC,
100100
ZEND_AST_YIELD_FROM,
101101
ZEND_AST_CLASS_NAME,
102-
ZEND_AST_ASSOCIATED_TYPE,
103102

104103
ZEND_AST_GLOBAL,
105104
ZEND_AST_UNSET,
@@ -155,6 +154,7 @@ enum _zend_ast_kind {
155154
ZEND_AST_MATCH_ARM,
156155
ZEND_AST_NAMED_ARG,
157156
ZEND_AST_PARENT_PROPERTY_HOOK_CALL,
157+
ZEND_AST_ASSOCIATED_TYPE,
158158

159159
/* 3 child nodes */
160160
ZEND_AST_METHOD_CALL = 3 << ZEND_AST_NUM_CHILDREN_SHIFT,

Zend/zend_language_parser.y

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,10 @@ enum_case_expr:
668668
;
669669

670670
associated_type:
671-
T_TYPE name ';'
672-
{ $$ = zend_ast_create(ZEND_AST_ASSOCIATED_TYPE, $2); }
671+
T_TYPE name ':' type_expr_without_static ';'
672+
{ $$ = zend_ast_create(ZEND_AST_ASSOCIATED_TYPE, $2, $4); }
673+
| T_TYPE name ';'
674+
{ $$ = zend_ast_create(ZEND_AST_ASSOCIATED_TYPE, $2, NULL); }
673675
;
674676

675677
extends_from:

0 commit comments

Comments
 (0)