Skip to content

Short match #6511

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 6 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
26 changes: 26 additions & 0 deletions Zend/tests/match/short-match-ast.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Short-hand match with implicit true subject, AST printing.
--INI--
assert.exception=0
--FILE--
<?php

assert((function () {
$a = 3;
match {
$a < 2 => 'small',
$a == 3 => 'medium',
default => 'large',
};
})());

?>
--EXPECTF--
Warning: assert(): assert(function () {
$a = 3;
match {
$a < 2 => 'small',
$a == 3 => 'medium',
default => 'large',
};
}()) failed in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/match/short-match.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Short-hand match with implicit true subject.
--FILE--
<?php

$a = 3;

print match {
$a < 2 => 'small',
$a == 3 => 'medium',
default => 'large',
};

?>
--EXPECTF--
medium
11 changes: 8 additions & 3 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1979,9 +1979,14 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
zend_ast_export_stmt(str, ast->child[1], indent + 1);
break;
case ZEND_AST_MATCH:
smart_str_appends(str, "match (");
zend_ast_export_ex(str, ast->child[0], 0, indent);
smart_str_appends(str, ") {\n");
if (ast->attr & ZEND_MATCH_SHORT) {
smart_str_appends(str, "match {\n");
}
else {
smart_str_appends(str, "match (");
zend_ast_export_ex(str, ast->child[0], 0, indent);
smart_str_appends(str, ") {\n");
}
zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
zend_ast_export_indent(str, indent);
smart_str_appendc(str, '}');
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,8 @@ ZEND_API zend_string *zend_type_to_string(zend_type type);
#define ZEND_PARAM_REF (1<<3)
#define ZEND_PARAM_VARIADIC (1<<4)

#define ZEND_MATCH_SHORT (1<<5)

#define ZEND_NAME_FQ 0
#define ZEND_NAME_NOT_FQ 1
#define ZEND_NAME_RELATIVE 2
Expand Down
9 changes: 8 additions & 1 deletion Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,14 @@ case_separator:

match:
T_MATCH '(' expr ')' '{' match_arm_list '}'
{ $$ = zend_ast_create(ZEND_AST_MATCH, $3, $6); };
{ $$ = zend_ast_create(ZEND_AST_MATCH, $3, $6); }
| T_MATCH '{' match_arm_list '}'
{
zval zv;
ZVAL_BOOL(&zv, 1);
$$ = zend_ast_create(ZEND_AST_MATCH, zend_ast_create_zval(&zv), $3);
$$->attr = ZEND_MATCH_SHORT;
}
;

match_arm_list:
Expand Down