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 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
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
17 changes: 17 additions & 0 deletions Zend/tests/short_function/inline_function.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Inline short declaration function
--FILE--
<?php
$f = function() => 123;

var_dump($f());
var_dump((function() => null)());
var_dump(get_debug_type(function() => null()));
var_dump(get_debug_type((function() => null)()));

?>
--EXPECT--
int(123)
NULL
string(7) "Closure"
string(4) "null"
28 changes: 28 additions & 0 deletions Zend/tests/short_function/inline_function_deref.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Inline short declaration function dereference
--FILE--
<?php

function a () {
$a = function() => 123;
$b = function() => $a;

return $b;
}

var_dump(get_debug_type( a()() ));

function b () {
$a = fn() => 123;
$b = fn() => $a;

return $b;
}

var_dump(get_debug_type( b()() ));

?>
--EXPECTF--
Warning: Undefined variable $a in %s on line %d
string(4) "null"
string(7) "Closure"
40 changes: 40 additions & 0 deletions Zend/tests/short_function/method_declaration.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Short function method declaration
--FILE--
<?php
class Decorator {
public $proxy;
function getId() => $this->proxy->id;
function getName() => $this->proxy->name;

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

$decorated = new stdClass;
$decorated->id = null;
$decorated->name = null;

$decorator = new Decorator;
$decorator->proxy = $decorated;

var_dump($decorated);

$decorator->setId(1);
$decorator->setName('Dmitrii');

var_dump($decorated);
?>
--EXPECT--
object(stdClass)#1 (2) {
["id"]=>
NULL
["name"]=>
NULL
}
object(stdClass)#1 (2) {
["id"]=>
int(1)
["name"]=>
string(7) "Dmitrii"
}
14 changes: 14 additions & 0 deletions Zend/tests/short_function/method_declaration2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Short function method declaration
--FILE--
<?php
class Describer {
function getType(): string => $this->type;
function getTypeName(): string => match($type) {
"variable" => "Variable",
"function_return" => "Function Return Type",
default => "unknown"
};
}
?>
--EXPECT--
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)
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 "{" 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
30 changes: 22 additions & 8 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
/* Token used to force a parse error from the lexer */
%token T_ERROR

%type <ast> top_statement namespace_name name statement function_declaration_statement
%type <ast> top_statement namespace_name name statement function_declaration_statement function_body inline_function_body
%type <ast> class_declaration_statement trait_declaration_statement legacy_namespace_name
%type <ast> interface_declaration_statement interface_extends_list
%type <ast> group_use_declaration inline_use_declarations inline_use_declaration
Expand Down Expand Up @@ -578,9 +578,15 @@ function_name:

function_declaration_statement:
function returns_ref function_name backup_doc_comment '(' parameter_list ')' return_type
backup_fn_flags '{' inner_statement_list '}' backup_fn_flags
{ $$ = zend_ast_create_decl(ZEND_AST_FUNC_DECL, $2 | $13, $1, $4,
zend_ast_get_str($3), $6, NULL, $11, $8, NULL); CG(extra_fn_flags) = $9; }
backup_fn_flags function_body backup_fn_flags
{ $$ = zend_ast_create_decl(ZEND_AST_FUNC_DECL, $2 | $11, $1, $4,
zend_ast_get_str($3), $6, NULL, $10, $8, NULL); CG(extra_fn_flags) = $9; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look correct. function foo() = 'bar'; will result in the equivalent of function foo() { 'bar'; return null; }, which doesn't have the desired effect.

;

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

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

property_modifiers:
Expand Down Expand Up @@ -1335,17 +1343,23 @@ expr:

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,
backup_fn_flags inline_function_body backup_fn_flags
{ $$ = zend_ast_create_decl(ZEND_AST_CLOSURE, $2 | $11, $1, $3,
NULL,
$5, $7, $11, $8, NULL); CG(extra_fn_flags) = $9; }
$5, $7, $10, $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; }
;

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

fn:
T_FN { $$ = CG(zend_lineno); }
;
Expand Down