Bug 22789 – Constructor flow analysis doesn't understand switch

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-02-18T17:42:13Z
Last change time
2024-12-13T19:21:00Z
Assigned to
No Owner
Creator
Paul Backus
Moved to GitHub: dmd#20054 →

Comments

Comment #0 by snarwin+bugzilla — 2022-02-18T17:42:13Z
As of DMD 2.098.1, the following program fails to compile: --- struct MustInit { string s; @disable this(); this(string s) { this.s = s; } } struct S { MustInit member; this(int n) { switch (n) { case 1: member = MustInit("one"); break; case 2: member = MustInit("two"); break; default: member = MustInit("many"); break; } } } --- However, the equivalent code using an if-else chain compiles successfully: --- struct MustInit { string s; @disable this(); this(string s) { this.s = s; } } struct S { MustInit member; this(int n) { if (n == 1) member = MustInit("one"); else if (n == 2) member = MustInit("two"); else member = MustInit("many"); } } --- In cases like this one, where the switch statement is equivalent to an if-else chain, the compiler should be able to analyze it the same way.
Comment #1 by robert.schadek — 2024-12-13T19:21:00Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20054 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB