www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22789] New: Constructor flow analysis doesn't understand switch

https://issues.dlang.org/show_bug.cgi?id=22789

          Issue ID: 22789
           Summary: Constructor flow analysis doesn't understand switch
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: snarwin+bugzilla gmail.com

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.

--
Feb 18 2022