Skip to content

Short functions #6221

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

Closed
wants to merge 9 commits into from
Closed
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
23 changes: 23 additions & 0 deletions Zend/tests/short_functions/short-func-match.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Short functions with match statements.
--FILE--
<?php

function pick_one(int $a) => match($a) {
1 => 'One',
2 => 'Two',
3 => 'Three',
default => 'More',
};

print pick_one(1) . PHP_EOL;
print pick_one(2) . PHP_EOL;
print pick_one(3) . PHP_EOL;
print pick_one(4) . PHP_EOL;

?>
--EXPECT--
One
Two
Three
More
12 changes: 12 additions & 0 deletions Zend/tests/short_functions/short-func-no-statement.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Non-expression statements parse error in short functions.
--FILE--
<?php

function test(array $a) => foreach ($a as $v) print $v;

test([1, 2, 3]);

?>
--EXPECTF--
Parse error: syntax error, unexpected token "foreach" in %s on line %d
17 changes: 17 additions & 0 deletions Zend/tests/short_functions/short-func.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Basic short functions return values.
--FILE--
<?php

function test(int $a) => $a + 1;

function test2(int $b): int
=> $b + 1;

print test(5) . PHP_EOL;
print test2(5) . PHP_EOL;

?>
--EXPECT--
6
6
19 changes: 19 additions & 0 deletions Zend/tests/short_functions/short-generator.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Short functions that yield.
--FILE--
<?php

function read($it): iterable => yield from $it;

$a = [1, 2, 3];

print_r(iterator_to_array(read($a)));

?>
--EXPECT--
Array
(
[0] => 1
[1] => 2
[2] => 3
)
19 changes: 19 additions & 0 deletions Zend/tests/short_functions/short-method-no-statement.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Short methods.
--FILE--
<?php

class Test {

public function __construct(private int $b) {}

public function out(array $a) => foreach ($a as $v) print $v;
}

$t = new Test(1);

print $t->out([1, 2, 3]) . PHP_EOL;

?>
--EXPECTF--
Parse error: syntax error, unexpected token "foreach" in %s on line %d
24 changes: 24 additions & 0 deletions Zend/tests/short_functions/short-method.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Short methods.
--FILE--
<?php

class Test {

public function __construct(private int $b) {}

public function addUp(int $a) => $a + $this->b;

public function addUp2(int $a): int
=> $a + $this->b;
}

$t = new Test(1);

print $t->addUp(5) . PHP_EOL;
print $t->addUp2(5) . PHP_EOL;

?>
--EXPECT--
6
6
17 changes: 11 additions & 6 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ 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
%type <ast> function_declaration_statement 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 @@ -547,12 +548,16 @@ unset_variable:
;

function_declaration_statement:
function returns_ref T_STRING 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; }
function returns_ref T_STRING backup_doc_comment '(' parameter_list ')' return_type
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; }
;

function_body:
'{' inner_statement_list '}' { $$ = $2; }
| T_DOUBLE_ARROW expr ';' { $$ = zend_ast_create(ZEND_AST_RETURN, $2); }

is_reference:
%empty { $$ = 0; }
| '&' { $$ = ZEND_PARAM_REF; }
Expand Down Expand Up @@ -947,7 +952,7 @@ absolute_trait_method_reference:

method_body:
';' /* abstract method */ { $$ = NULL; }
| '{' inner_statement_list '}' { $$ = $2; }
| function_body { $$ = $1; }
;

variable_modifiers:
Expand Down