Skip to content

Commit a9cf551

Browse files
committed
test: allow only expressions
1 parent 33a5943 commit a9cf551

File tree

9 files changed

+56
-53
lines changed

9 files changed

+56
-53
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Short global function declaration
3+
--FILE--
4+
<?php
5+
function buz(): int => 123;
6+
echo buz() . PHP_EOL;
7+
8+
function compare(int $what, int $with): string => match(true) {
9+
$what > $with => "greater",
10+
$what < $with => "less",
11+
$what == $with => "equals",
12+
default => throw new Exception("Unreachable statement"),
13+
};
14+
15+
var_dump(compare(1, 2));
16+
var_dump(compare(20, 10));
17+
var_dump(compare(5, 5));
18+
19+
echo "done";
20+
?>
21+
--EXPECT--
22+
123
23+
string(4) "less"
24+
string(7) "greater"
25+
string(6) "equals"
26+
done

Zend/tests/short_function/short_method_declaration.phpt renamed to Zend/tests/short_function/method_declaration.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
--TEST--
2-
Short method function declaration
2+
Short function method declaration
33
--FILE--
44
<?php
55
class Decorator {
66
public $proxy;
7-
function getId() = $this->proxy->id;
8-
function getName() = $this->proxy->name;
7+
function getId() => $this->proxy->id;
8+
function getName() => $this->proxy->name;
99

10-
function setId($value) = $this->proxy->id = $value;
11-
function setName($value) = $this->proxy->name = $value;
10+
function setId($value) => $this->proxy->id = $value;
11+
function setName($value) => $this->proxy->name = $value;
1212
}
1313

1414
$decorated = new stdClass;

Zend/tests/short_function/short_method_declaration2.phpt renamed to Zend/tests/short_function/method_declaration2.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
--TEST--
2-
Short method function declaration
2+
Short function method declaration
33
--FILE--
44
<?php
55
class Describer {
6-
function getType(): string = $this->type;
7-
function getTypeName(): string = match($type) {
6+
function getType(): string => $this->type;
7+
function getTypeName(): string => match($type) {
88
"variable" => "Variable",
99
"function_return" => "Function Return Type",
1010
default => "unknown"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Test possible declarations for expr&stmt options
3+
--FILE--
4+
<?php
5+
function expr() => 123; // returns 123
6+
7+
var_dump(expr());
8+
9+
?>
10+
--EXPECT--
11+
int(123)

Zend/tests/short_function/short_function_declaration.phpt

Lines changed: 0 additions & 39 deletions
This file was deleted.

Zend/tests/type_declarations/intersection_types/invalid_types/invalid_nullable_type.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ function foo(): ?Countable&Iterator {}
77

88
?>
99
--EXPECTF--
10-
Parse error: syntax error, unexpected token "&", expecting "=" or "{" in %s on line %d
10+
Parse error: syntax error, unexpected token "&", expecting "=>" or "{" in %s on line %d

Zend/zend_compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8365,7 +8365,7 @@ static zend_op_array *zend_compile_func_decl_ex(
83658365
zend_compile_closure_uses(uses_ast);
83668366
}
83678367

8368-
if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) {
8368+
if (decl->flags & ZEND_ACC_SHORT_DECLARATION && stmt_ast->kind != ZEND_AST_RETURN) {
83698369
bool needs_return = true;
83708370
if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
83718371
zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;

Zend/zend_compile.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ typedef struct _zend_oparray_context {
333333
/* Class cannot be serialized or unserialized | | | */
334334
#define ZEND_ACC_NOT_SERIALIZABLE (1 << 29) /* X | | | */
335335
/* | | | */
336-
/* Function Flags (unused: 29-30) | | | */
336+
/* Function Flags (unused: 30) | | | */
337337
/* ============== | | | */
338338
/* | | | */
339339
/* deprecation flag | | | */
@@ -395,6 +395,9 @@ typedef struct _zend_oparray_context {
395395
/* has #[\Override] attribute | | | */
396396
#define ZEND_ACC_OVERRIDE (1 << 28) /* | X | | */
397397
/* | | | */
398+
/* Function returning by reference | | | */
399+
#define ZEND_ACC_SHORT_DECLARATION (1 << 29) /* | X | | */
400+
/* | | | */
398401
/* op_array uses strict mode types | | | */
399402
#define ZEND_ACC_STRICT_TYPES (1U << 31) /* | X | | */
400403

Zend/zend_language_parser.y

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,8 @@ function_declaration_statement:
585585

586586
function_body:
587587
'{' inner_statement_list '}' { $$ = $2; }
588-
| '=' statement { $$ = $2; }
588+
| T_DOUBLE_ARROW expr ';'
589+
{ $$ = $2; CG(extra_fn_flags) |= ZEND_ACC_SHORT_DECLARATION; }
589590
;
590591

591592
is_reference:
@@ -1035,7 +1036,8 @@ absolute_trait_method_reference:
10351036
method_body:
10361037
';' /* abstract method */ { $$ = NULL; }
10371038
| '{' inner_statement_list '}' { $$ = $2; }
1038-
| '=' statement { $$ = $2; }
1039+
| T_DOUBLE_ARROW expr ';'
1040+
{ $$ = $2; CG(extra_fn_flags) |= ZEND_ACC_SHORT_DECLARATION; }
10391041
;
10401042

10411043
property_modifiers:
@@ -1347,7 +1349,7 @@ inline_function:
13471349
$5, $7, $11, $8, NULL); CG(extra_fn_flags) = $9; }
13481350
| fn returns_ref backup_doc_comment '(' parameter_list ')' return_type
13491351
T_DOUBLE_ARROW backup_fn_flags backup_lex_pos expr backup_fn_flags
1350-
{ $$ = zend_ast_create_decl(ZEND_AST_ARROW_FUNC, $2 | $12, $1, $3,
1352+
{ $$ = zend_ast_create_decl(ZEND_AST_ARROW_FUNC, $2 | $12 | ZEND_ACC_SHORT_DECLARATION, $1, $3,
13511353
NULL, $5, NULL, $11, $7, NULL);
13521354
CG(extra_fn_flags) = $9; }
13531355
;

0 commit comments

Comments
 (0)