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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
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):
13 changes: 13 additions & 0 deletions Zend/tests/arrow_functions/multiline_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
Arrow functions implicit use must be throwing notices only upon actual use
--FILE--
<?php

$b = 1;

var_dump((fn() => {return $b + $c;})());

?>
--EXPECTF--
Warning: Undefined variable $c in %s on line %d
int(1)
19 changes: 19 additions & 0 deletions Zend/tests/arrow_functions/multiline_gh7900_return.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
GH-7900: Arrow function with never return type compile-time errors (multiline, no errors)
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php

$x = fn(): never => { throw new \Exception('Here'); };

try {
var_dump($x());
} catch (\Exception $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECT--
Here
17 changes: 17 additions & 0 deletions Zend/tests/arrow_functions/multiline_gh7900_throw.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
GH-7900: Arrow function with never return type compile-time errors (multiline, Fatal error)
--INI--
zend.assertions=1
assert.exception=1
--FILE--
<?php

try {
assert((fn(): never => { return 42; }) && false);
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECTF--
Fatal error: A never-returning function must not return in %s on line %d
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