From babca699a15ea6491d10474882ac9040040419db Mon Sep 17 00:00:00 2001 From: Dmitriy Derepko Date: Tue, 4 Feb 2025 10:27:47 +0300 Subject: [PATCH 1/2] feat: implement empty match subject (default=true) --- Zend/tests/match/048.phpt | 25 +++++++++++++++++++++++++ Zend/zend_language_parser.y | 10 +++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 Zend/tests/match/048.phpt diff --git a/Zend/tests/match/048.phpt b/Zend/tests/match/048.phpt new file mode 100644 index 0000000000000..b32451058803b --- /dev/null +++ b/Zend/tests/match/048.phpt @@ -0,0 +1,25 @@ +--TEST-- +Empty match subject with default "true" +--FILE-- + 'x', + default => 'y', +}); + +var_dump(match { + false => 'x', + !false => 'y', + default => 'z', +}); + +var_dump(match { + 1 > 2 => 'x', + 3 < 2 => 'y', + default => 'z', +}); +?> +--EXPECT-- +string(1) "x" +string(1) "y" +string(1) "z" diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y index d2a29e670d8bf..4b8310ef9a404 100644 --- a/Zend/zend_language_parser.y +++ b/Zend/zend_language_parser.y @@ -280,7 +280,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*); %type inline_function union_type_element union_type intersection_type %type attributed_statement attributed_class_statement attributed_parameter %type attribute_decl attribute attributes attribute_group namespace_declaration_name -%type match match_arm_list non_empty_match_arm_list match_arm match_arm_cond_list +%type match match_arm_list non_empty_match_arm_list match_arm match_arm_cond_list match_cond_subject %type enum_declaration_statement enum_backing_type enum_case enum_case_expr %type function_name non_empty_member_modifiers %type property_hook property_hook_list optional_property_hook_list hooked_property property_hook_body @@ -716,10 +716,14 @@ case_separator: | ';' ; +match_cond_subject: + %empty { zval z; ZVAL_TRUE(&z); $$ = zend_ast_create_zval(&z); } + | '(' expr ')' { $$ = $2; } +; match: - T_MATCH '(' expr ')' '{' match_arm_list '}' - { $$ = zend_ast_create(ZEND_AST_MATCH, $3, $6); }; + T_MATCH match_cond_subject '{' match_arm_list '}' + { $$ = zend_ast_create(ZEND_AST_MATCH, $2, $4); }; ; match_arm_list: From 8d7d9e68719665b5bb42d52f03bb170ce6118dba Mon Sep 17 00:00:00 2001 From: Dmitriy Derepko Date: Tue, 4 Feb 2025 10:32:06 +0300 Subject: [PATCH 2/2] test: move tests to a new file --- Zend/tests/match/050.phpt | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Zend/tests/match/050.phpt diff --git a/Zend/tests/match/050.phpt b/Zend/tests/match/050.phpt new file mode 100644 index 0000000000000..b32451058803b --- /dev/null +++ b/Zend/tests/match/050.phpt @@ -0,0 +1,25 @@ +--TEST-- +Empty match subject with default "true" +--FILE-- + 'x', + default => 'y', +}); + +var_dump(match { + false => 'x', + !false => 'y', + default => 'z', +}); + +var_dump(match { + 1 > 2 => 'x', + 3 < 2 => 'y', + default => 'z', +}); +?> +--EXPECT-- +string(1) "x" +string(1) "y" +string(1) "z"