Skip to content

Commit d3b2cc0

Browse files
committed
Add parser support for constraint type
1 parent d179bc5 commit d3b2cc0

File tree

5 files changed

+63
-7
lines changed

5 files changed

+63
-7
lines changed
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)
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

+10-4
Original file line numberDiff line numberDiff line change
@@ -2342,10 +2342,6 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
23422342
APPEND_NODE_1("break");
23432343
case ZEND_AST_CONTINUE:
23442344
APPEND_NODE_1("continue");
2345-
case ZEND_AST_ASSOCIATED_TYPE:
2346-
smart_str_appends(str, "type ");
2347-
zend_ast_export_name(str, ast->child[0], 0, indent);
2348-
break;
23492345

23502346
/* 2 child nodes */
23512347
case ZEND_AST_DIM:
@@ -2668,6 +2664,16 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
26682664
smart_str_appends(str, ": ");
26692665
ast = ast->child[1];
26702666
goto tail_call;
2667+
case ZEND_AST_ASSOCIATED_TYPE:
2668+
smart_str_appends(str, "type ");
2669+
zend_ast_export_name(str, ast->child[0], 0, indent);
2670+
if (ast->child[1]) {
2671+
smart_str_appends(str, " : ");
2672+
smart_str_appends(str, " : ");
2673+
zend_ast_export_type(str, ast->child[1], indent);
2674+
}
2675+
smart_str_appendc(str, ';');
2676+
break;
26712677

26722678
/* 3 child nodes */
26732679
case ZEND_AST_METHOD_CALL:

Zend/zend_ast.h

+1-1
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

+4-2
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)