Skip to content

Commit 27d8896

Browse files
committed
Deprecate terminating case statements with semicolon
Part of https://wiki.php.net/rfc/deprecations_php_8_5 Closes GH-15258
1 parent aba6b89 commit 27d8896

File tree

6 files changed

+17
-11
lines changed

6 files changed

+17
-11
lines changed

Zend/zend_compile.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6303,6 +6303,11 @@ static void zend_compile_switch(zend_ast *ast) /* {{{ */
63036303
continue;
63046304
}
63056305

6306+
if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) {
6307+
CG(zend_lineno) = case_ast->lineno;
6308+
zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead");
6309+
}
6310+
63066311
zend_compile_expr(&cond_node, cond_ast);
63076312

63086313
if (expr_node.op_type == IS_CONST

Zend/zend_compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ ZEND_API zend_string *zend_type_to_string(zend_type type);
11261126
((ZEND_TYPE_FULL_MASK((arg_info)->type) & _ZEND_IS_TENTATIVE_BIT) != 0)
11271127

11281128
#define ZEND_DIM_IS (1 << 0) /* isset fetch needed for null coalesce. Set in zend_compile.c for ZEND_AST_DIM nested within ZEND_AST_COALESCE. */
1129+
#define ZEND_ALT_CASE_SYNTAX (1 << 1) /* deprecated switch case terminated by semicolon */
11291130

11301131
/* Attributes for ${} encaps var in strings (ZEND_AST_DIM or ZEND_AST_VAR node) */
11311132
/* ZEND_AST_VAR nodes can have any of the ZEND_ENCAPS_VAR_* flags */

Zend/zend_language_parser.y

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -713,15 +713,14 @@ switch_case_list:
713713

714714
case_list:
715715
%empty { $$ = zend_ast_create_list(0, ZEND_AST_SWITCH_LIST); }
716-
| case_list T_CASE expr case_separator inner_statement_list
716+
| case_list T_CASE expr ':' inner_statement_list
717717
{ $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_SWITCH_CASE, $3, $5)); }
718-
| case_list T_DEFAULT case_separator inner_statement_list
718+
| case_list T_CASE expr ';' inner_statement_list
719+
{ $$ = zend_ast_list_add($1, zend_ast_create_ex(ZEND_AST_SWITCH_CASE, ZEND_ALT_CASE_SYNTAX, $3, $5)); }
720+
| case_list T_DEFAULT ':' inner_statement_list
719721
{ $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_SWITCH_CASE, NULL, $4)); }
720-
;
721-
722-
case_separator:
723-
':'
724-
| ';'
722+
| case_list T_DEFAULT ';' inner_statement_list
723+
{ $$ = zend_ast_list_add($1, zend_ast_create_ex(ZEND_AST_SWITCH_CASE, ZEND_ALT_CASE_SYNTAX, NULL, $4)); }
725724
;
726725

727726

ext/opcache/tests/issue0057.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class ZException extends Exception {
1515
function dummy($query) {
1616
try {
1717
switch ($query) {
18-
case 1;
18+
case 1:
1919
break;
20-
case 2;
20+
case 2:
2121
break;
2222
default:
2323
throw new Exception('exception');

tests/lang/033.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ switch ($a):
3737
break;
3838
endswitch;
3939
?>
40-
--EXPECT--
40+
--EXPECTF--
41+
Deprecated: Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead in %s
4142
If: 11
4243
While: 12346789
4344
For: 0123401234

tests/lang/bug26696.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ for ($i = 0; $i < $len; $i++) {
1515

1616
$str = '*';
1717
switch ($str[0]) {
18-
case '*';
18+
case '*':
1919
echo "OK\n";
2020
break;
2121
default:

0 commit comments

Comments
 (0)