[Issue 22789] New: Constructor flow analysis doesn't understand switch

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Feb 18 17:42:13 UTC 2022


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 at puremagic.com
          Reporter: snarwin+bugzilla at 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.

--


More information about the Digitalmars-d-bugs mailing list