Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 14, 2011 Should the 2.054 feature about warning on implicit fallthrough include labels? | ||||
---|---|---|---|---|
| ||||
Not sure if this is a bug or as intended. import std.stdio; void main() { int i = 1; switch(i) { case 0: writeln("case 0"); goto default; // needed here default: writeln("default"); // But always falls through here aLabel: writeln("a label"); } } |
September 14, 2011 Re: Should the 2.054 feature about warning on implicit fallthrough include | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | simendsjo Wrote: > Not sure if this is a bug or as intended. The semantics of switch is a mess (example: see http://d.puremagic.com/issues/show_bug.cgi?id=3820 ). Mixing labels and switch cases seems a good way to create a bigger mess. If I invert some things in your code I get an error... import std.stdio; void main() { int i = 1; switch(i) { case 0: writeln("case 0"); goto default; // needed here aLabel: writeln("a label"); default: writeln("default"); // But always falls through here } } Bye, bearophile |
September 14, 2011 Re: Should the 2.054 feature about warning on implicit fallthrough include labels? | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On 14.09.2011 22:52, simendsjo wrote: > Not sure if this is a bug or as intended. > > import std.stdio; > void main() { > int i = 1; > switch(i) { > case 0: > writeln("case 0"); > goto default; // needed here > default: > writeln("default"); > // But always falls through here > aLabel: > writeln("a label"); > } > } Label doesn't affect switch statement in any way, e.g. it's not part of case x: synatx, and as such is "fallthrough" like in normal code. So this part works as intended, though I agree with bearophile there are some messy things w.r.t. switch. -- Dmitry Olshansky |
September 14, 2011 Re: Should the 2.054 feature about warning on implicit fallthrough include | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 09/14/2011 09:11 PM, bearophile wrote: > simendsjo Wrote: > >> Not sure if this is a bug or as intended. > > The semantics of switch is a mess (example: see http://d.puremagic.com/issues/show_bug.cgi?id=3820 ). > Mixing labels and switch cases seems a good way to create a bigger mess. I think it is a bit mean to say the whole semantics is a mess, because there is an accepts-invalid bug. ;) What is really nice about D's case labels, is that they introduce a new scope. (as opposed to C) Unless they are mixed in: http://d.puremagic.com/issues/show_bug.cgi?id=6590 . switch statements and gotos provide nice means of writing highly efficient code that still looks somewhat structured. I don't agree that using labels inside a switch creates a mess. > > If I invert some things in your code I get an error... > > import std.stdio; > void main() { > int i = 1; > switch(i) { > case 0: > writeln("case 0"); > goto default; // needed here > aLabel: > writeln("a label"); > default: > writeln("default"); > // But always falls through here > } > } > > Bye, > bearophile Well, yes, but that is as expected. Would you expect a fall through error on this code? writeln(1); label: writeln(2); labeled statements and case/default statements are not the same thing. |
September 14, 2011 Re: Should the 2.054 feature about warning on implicit fallthrough include labels? | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On 09/14/2011 08:52 PM, simendsjo wrote:
> Not sure if this is a bug or as intended.
>
> import std.stdio;
> void main() {
> int i = 1;
> switch(i) {
> case 0:
> writeln("case 0");
> goto default; // needed here
> default:
> writeln("default");
> // But always falls through here
> aLabel:
> writeln("a label");
> }
> }
This is as intended. Labels and case/default statements are not the same thing. As an example, case/default statements introduce a new scope, while labels do not.
|
September 14, 2011 Re: Should the 2.054 feature about warning on implicit fallthrough | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | Timon Gehr: > I think it is a bit mean to say the whole semantics is a mess, The original C design is a mess with several traps, and successive things added in D to the switch have increased the mess, they were designed from very narrow points of view, with a lack of global vision: 1) The case x: .. case y: syntax is ugly, and it doesn't respect the semantics of the .. in D that means a range open on the right. 2) The introduction of final switches is badly designed and not complete, so the safety of the situation is not improved: http://d.puremagic.com/issues/show_bug.cgi?id=5713 3) The recent improvements (that are patches on the stupid semantics of the C switch) are buggy and need a patch: https://github.com/D-Programming-Language/dmd/pull/370 Switch is too much limited, ignoring other needs: http://d.puremagic.com/issues/show_bug.cgi?id=596 I'll never agree that the C switch is well (or even "well enough") designed. Even a simple language as Pascal gets the case-of statement better than C. > switch statements and gotos provide nice means of writing highly efficient code that still looks somewhat structured. I'd like computed gotos in D. I have discussed with Walter often about this, but he doesn't listen much on this. > Well, yes, but that is as expected. Would you expect a fall through error on this code? Have you run that code? Bye, bearophile |
September 14, 2011 Re: Should the 2.054 feature about warning on implicit fallthrough | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 15.09.2011 0:51, bearophile wrote: > Timon Gehr: > >> I think it is a bit mean to say the whole semantics is a mess, > > The original C design is a mess with several traps, and successive things added in D to the switch have increased the mess, they were designed from very narrow points of view, with a lack of global vision: > > 1) The case x: .. case y: syntax is ugly, and it doesn't respect the semantics of the .. in D that means a range open on the right. > > 2) The introduction of final switches is badly designed and not complete, so the safety of the situation is not improved: > http://d.puremagic.com/issues/show_bug.cgi?id=5713 > > 3) The recent improvements (that are patches on the stupid semantics of the C switch) are buggy and need a patch: > https://github.com/D-Programming-Language/dmd/pull/370 > > Switch is too much limited, ignoring other needs: > http://d.puremagic.com/issues/show_bug.cgi?id=596 > > I'll never agree that the C switch is well (or even "well enough") designed. Even a simple language as Pascal gets the case-of statement better than C. > It depends on what you need it to do. If all you need is jump table then C switch is your best friend. > >> switch statements and gotos provide nice means of writing highly >> efficient code that still looks somewhat structured. > > I'd like computed gotos in D. I have discussed with Walter often about this, but he doesn't listen much on this. > +1. > >> Well, yes, but that is as expected. Would you expect a fall through >> error on this code? > > Have you run that code? > > Bye, > bearophile -- Dmitry Olshansky |
September 14, 2011 Re: Should the 2.054 feature about warning on implicit fallthrough | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 09/14/2011 10:51 PM, bearophile wrote: > Timon Gehr: > >> I think it is a bit mean to say the whole semantics is a mess, > > The original C design is a mess with several traps, and successive things added in D to the switch have increased the mess, they were designed from very narrow points of view, with a lack of global vision: > > 1) The case x: .. case y: syntax is ugly, and it doesn't respect the semantics of the .. in D that means a range open on the right. > That's subjective of course. It does not strike me as particularly ugly. (if you'd rather see case x .. y+1:, I think that is exceptionally ugly). The fact that the last case is included is a move to make it more useful. What would you suggest? > 2) The introduction of final switches is badly designed and not complete, so the safety of the situation is not improved: > http://d.puremagic.com/issues/show_bug.cgi?id=5713 > Yah, that is quite messy, but I think that can be fixed. > 3) The recent improvements (that are patches on the stupid semantics of the C switch) are buggy and need a patch: > https://github.com/D-Programming-Language/dmd/pull/370 QOI issue, not a fundamental design flaw. I am sure the patch will be applied. > > Switch is too much limited, ignoring other needs: > http://d.puremagic.com/issues/show_bug.cgi?id=596 > I agree that it would be nice for switch to work on other types. The other needs are better addressed by the introduction of a match expression. > I'll never agree that the C switch is well (or even "well enough") designed. Even a simple language as Pascal gets the case-of statement better than C. > 'C switch' means 'jump table'. It does do that perfectly. ;) > >> switch statements and gotos provide nice means of writing highly >> efficient code that still looks somewhat structured. > > I'd like computed gotos in D. I have discussed with Walter often about this, but he doesn't listen much on this. > +1. > >> Well, yes, but that is as expected. Would you expect a fall through >> error on this code? > > Have you run that code? > What is your point? |
September 14, 2011 Re: Should the 2.054 feature about warning on implicit fallthrough | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | Timon Gehr: > What would you suggest? At the moment I suggest nothing, because the situation is set. Case syntax was discussed a lot, by me too. I suggested to differentiate the syntax, not using ".." because in D they denote an interval open on the right. > 'C switch' means 'jump table'. It does do that perfectly. ;) C language is full of badly designed parts :-) > What is your point? Well, I don't understand the error it gives :-) Are you able to explain it to me? import std.stdio; void main() { int i = 1; switch(i) { case 0: writeln("case 0"); goto default; // needed here aLabel: writeln("a label"); default: writeln("default"); // But always falls through here } } test.d(10): Error: switch case fallthrough - use 'goto default;' if intended Bye, bearophile |
September 14, 2011 Re: Should the 2.054 feature about warning on implicit fallthrough | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 09/15/2011 12:43 AM, bearophile wrote:
> Timon Gehr:
>
>> What would you suggest?
>
> At the moment I suggest nothing, because the situation is set.
>
> Case syntax was discussed a lot, by me too. I suggested to differentiate the syntax, not using ".." because in D they denote an interval open on the right.
>
>
>> 'C switch' means 'jump table'. It does do that perfectly. ;)
>
> C language is full of badly designed parts :-)
>
>
>> What is your point?
>
> Well, I don't understand the error it gives :-) Are you able to explain it to me?
>
>
> import std.stdio;
> void main() {
> int i = 1;
> switch(i) {
> case 0:
> writeln("case 0");
> goto default; // needed here
> aLabel:
> writeln("a label");
> default:
> writeln("default");
> // But always falls through here
> }
> }
>
> test.d(10): Error: switch case fallthrough - use 'goto default;' if intended
>
> Bye,
> bearophile
import std.stdio;
void main() {
int i = 1;
switch(i) {
case 0:
writeln("case 0");
// goto default; // NOT needed here (for it to compile)
aLabel:
writeln("a label");
goto default; // explicit fall through
default:
writeln("default");
// But always falls through here
}
}
And that is exactly the same as this:
import std.stdio;
void main() {
int i = 1;
switch(i) {
case 0:
writeln("case 0");
writeln("a label");
goto default; // comment this out to get your error back.
default:
writeln("default");
}
}
It is a simple case of switch case fallthrough.
|
Copyright © 1999-2021 by the D Language Foundation