Skip to content

Commit fdebad0

Browse files
authored
zend_ast: Add parentheses around IIFE in zend_ast_export() (#18688)
1 parent 399cb4c commit fdebad0

File tree

6 files changed

+35
-10
lines changed

6 files changed

+35
-10
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ PHP NEWS
5252
. Fixed bugs GH-17711 and GH-18022 (Infinite recursion on deprecated attribute
5353
evaluation) and GH-18464 (Recursion protection for deprecation constants not
5454
released on bailout). (DanielEScherzer and ilutov)
55+
. Fixed AST printing for immediately invoked Closure. (Dmitrii Derepko)
5556

5657
- Curl:
5758
. Added curl_multi_get_handles(). (timwolla)

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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,12 +2424,20 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
24242424
smart_str_appends(str, "::$");
24252425
zend_ast_export_var(str, ast->child[1], 0, indent);
24262426
break;
2427-
case ZEND_AST_CALL:
2428-
zend_ast_export_ns_name(str, ast->child[0], 0, indent);
2427+
case ZEND_AST_CALL: {
2428+
zend_ast *left = ast->child[0];
2429+
if (left->kind == ZEND_AST_ARROW_FUNC || left->kind == ZEND_AST_CLOSURE) {
2430+
smart_str_appends(str, "(");
2431+
zend_ast_export_ns_name(str, left, 0, indent);
2432+
smart_str_appends(str, ")");
2433+
} else {
2434+
zend_ast_export_ns_name(str, left, 0, indent);
2435+
}
24292436
smart_str_appendc(str, '(');
24302437
zend_ast_export_ex(str, ast->child[1], 0, indent);
24312438
smart_str_appendc(str, ')');
24322439
break;
2440+
}
24332441
case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
24342442
smart_str_append(str, Z_STR_P(zend_ast_get_zval(ast->child[0])));
24352443
smart_str_appendc(str, '(');

0 commit comments

Comments
 (0)