Skip to content

Commit 5bec932

Browse files
committed
Properly reject non-expression short function bodies.
1 parent ca79c71 commit 5bec932

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Non-expression statements parse error in short functions.
3+
--FILE--
4+
<?php
5+
6+
function test(array $a) => foreach ($a as $v) print $v;
7+
8+
test([1, 2, 3]);
9+
10+
?>
11+
--EXPECTF--
12+
Parse error: syntax error, unexpected token "foreach" in %s on line %d
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Short methods.
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
8+
public function __construct(private int $b) {}
9+
10+
public function out(array $a) => foreach ($a as $v) print $v;
11+
}
12+
13+
$t = new Test(1);
14+
15+
print $t->out([1, 2, 3]) . PHP_EOL;
16+
17+
?>
18+
--EXPECTF--
19+
Parse error: syntax error, unexpected token "foreach" in %s on line %d

Zend/zend_language_parser.y

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ function_declaration_statement:
549549
{ $$ = zend_ast_create_decl(ZEND_AST_FUNC_DECL, $2 | $13, $1, $4,
550550
zend_ast_get_str($3), $6, NULL, $11, $8, NULL); CG(extra_fn_flags) = $9; }
551551
| function returns_ref T_STRING backup_doc_comment '(' parameter_list ')' return_type
552-
backup_fn_flags T_DOUBLE_ARROW inner_statement backup_fn_flags
552+
backup_fn_flags T_DOUBLE_ARROW expr ';' backup_fn_flags
553553
{ $$ = zend_ast_create_decl(ZEND_AST_FUNC_DECL, $2, $1, $4,
554554
zend_ast_get_str($3), $6, NULL, zend_ast_create(ZEND_AST_RETURN, $11), $8, NULL); CG(extra_fn_flags) = $9; }
555555
;
@@ -927,7 +927,7 @@ absolute_trait_method_reference:
927927
method_body:
928928
';' /* abstract method */ { $$ = NULL; }
929929
| '{' inner_statement_list '}' { $$ = $2; }
930-
| T_DOUBLE_ARROW inner_statement { $$ = zend_ast_create(ZEND_AST_RETURN, $2); }
930+
| T_DOUBLE_ARROW expr ';' { $$ = zend_ast_create(ZEND_AST_RETURN, $2); }
931931
;
932932

933933
variable_modifiers:

0 commit comments

Comments
 (0)