Skip to content

Commit 082dd45

Browse files
committed
Changed match rules to prohibit sharing conditions with the default arm.
1 parent 04ccf4c commit 082dd45

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Tests declaring a default match arm adjacent to another condition is still prohibited in an argument context
3+
--FILE--
4+
<?php
5+
6+
function F($V = 1) {}
7+
F(
8+
match (0) {
9+
0,
10+
default => 1,
11+
}
12+
);
13+
?>
14+
--EXPECTF--
15+
Fatal error: Match arms may not share conditions with the default arm in %s on line %d

Zend/tests/default_expression/match_multiple_default.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ Tests declaring multiple default match arms is still prohibited in an argument c
66
function F($V = 1) {}
77
F(
88
match (0) {
9-
0,
109
default => 1,
1110
default => 2,
1211
}

Zend/zend_compile.c

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6375,18 +6375,6 @@ static void zend_compile_switch(zend_ast *ast) /* {{{ */
63756375
}
63766376
/* }}} */
63776377

6378-
static bool do_match_conditions_contain_default(zend_ast_list *conditions)
6379-
{
6380-
for (uint32_t i = 0; i < conditions->children; ++i) {
6381-
// Default as an expression is still allowed, so we don't need to recurse sub-lists.
6382-
if (conditions->child[i]->kind == ZEND_AST_DEFAULT) {
6383-
return true;
6384-
}
6385-
}
6386-
6387-
return false;
6388-
}
6389-
63906378
static uint32_t count_match_conds(zend_ast_list *arms)
63916379
{
63926380
uint32_t num_conds = 0;
@@ -6395,7 +6383,7 @@ static uint32_t count_match_conds(zend_ast_list *arms)
63956383
zend_ast *arm_ast = arms->child[i];
63966384
zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
63976385

6398-
if (do_match_conditions_contain_default(conds)) {
6386+
if (conds->child[0]->kind == ZEND_AST_DEFAULT) {
63996387
continue;
64006388
}
64016389

@@ -6410,7 +6398,7 @@ static bool can_match_use_jumptable(zend_ast_list *arms) {
64106398
zend_ast *arm_ast = arms->child[i];
64116399
zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
64126400

6413-
if (do_match_conditions_contain_default(conds)) {
6401+
if (conds->child[0]->kind == ZEND_AST_DEFAULT) {
64146402
/* Skip default arm */
64156403
continue;
64166404
}
@@ -6457,13 +6445,20 @@ static void zend_compile_match(znode *result, zend_ast *ast)
64576445
zend_ast *arm_ast = arms->child[i];
64586446
zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
64596447

6460-
if (do_match_conditions_contain_default(conds)) {
6461-
if (has_default_arm) {
6462-
CG(zend_lineno) = arm_ast->lineno;
6463-
zend_error_noreturn(E_COMPILE_ERROR,
6464-
"Match expressions may only contain one default arm");
6448+
for (uint32_t j = 0; j < conds->children; ++j) {
6449+
if (conds->child[j]->kind == ZEND_AST_DEFAULT) {
6450+
if (conds->children > 1) {
6451+
zend_error_noreturn(E_COMPILE_ERROR,
6452+
"Match arms may not share conditions with the default arm");
6453+
}
6454+
6455+
if (has_default_arm) {
6456+
CG(zend_lineno) = arm_ast->lineno;
6457+
zend_error_noreturn(E_COMPILE_ERROR,
6458+
"Match expressions may only contain one default arm");
6459+
}
6460+
has_default_arm = 1;
64656461
}
6466-
has_default_arm = 1;
64676462
}
64686463
}
64696464

@@ -6490,7 +6485,7 @@ static void zend_compile_match(znode *result, zend_ast *ast)
64906485
for (uint32_t j = 0; j < conds->children; j++) {
64916486
zend_ast *cond_ast = conds->child[j];
64926487

6493-
if (do_match_conditions_contain_default(conds)) {
6488+
if (conds->child[0]->kind == ZEND_AST_DEFAULT) {
64946489
break;
64956490
}
64966491

@@ -6547,7 +6542,7 @@ static void zend_compile_match(znode *result, zend_ast *ast)
65476542
zend_ast *body_ast = arm_ast->child[1];
65486543
zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
65496544

6550-
if (!do_match_conditions_contain_default(conds)) {
6545+
if (conds->child[0]->kind != ZEND_AST_DEFAULT) {
65516546
for (uint32_t j = 0; j < conds->children; j++) {
65526547
zend_ast *cond_ast = conds->child[j];
65536548

0 commit comments

Comments
 (0)