Skip to content

Commit 1850785

Browse files
committed
Improve switch continue warning
Don't suggest "continue N+1" if there is no wrapping loop. The resulting code would be illegal.
1 parent d319098 commit 1850785

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

Zend/tests/continue_targeting_switch_warning.phpt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ function test() {
2222
}
2323
}
2424

25+
switch ($bar) {
26+
case 0:
27+
while ($xyz) {
28+
continue 2; // INVALID
29+
}
30+
case 1:
31+
while ($xyz) {
32+
continue;
33+
}
34+
case 2:
35+
while ($xyz) {
36+
break 2;
37+
}
38+
}
39+
2540
while ($foo) {
2641
switch ($bar) {
2742
case 0:
@@ -42,8 +57,10 @@ function test() {
4257

4358
?>
4459
--EXPECTF--
45-
Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in %s on line 6
60+
Warning: "continue" targeting switch is equivalent to "break" in %s on line 6
4661

4762
Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in %s on line 14
4863

49-
Warning: "continue 2" targeting switch is equivalent to "break 2". Did you mean to use "continue 3"? in %s on line 26
64+
Warning: "continue 2" targeting switch is equivalent to "break 2" in %s on line 25
65+
66+
Warning: "continue 2" targeting switch is equivalent to "break 2". Did you mean to use "continue 3"? in %s on line 41

Zend/zend_compile.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4891,15 +4891,26 @@ void zend_compile_break_continue(zend_ast *ast) /* {{{ */
48914891

48924892
if (CG(context).brk_cont_array[cur].is_switch) {
48934893
if (depth == 1) {
4894-
zend_error(E_WARNING,
4895-
"\"continue\" targeting switch is equivalent to \"break\". " \
4896-
"Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
4897-
depth + 1);
4894+
if (CG(context).brk_cont_array[cur].parent == -1) {
4895+
zend_error(E_WARNING,
4896+
"\"continue\" targeting switch is equivalent to \"break\"");
4897+
} else {
4898+
zend_error(E_WARNING,
4899+
"\"continue\" targeting switch is equivalent to \"break\". " \
4900+
"Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
4901+
depth + 1);
4902+
}
48984903
} else {
4899-
zend_error(E_WARNING,
4900-
"\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
4901-
"Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
4902-
depth, depth, depth + 1);
4904+
if (CG(context).brk_cont_array[cur].parent == -1) {
4905+
zend_error(E_WARNING,
4906+
"\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
4907+
depth, depth);
4908+
} else {
4909+
zend_error(E_WARNING,
4910+
"\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
4911+
"Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
4912+
depth, depth, depth + 1);
4913+
}
49034914
}
49044915
}
49054916
}

0 commit comments

Comments
 (0)