Skip to content

feat: single expression functions #17677

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
Prev Previous commit
Next Next commit
test: allow only expressions
  • Loading branch information
xepozz committed May 28, 2025
commit a9cf551dff08b785fe830f50f38daae411de85d3
26 changes: 26 additions & 0 deletions Zend/tests/short_function/global_declaration.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Short global function declaration
--FILE--
<?php
function buz(): int => 123;
echo buz() . PHP_EOL;

function compare(int $what, int $with): string => match(true) {
$what > $with => "greater",
$what < $with => "less",
$what == $with => "equals",
default => throw new Exception("Unreachable statement"),
};

var_dump(compare(1, 2));
var_dump(compare(20, 10));
var_dump(compare(5, 5));

echo "done";
?>
--EXPECT--
123
string(4) "less"
string(7) "greater"
string(6) "equals"
done
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
--TEST--
Short method function declaration
Short function method declaration
--FILE--
<?php
class Decorator {
public $proxy;
function getId() = $this->proxy->id;
function getName() = $this->proxy->name;
function getId() => $this->proxy->id;
function getName() => $this->proxy->name;

function setId($value) = $this->proxy->id = $value;
function setName($value) = $this->proxy->name = $value;
function setId($value) => $this->proxy->id = $value;
function setName($value) => $this->proxy->name = $value;
}

$decorated = new stdClass;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
--TEST--
Short method function declaration
Short function method declaration
--FILE--
<?php
class Describer {
function getType(): string = $this->type;
function getTypeName(): string = match($type) {
function getType(): string => $this->type;
function getTypeName(): string => match($type) {
"variable" => "Variable",
"function_return" => "Function Return Type",
default => "unknown"
Expand Down
11 changes: 11 additions & 0 deletions Zend/tests/short_function/possible_declaration.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Test possible declarations for expr&stmt options
--FILE--
<?php
function expr() => 123; // returns 123

var_dump(expr());

?>
--EXPECT--
int(123)
39 changes: 0 additions & 39 deletions Zend/tests/short_function/short_function_declaration.phpt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ function foo(): ?Countable&Iterator {}

?>
--EXPECTF--
Parse error: syntax error, unexpected token "&", expecting "=" or "{" in %s on line %d
Parse error: syntax error, unexpected token "&", expecting "=>" or "{" in %s on line %d
2 changes: 1 addition & 1 deletion Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8365,7 +8365,7 @@ 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 (decl->flags & ZEND_ACC_SHORT_DECLARATION && stmt_ast->kind != ZEND_AST_RETURN) {
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
5 changes: 4 additions & 1 deletion Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ typedef struct _zend_oparray_context {
/* Class cannot be serialized or unserialized | | | */
#define ZEND_ACC_NOT_SERIALIZABLE (1 << 29) /* X | | | */
/* | | | */
/* Function Flags (unused: 29-30) | | | */
/* Function Flags (unused: 30) | | | */
/* ============== | | | */
/* | | | */
/* deprecation flag | | | */
Expand Down Expand Up @@ -395,6 +395,9 @@ typedef struct _zend_oparray_context {
/* has #[\Override] attribute | | | */
#define ZEND_ACC_OVERRIDE (1 << 28) /* | X | | */
/* | | | */
/* Function returning by reference | | | */
#define ZEND_ACC_SHORT_DECLARATION (1 << 29) /* | X | | */
/* | | | */
/* op_array uses strict mode types | | | */
#define ZEND_ACC_STRICT_TYPES (1U << 31) /* | X | | */

Expand Down
8 changes: 5 additions & 3 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,8 @@ function_declaration_statement:

function_body:
'{' inner_statement_list '}' { $$ = $2; }
| '=' statement { $$ = $2; }
| T_DOUBLE_ARROW expr ';'
{ $$ = $2; CG(extra_fn_flags) |= ZEND_ACC_SHORT_DECLARATION; }
;

is_reference:
Expand Down Expand Up @@ -1035,7 +1036,8 @@ absolute_trait_method_reference:
method_body:
';' /* abstract method */ { $$ = NULL; }
| '{' inner_statement_list '}' { $$ = $2; }
| '=' statement { $$ = $2; }
| T_DOUBLE_ARROW expr ';'
{ $$ = $2; CG(extra_fn_flags) |= ZEND_ACC_SHORT_DECLARATION; }
;

property_modifiers:
Expand Down Expand Up @@ -1347,7 +1349,7 @@ inline_function:
$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,
{ $$ = zend_ast_create_decl(ZEND_AST_ARROW_FUNC, $2 | $12 | ZEND_ACC_SHORT_DECLARATION, $1, $3,
NULL, $5, NULL, $11, $7, NULL);
CG(extra_fn_flags) = $9; }
;
Expand Down