Re: Adding explicit intent for SWITCH/CASE fall through?

From: Date: Fri, 18 Oct 2019 03:14:09 +0000
Subject: Re: Adding explicit intent for SWITCH/CASE fall through?
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-107571@lists.php.net to get a copy of this message
On Thu, Oct 17, 2019 at 4:54 PM Mike Schinkel <mike@newclarity.net> wrote: > Before creating an RFC I wanted to get reactions to the idea of adding > FALLTHROUGH option to SWITCH/CASE for when the developer explicitly want > logic to fall through to the next case and does not want to use a BREAK. > > My simples motivation for this feature is that my IDE (PhpStorm) always > flags CASE statements w/o BREAKs because it cannot know when I intend for > the logic to fallthrough and not break. If there was an optional > FALLTHROUGH then my IDE could flag any CASE statements that do not have a > FALLTHROUGH or BREAK — which would indicate an error of my intent — and > avoid flagging all the situations where I intentionally want it to fall > through. > > Beyond that, this might also be useful for other static analysis tools > such as PhpStan. > Ust > Additionally it could be added in 8.0 and then in 8.1 flagged with a > warning when a CASE does not have one for the two, but only if people don't > object to this. While I know some on this list feel strongly that warnings > must come with to future plans to generate errors I would personally be > 100% okay if it indefinitely generated only a warning. > > In summary I want a mechanism to be allow me to explicitly indicate my > intent with respect to SELECT/CASE statements. What do you think? Is this > worthy of an RFC, or is there some reason a majority would object to such > an addition? > +1. It's 100% opt-in. People can keep using implicit fall-through, their own /* fallthrough */ comments, or the new token. There's virtually no overhead in the compile phase and it doesn't mess with the jump table optimizations. There's precedent for this desire in PHP static analyzers [1] as well as other major projects: eg, the Linux Foundation just completed a 2 year project to remove implicit fall-through in the Linux kernel [2] (*). That said, some questions: Do you envision this token accepting an optional argument, as break does, to fall-through multiple nesting levels? Eg: <?php $x = $y = $z = 1; switch ($x) { case 1: switch ($y) { case 1: switch ($z) { case 1: fallthrough 3; case 2: echo 'z2'; } } break; case 2: echo 'x2'; break; } ?> What do you envision the result of indicating fall-through to no subsequent case? Eg: <?php switch ($x) { case 1: fallthrough; } ?> Would a "pragma" (of sorts) be a part of the concept? <?php declare(strict_break=1); switch ($x) { case 1: echo 'x1'; // ImplicitBreakError thrown here case 2: echo 'x2'; } ?> [1]:https://github.com/phan/phan/issues/2550 [2]:https://lwn.net/Articles/794944/ (*) Fun trivia: PHP is mentioned in that LWN article about naming conventions.

« previous php.internals (#107571) next »