Bug 16549 – Switch statement can skip initialization

Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-09-27T06:44:00Z
Last change time
2016-09-27T15:32:10Z
Assigned to
nobody
Creator
doob

Comments

Comment #0 by doob — 2016-09-27T06:44:29Z
Compiling and running the following code will trigger the assertion since "foo" is not initialized: struct State { int jumpLabel; } void bar(State s) { switch(s.jumpLabel) { int foo; case 0: assert(foo == foo.init); default: break; } } void main() { bar(State(0)); } It seems like it's the struct that is fooling the compiler. If it's replaced with a plain int, "foo" will be initialized.
Comment #1 by doob — 2016-09-27T06:51:41Z
Here's a very similar case where it fails to initialize "foo" as well: import std.stdio; struct State {} State bar(int jumpLabel) { switch (jumpLabel) { int foo; case 0: writeln(foo); default: break; } return State(); } void main() { auto state = bar(0); }
Comment #2 by mathias.lang — 2016-09-27T08:15:40Z
Compiling with master will give you a deprecation message: lol.d(8): Deprecation: 'switch' skips declaration of variable lol.bar.foo at lol.d(10)
Comment #3 by doob — 2016-09-27T14:15:32Z
(In reply to Mathias Lang from comment #2) > Compiling with master will give you a deprecation message: > > lol.d(8): Deprecation: 'switch' skips declaration of variable lol.bar.foo at > lol.d(10) Ah, yes. Is there a commit to refer to so we can close this. Or is this a duplicate?
Comment #4 by mathias.lang — 2016-09-27T15:32:10Z
I'll mark as duplicate. It was fixed by https://github.com/dlang/dmd/pull/5869 *** This issue has been marked as a duplicate of issue 14532 ***