Skip to content

Commit ac6897b

Browse files
committed
Add parser support for constraint type
1 parent 099b370 commit ac6897b

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
@@ -2377,10 +2377,6 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
23772377
APPEND_NODE_1("break");
23782378
case ZEND_AST_CONTINUE:
23792379
APPEND_NODE_1("continue");
2380-
case ZEND_AST_ASSOCIATED_TYPE:
2381-
smart_str_appends(str, "type ");
2382-
zend_ast_export_name(str, ast->child[0], 0, indent);
2383-
break;
23842380

23852381
/* 2 child nodes */
23862382
case ZEND_AST_DIM:
@@ -2703,6 +2699,16 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
27032699
smart_str_appends(str, ": ");
27042700
ast = ast->child[1];
27052701
goto tail_call;
2702+
case ZEND_AST_ASSOCIATED_TYPE:
2703+
smart_str_appends(str, "type ");
2704+
zend_ast_export_name(str, ast->child[0], 0, indent);
2705+
if (ast->child[1]) {
2706+
smart_str_appends(str, " : ");
2707+
smart_str_appends(str, " : ");
2708+
zend_ast_export_type(str, ast->child[1], indent);
2709+
}
2710+
smart_str_appendc(str, ';');
2711+
break;
27062712

27072713
/* 3 child nodes */
27082714
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
@@ -664,8 +664,10 @@ enum_case_expr:
664664
;
665665

666666
associated_type:
667-
T_TYPE name ';'
668-
{ $$ = zend_ast_create(ZEND_AST_ASSOCIATED_TYPE, $2); }
667+
T_TYPE name ':' type_expr_without_static ';'
668+
{ $$ = zend_ast_create(ZEND_AST_ASSOCIATED_TYPE, $2, $4); }
669+
| T_TYPE name ';'
670+
{ $$ = zend_ast_create(ZEND_AST_ASSOCIATED_TYPE, $2, NULL); }
669671
;
670672

671673
extends_from:

0 commit comments

Comments
 (0)