Switch case falltrhough, regression or intended behavior ?

Jonathan M Davis jmdavisProg at gmx.com
Sun Feb 17 17:30:37 PST 2013


On Monday, February 18, 2013 01:04:41 Stewart Gordon wrote:
> On 17/02/2013 20:07, Jonathan M Davis wrote:
> <snip>
> 
> >> Implicit fall through shouldn't have been allowed from the beginning.  It
> >> would appear that this has finally been banned.
> > 
> > Implicit fallthrough is a warning when a case stament is non-empty, but if
> > it's empty (as in the example), then there is no warning.
> 
> What version of DMD are you using?

That's irrelevant. I'm stating what the intended behavior is. If the compiler 
doesn't follow that behavior, then it's a bug.

> You see, it's independent of whether there are any statements between the
> two case markers,

Which is a bug.

> and there's a further anomaly in that it's an _error_
> that's emitted only if _warnings_ are enabled.

That's normal. That's what -w does. If you don't use any compiler flags, then 
you get no warnings. If you use -w, you get them, but they're treated as 
errors. If you use -wi, then you get "informational warnings" which is what 
most everyone expects compilers to do normally - i.e. print the warning but 
not treat it as an error - but Walter Bright thinks differently about such 
things than many people, and his compilers often don't do what many people 
would consider normal. He doesn't believe in warnings in the first place. 
Rather, he thinks that everything should be an error or not as far as the 
compiler is concerned (which I very much agree with), but unfortunately, he 
gave in enough to create the -w flag (and eventually, the -wi flag), and when he 
did, he didn't follow the behavior of your average compiler, which has 
generated a fair bit of confusion with regards to warnings.

> > If the compiler is warning about falling through an empty case statement,
> > it's a bug.
> There's no such thing as an "empty case statement", unless you mean the case
> where the ScopeStatementList is ";" or "{}" by itself.  Look at the grammar
> carefully.
> 
> So by the current spec, the first example is a syntax error, since a
> CaseStatement explicitly forbids another CaseStatement as its body.

I mean a case statement with no statements in it. The only thing following it 
is another case statement or the closing brace of the switch statement. That 
sort of fallthrough is not supposed to generate a warning. If the grammar 
claims anything about empty case statements being illegal, then the spec is in 
error (which happens far too often).

Walter has been very clear on the intended behavior. It's a warning to have 
implicit fallthrough except in the situation where the case statement is empty 
(because it would be very annoying to have to put a whole lot of gotos in case 
statements which are all supposed to shared the same code by falling through). 
If the compiler or grammar disagree with that, then it's a bug.

Actually, looking at the most recent compiler, it's generating a deprecation 
message now, so it's been moved from warning to deprecated, and it _is_ 
generating the message for an empty case statement, which is wrong.

- Jonathan M Davis


More information about the Digitalmars-d mailing list