Skip to content

feat: allow multiline arrow functions #18500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: allow multiline arrow functions
  • Loading branch information
xepozz committed May 4, 2025
commit f55a097827f971a29964d010fd260e1d38d7c48e
21 changes: 21 additions & 0 deletions Zend/tests/arrow_functions/multiline_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Multiline arrow function
--FILE--
<?php

function a($a) {
$f = fn($a) => {
echo "inside fn($a):\n";
return $a;
};

$f($a);
echo "inside a($a):\n";
}

a(12345);

?>
--EXPECT--
inside fn(12345):
inside a(12345):
4 changes: 3 additions & 1 deletion Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8365,7 +8365,9 @@ static zend_op_array *zend_compile_func_decl_ex(
zend_compile_closure_uses(uses_ast);
}

if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) {
if (ast->kind == ZEND_AST_ARROW_FUNC
&& stmt_ast->kind != ZEND_AST_RETURN
&& stmt_ast->kind != ZEND_AST_STMT_LIST) {
bool needs_return = true;
if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
Expand Down
10 changes: 6 additions & 4 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -1337,13 +1337,15 @@ inline_function:
function returns_ref backup_doc_comment '(' parameter_list ')' lexical_vars return_type
backup_fn_flags '{' inner_statement_list '}' backup_fn_flags
{ $$ = zend_ast_create_decl(ZEND_AST_CLOSURE, $2 | $13, $1, $3,
NULL,
$5, $7, $11, $8, NULL); CG(extra_fn_flags) = $9; }
NULL, $5, $7, $11, $8, NULL); CG(extra_fn_flags) = $9; }
| fn returns_ref backup_doc_comment '(' parameter_list ')' return_type
T_DOUBLE_ARROW backup_fn_flags backup_lex_pos expr backup_fn_flags
{ $$ = zend_ast_create_decl(ZEND_AST_ARROW_FUNC, $2 | $12, $1, $3,
NULL, $5, NULL, $11, $7, NULL);
CG(extra_fn_flags) = $9; }
NULL, $5, NULL, $11, $7, NULL); CG(extra_fn_flags) = $9; }
| fn returns_ref backup_doc_comment '(' parameter_list ')' return_type
T_DOUBLE_ARROW backup_fn_flags '{' inner_statement_list '}' backup_fn_flags
{ $$ = zend_ast_create_decl(ZEND_AST_ARROW_FUNC, $2 | $13, $1, $3,
NULL, $5, NULL, $11, $7, NULL); CG(extra_fn_flags) = $9; }
;

fn:
Expand Down