zend_ast: Add parentheses around IIFE in zend_ast_export() (#18688)

This commit is contained in:
Dmitrii Derepko 2025-05-29 17:47:47 +04:00 committed by GitHub
parent 399cb4ca85
commit fdebad0b25
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 35 additions and 10 deletions

1
NEWS
View file

@ -52,6 +52,7 @@ PHP NEWS
. Fixed bugs GH-17711 and GH-18022 (Infinite recursion on deprecated attribute . Fixed bugs GH-17711 and GH-18022 (Infinite recursion on deprecated attribute
evaluation) and GH-18464 (Recursion protection for deprecation constants not evaluation) and GH-18464 (Recursion protection for deprecation constants not
released on bailout). (DanielEScherzer and ilutov) released on bailout). (DanielEScherzer and ilutov)
. Fixed AST printing for immediately invoked Closure. (Dmitrii Derepko)
- Curl: - Curl:
. Added curl_multi_get_handles(). (timwolla) . Added curl_multi_get_handles(). (timwolla)

View file

@ -5,8 +5,6 @@ zend.assertions=1
--FILE-- --FILE--
<?php <?php
// TODO We're missing parentheses for the direct call
try { try {
assert((fn() => false)()); assert((fn() => false)());
} catch (AssertionError $e) { } catch (AssertionError $e) {
@ -21,5 +19,5 @@ try {
?> ?>
--EXPECT-- --EXPECT--
assert(): assert(fn() => false()) failed assert(): assert((fn() => false)()) failed
assert(): assert(fn&(int ...$args): ?bool => $args[0](false)) failed assert(): assert((fn&(int ...$args): ?bool => $args[0])(false)) failed

View file

@ -28,7 +28,7 @@ try {
?> ?>
--EXPECT-- --EXPECT--
assert(function () { assert((function () {
enum Foo { enum Foo {
case Bar; case Bar;
} }
@ -45,4 +45,4 @@ assert(function () {
} }
return false; return false;
}()) })())

View file

@ -0,0 +1,18 @@
--TEST--
Pretty printing for arrow functions
--INI--
zend.assertions=1
--FILE--
<?php
try {
assert((function() { return false; })());
} catch (AssertionError $e) {
echo 'assert(): ', $e->getMessage(), ' failed', PHP_EOL;
}
?>
--EXPECT--
assert(): assert((function () {
return false;
})()) failed

View file

@ -19,10 +19,10 @@ assert((function () {
?> ?>
--EXPECTF-- --EXPECTF--
assert(): assert(function () { assert(): assert((function () {
match ('foo') { match ('foo') {
'foo', 'bar' => false, 'foo', 'bar' => false,
'baz' => 'a', 'baz' => 'a',
default => 'b', default => 'b',
}; };
}()) failed })()) failed

View file

@ -2424,12 +2424,20 @@ simple_list:
smart_str_appends(str, "::$"); smart_str_appends(str, "::$");
zend_ast_export_var(str, ast->child[1], 0, indent); zend_ast_export_var(str, ast->child[1], 0, indent);
break; break;
case ZEND_AST_CALL: case ZEND_AST_CALL: {
zend_ast_export_ns_name(str, ast->child[0], 0, indent); zend_ast *left = ast->child[0];
if (left->kind == ZEND_AST_ARROW_FUNC || left->kind == ZEND_AST_CLOSURE) {
smart_str_appends(str, "(");
zend_ast_export_ns_name(str, left, 0, indent);
smart_str_appends(str, ")");
} else {
zend_ast_export_ns_name(str, left, 0, indent);
}
smart_str_appendc(str, '('); smart_str_appendc(str, '(');
zend_ast_export_ex(str, ast->child[1], 0, indent); zend_ast_export_ex(str, ast->child[1], 0, indent);
smart_str_appendc(str, ')'); smart_str_appendc(str, ')');
break; break;
}
case ZEND_AST_PARENT_PROPERTY_HOOK_CALL: case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
smart_str_append(str, Z_STR_P(zend_ast_get_zval(ast->child[0]))); smart_str_append(str, Z_STR_P(zend_ast_get_zval(ast->child[0])));
smart_str_appendc(str, '('); smart_str_appendc(str, '(');