Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
April 09, 2007 [Issue 1118] New: weird switch statement behaviour | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=1118 Summary: weird switch statement behaviour Product: D Version: 1.010 Platform: PC OS/Version: Linux Status: NEW Keywords: accepts-invalid, spec Severity: normal Priority: P2 Component: DMD AssignedTo: bugzilla@digitalmars.com ReportedBy: manuelk89@gmx.net The documentation for the switch statement says: SwitchStatement: switch ( Expression ) ScopeStatement So there must not be 'case', 'default' or scoping brackets after the switch. Hence this would be valid code (and actually compiles, throwing a 'Switch Default' error for the first example): switch (1) for (int i=0; i<5; i++) writefln(i); // another, yet acceptable but ugly looking example example switch (true) case true: writefln("foo"); switch (5) { // do anything but no switch/case writefln("foo"); } But beside a ScopeStatement, even a normal Statement gets accepted by the compiler (at least I could not figure out a transition from a ScopeStatement to an ExpressionStatement): switch(2) writefln("foo"); _________________________________________________________________ Examples were tested on Ubuntu Linux with * dmd 1.010 * gdc 0.23 _________________________________________________________________ PS: I think a definition like this would do the job: SwitchStatement: switch ( Expression ) { SwitchItemList } SwitchItemList: SwitchItem SwitchItem SwitchItemList SwitchItem: CaseStatement DefaultStatement -- |
April 09, 2007 [Issue 1118] weird switch statement behaviour | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=1118 shro8822@uidaho.edu changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |shro8822@uidaho.edu ------- Comment #1 from shro8822@uidaho.edu 2007-04-09 18:06 ------- What about cases like this: switch(i) { while(i) { foo(); case 0: bar(); case 1: baz(); case 3: i--; } } -- |
April 09, 2007 Re: [Issue 1118] weird switch statement behaviour | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail |
> What about cases like this:
>
> switch(i)
> {
> while(i)
> {
> foo();
> case 0: bar();
> case 1: baz();
> case 3: i--;
> }
> }
>
>
Nice idea, never had to use it that way. Think this issue should be tagged as INVALID...
But looking at the examples given before, it would be nice if the compiler would give at least a warning about missing switch labels.
|
April 09, 2007 [Issue 1118] weird switch statement behaviour | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=1118 ------- Comment #3 from shro8822@uidaho.edu 2007-04-09 18:41 ------- Not having a case might not be a problem: tuples can be foreached to generate cases and a zero length tuple might be valid. You would however expect their to be a default in that case. int Foo(A...)(int i) { switch(i) { default: // code break; foreach(a;A) { case a: // code // break; } } } -- |
April 10, 2007 Re: [Issue 1118] weird switch statement behaviour | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | > ------- Comment #3 from shro8822@uidaho.edu 2007-04-09 18:41 -------
> Not having a case might not be a problem: tuples can be foreached to generate
> cases and a zero length tuple might be valid. You would however expect their to
> be a default in that case.
>
> int Foo(A...)(int i)
> {
> switch(i)
> {
> default:
> // code
> break;
> foreach(a;A)
> {
> case a:
> // code
> // break;
> }
> }
> }
>
>
Wow, again I'm amazed by D's features! But I could not compile your code (yet a real bug :P ). But I could find a workaround:
import std.stdio;
// your version (should work, but it doesn't)
void Foo1(A...)(int i)
{
switch (i)
{
foreach(a; A)
{
case a: // line 9
writefln(a);
}
}
}
// workaround (does exactly the same thing, but with more clumsy code)
void Foo2(A...)(int i)
{
switch (i)
{
foreach(j, a; A)
{
case A[j]:
writefln(a);
}
}
}
void main()
{
//Foo1!(1,2,3,4,5)(1); // line 29
Foo2!(1,2,3,4,5)(1);
}
The output is
1
2
3
4
5
just as expected. But uncommenting Foo1 gives an error. Error log from Code::Blocks:
hello.d:9: Error: case must be a string or an integral constant, not a
hello.d:9: Error: case must be a string or an integral constant, not a
hello.d:9: Error: duplicate case 0 in switch statement
hello.d:9: Error: case must be a string or an integral constant, not a
hello.d:9: Error: duplicate case 0 in switch statement
hello.d:9: Error: case must be a string or an integral constant, not a
hello.d:9: Error: duplicate case 0 in switch statement
hello.d:9: Error: case must be a string or an integral constant, not a
hello.d:9: Error: duplicate case 0 in switch statement
hello.d:29: template instance hello.Foo1!(1,2,3,4,5) error instantiating
:: === Build finished: 10 errors, 0 warnings ===
I think that's worth a bug report.
|
July 01, 2007 [Issue 1118] weird switch statement behaviour | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=1118 bugzilla@digitalmars.com changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |FIXED ------- Comment #5 from bugzilla@digitalmars.com 2007-07-01 13:29 ------- Fixed DMD 1.018 and DMD 2.002 -- |
Copyright © 1999-2021 by the D Language Foundation