diff --git a/Zend/tests/match/short-match-ast.phpt b/Zend/tests/match/short-match-ast.phpt new file mode 100644 index 0000000000000..edb5662b97ae4 --- /dev/null +++ b/Zend/tests/match/short-match-ast.phpt @@ -0,0 +1,26 @@ +--TEST-- +Short-hand match with implicit true subject, AST printing. +--INI-- +assert.exception=0 +--FILE-- + '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 diff --git a/Zend/tests/match/short-match.phpt b/Zend/tests/match/short-match.phpt new file mode 100644 index 0000000000000..bd2b36e397dfb --- /dev/null +++ b/Zend/tests/match/short-match.phpt @@ -0,0 +1,16 @@ +--TEST-- +Short-hand match with implicit true subject. +--FILE-- + 'small', + $a == 3 => 'medium', + default => 'large', +}; + +?> +--EXPECTF-- +medium diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c index cb61bec5d7644..fff981c2f16f6 100644 --- a/Zend/zend_ast.c +++ b/Zend/zend_ast.c @@ -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, '}'); diff --git a/Zend/zend_compile.h b/Zend/zend_compile.h index b4af5d9e3fa12..efc1b3816dcc3 100644 --- a/Zend/zend_compile.h +++ b/Zend/zend_compile.h @@ -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 diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y index ed800c46f867e..df0f032f6f87d 100644 --- a/Zend/zend_language_parser.y +++ b/Zend/zend_language_parser.y @@ -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: