Skip to content

Commit 4960d30

Browse files
committed
fix: add parentheses to ast functions dump
1 parent 9040e79 commit 4960d30

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

Zend/tests/arrow_functions/007.phpt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ zend.assertions=1
55
--FILE--
66
<?php
77

8-
// TODO We're missing parentheses for the direct call
9-
108
try {
119
assert((fn() => false)());
1210
} catch (AssertionError $e) {
@@ -21,5 +19,5 @@ try {
2119

2220
?>
2321
--EXPECT--
24-
assert(): assert(fn() => false()) failed
25-
assert(): assert(fn&(int ...$args): ?bool => $args[0](false)) failed
22+
assert(): assert((fn() => false)()) failed
23+
assert(): assert((fn&(int ...$args): ?bool => $args[0])(false)) failed

Zend/tests/enum/ast-dumper.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ try {
2828

2929
?>
3030
--EXPECT--
31-
assert(function () {
31+
assert((function () {
3232
enum Foo {
3333
case Bar;
3434
}
@@ -45,4 +45,4 @@ assert(function () {
4545
}
4646

4747
return false;
48-
}())
48+
})())

Zend/tests/functions/007.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Pretty printing for arrow functions
3+
--INI--
4+
zend.assertions=1
5+
--FILE--
6+
<?php
7+
8+
try {
9+
assert((function() { return false; })());
10+
} catch (AssertionError $e) {
11+
echo 'assert(): ', $e->getMessage(), ' failed', PHP_EOL;
12+
}
13+
14+
?>
15+
--EXPECT--
16+
assert(): assert((function () {
17+
return false;
18+
})()) failed

Zend/tests/match/009_ast_export.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ assert((function () {
1919

2020
?>
2121
--EXPECTF--
22-
assert(): assert(function () {
22+
assert(): assert((function () {
2323
match ('foo') {
2424
'foo', 'bar' => false,
2525
'baz' => 'a',
2626
default => 'b',
2727
};
28-
}()) failed
28+
})()) failed

Zend/zend_ast.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2233,7 +2233,14 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
22332233
zend_ast_export_var(str, ast->child[1], 0, indent);
22342234
break;
22352235
case ZEND_AST_CALL:
2236-
zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2236+
zend_ast *left = ast->child[0];
2237+
if (left->kind == ZEND_AST_ARROW_FUNC || left->kind == ZEND_AST_CLOSURE) {
2238+
smart_str_appends(str, "(");
2239+
zend_ast_export_ns_name(str, left, 0, indent);
2240+
smart_str_appends(str, ")");
2241+
} else {
2242+
zend_ast_export_ns_name(str, left, 0, indent);
2243+
}
22372244
smart_str_appendc(str, '(');
22382245
zend_ast_export_ex(str, ast->child[1], 0, indent);
22392246
smart_str_appendc(str, ')');

0 commit comments

Comments
 (0)