Skip to content

Commit 5f8d648

Browse files
authored
Deprecate terminating case statements with a semicolon (#19215)
Part of https://wiki.php.net/rfc/deprecations_php_8_5 Closes GH-15258
1 parent d01aa02 commit 5f8d648

File tree

8 files changed

+22
-11
lines changed

8 files changed

+22
-11
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ PHP NEWS
77
been deprecated. (Girgias)
88
. The $exclude_disabled parameter of the get_defined_functions() function has
99
been deprecated, as it no longer has any effect since PHP 8.0. (Girgias)
10+
. Terminating case statements with a semicolon instead of a colon has
11+
been deprecated. (theodorejb)
1012

1113
- DOM:
1214
. Fixed bug GH-18877 (\Dom\HTMLDocument querySelectorAll selecting only the

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ PHP 8.5 UPGRADE NOTES
322322
. The $exclude_disabled parameter of the get_defined_functions() function has
323323
been deprecated, as it no longer has any effect since PHP 8.0.
324324
RFC: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_the_exclude_disabled_parameter_of_get_defined_functions
325+
. Terminating case statements with a semicolon instead of a colon has
326+
been deprecated.
327+
RFC: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_semicolon_after_case_in_switch_statement
325328

326329
- FileInfo:
327330
. The finfo_close() function has been deprecated.

Zend/zend_compile.c

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

6316+
if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) {
6317+
CG(zend_lineno) = case_ast->lineno;
6318+
zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead");
6319+
}
6320+
63166321
zend_compile_expr(&cond_node, cond_ast);
63176322

63186323
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
@@ -1127,6 +1127,7 @@ ZEND_API zend_string *zend_type_to_string(zend_type type);
11271127
((ZEND_TYPE_FULL_MASK((arg_info)->type) & _ZEND_IS_TENTATIVE_BIT) != 0)
11281128

11291129
#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. */
1130+
#define ZEND_ALT_CASE_SYNTAX (1 << 1) /* deprecated switch case terminated by semicolon */
11301131

11311132
/* Attributes for ${} encaps var in strings (ZEND_AST_DIM or ZEND_AST_VAR node) */
11321133
/* 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)