-
Notifications
You must be signed in to change notification settings - Fork 8k
RFC: Explicit fallthrough for switch()
#21821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
TimWolla
wants to merge
1
commit into
php:master
Choose a base branch
from
TimWolla:switch-case-fallthrough-warn
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| --TEST-- | ||
| Warning on switch case falling through | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function test($foo) { | ||
| switch ($foo) { | ||
| case 0: | ||
| case 1: | ||
| echo "a", PHP_EOL; | ||
| break; | ||
| case 2: | ||
| echo "b", PHP_EOL; | ||
| case 3: | ||
| echo "c", PHP_EOL; | ||
| break; | ||
| case 4: | ||
| echo "d", PHP_EOL; | ||
| fallthrough; | ||
| case 5: | ||
| echo "e", PHP_EOL; | ||
| break; | ||
| case 6: | ||
| echo "f", PHP_EOL; | ||
| return; | ||
| case 7: | ||
| echo "g", PHP_EOL; | ||
| continue; | ||
| case 8: | ||
| echo "h", PHP_EOL; | ||
| goto end; | ||
| case 9: | ||
| echo "i", PHP_EOL; | ||
| throw new \Exception("exception"); | ||
| } | ||
| end: | ||
| } | ||
|
|
||
| for ($i = 0; $i <= 9; $i++) { | ||
| try { | ||
| test($i); | ||
| } catch (\Throwable $e) { | ||
| echo $e::class, ": ", $e->getMessage(), PHP_EOL; | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Warning: Non-empty case falls through to the next case, terminate the case with "fallthrough;" if this is intentional in %s on line 9 | ||
|
|
||
| Warning: "continue" targeting switch is equivalent to "break" in %s on line 25 | ||
| a | ||
| a | ||
| b | ||
| c | ||
| c | ||
| d | ||
| e | ||
| e | ||
| f | ||
| g | ||
| h | ||
| i | ||
| Exception: exception |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,3 +141,5 @@ | |
| * @undocumentable | ||
| */ | ||
| const NULL = null; | ||
|
|
||
| const fallthrough = null; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit unfortunate this won't work for
if (foo) { return $x; } else { return $y; }(that's solvable), or functions with theneverreturn type that is officially supported to indicate exactly this (that's not solvable). A runtime check would solve that. Static analysis could bridge the gap to make this more ergonomic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We considered that, but felt that going down that route would require a full control-flow analysis here, since folks would rightfully expect nested
if()to work correctly as well. And perhaps alsoswitch()with adefault:. Or an exhaustive switch (which isn't solvable).In the end just adding a
break;below such anif()would also make it easier to scan the code because thecaseis clearly terminated on the expected nesting level.It would be solvable for internal functions (since they are guaranteed to be available at compile time), but not for userland functions.
I also was a little annoyed needing to put a
break;afterexit();, but overall functions returning: neverare few and far-between. It's really onlyexit()and perhapspcntl_exec(), if the latter would use exceptions.I could add support for internal functions being
: never, but I'm not sure if this would add or remove to the consistency 🤔Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unreachable
breaks can also be misleading though.throw new UnreachableError();would be better, but this error is not built-in.assert(0, "Unreachable");also won't work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TimWolla I think I agree that emitting a warning at runtime / throwing an exception when
fallthroughis missing is more reasonable for PHP.It also prevents you from putting
breakthere to make the runtime happy, and it actually going down the wrong path accidentally, because it did not actually throw/return/whatever due to a bug.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't understand what you're trying to say there. That's worded too abstract.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically "insert a (runtime) warning/throw exception before any case statement". Have
fallthrough;jump over this.So, if normal control flow reaches that code path, due to a missing fallthrough or missing break, or return or whatever, there's a warning/exception emitted.