Skip to content

Commit

Permalink
Properly reject non-expression short function bodies.
Browse files Browse the repository at this point in the history
  • Loading branch information
Crell committed Oct 19, 2020
1 parent ca79c71 commit 5bec932
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Zend/tests/short_functions/short-func-no-statement.phpt
@@ -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
19 changes: 19 additions & 0 deletions Zend/tests/short_functions/short-method-no-statement.phpt
@@ -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
4 changes: 2 additions & 2 deletions Zend/zend_language_parser.y
Expand Up @@ -549,7 +549,7 @@ function_declaration_statement:
{ $$ = 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 T_DOUBLE_ARROW inner_statement backup_fn_flags
backup_fn_flags T_DOUBLE_ARROW expr ';' backup_fn_flags
{ $$ = zend_ast_create_decl(ZEND_AST_FUNC_DECL, $2, $1, $4,
zend_ast_get_str($3), $6, NULL, zend_ast_create(ZEND_AST_RETURN, $11), $8, NULL); CG(extra_fn_flags) = $9; }
;
Expand Down Expand Up @@ -927,7 +927,7 @@ absolute_trait_method_reference:
method_body:
';' /* abstract method */ { $$ = NULL; }
| '{' inner_statement_list '}' { $$ = $2; }
| T_DOUBLE_ARROW inner_statement { $$ = zend_ast_create(ZEND_AST_RETURN, $2); }
| T_DOUBLE_ARROW expr ';' { $$ = zend_ast_create(ZEND_AST_RETURN, $2); }
;

variable_modifiers:
Expand Down

0 comments on commit 5bec932

Please sign in to comment.