Bug 14728 – Problems in the switch statement with enumerated data types
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-06-23T20:40:25Z
Last change time
2017-12-15T14:31:54Z
Assigned to
No Owner
Creator
dennis.m.ritchie
Comments
Comment #0 by dennis.m.ritchie — 2015-06-23T20:40:25Z
Found by Denis M...
https://vk.com/topic-471951_32042913
I think it can not write, but still it is a strange behavior and sometimes even compile.
`````
import std.stdio;
void main()
{
int x = 1;
long y = 2;
switch(x, y) {
case x : writeln("1"); break;
// Error: variable x cannot be read at compile time
case y : writeln("2"); break;
default: break;
}
}
`````
import std.stdio;
// compiles
void main()
{
int x = 1;
int y = 2;
switch(x, y) {
case x : writeln("1"); break;
case y : writeln("2"); break;
default: break;
}
}
I think that should be banned in the switch to send the enumerated arguments T...
Comment #1 by dlang-bugzilla — 2015-06-23T21:27:24Z
Here "," is the comma operator, which works as in C (i.e. the first expression is evaluated but its result is discarded). It is the same situation as e.g.
if (x, y) { ... }
The compiler should probably warn that the expression "x" has no side effects.
Comment #2 by dennis.m.ritchie — 2015-06-23T21:45:58Z
(In reply to Vladimir Panteleev from comment #1)
> Here "," is the comma operator, which works as in C (i.e. the first
> expression is evaluated but its result is discarded). It is the same
> situation as e.g.
>
> if (x, y) { ... }
>
> The compiler should probably warn that the expression "x" has no side
> effects.
Yes, probably, it would be nice if the compiler warns about this.
But I see the second problem. Why is it compiled?
int x = 1;
int y = 2;
but it is not compiled into a switch statement?
I think that this option should also be compiled:
int x = 1;
long y = 2;
Comment #3 by ali.akhtarzada — 2017-12-15T13:22:58Z
As of dmd v2.077.1 both scenarios compile and you get a "Deprecation: Using the result of a comma expression is deprecated"
Mark as FIXED?
Comment #4 by greensunny12 — 2017-12-15T14:31:54Z
> Mark as FIXED?
Yes I think so. Otherwise please reopen.
Btw it's since 2.074.1, see https://run.dlang.io/is/W3Ms3E (the All mode tests the code sample against all D compilers and shows a summary).