www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22118] New: Const union causes false multiple-initialization

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

          Issue ID: 22118
           Summary: Const union causes false multiple-initialization error
                    in constructor
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: snarwin+bugzilla gmail.com

As of DMD 2.097.0, the following program fails to compile:

---
struct NeedsInit
{
    int n;
     disable this();
}

union U
{
    NeedsInit a;
}

struct S
{
    U u;
    this(const NeedsInit arg) const { u.a = arg; }
}
---

Compiler output, from run.dlang.io (with -vcolumns):

---
onlineapp.d(15,39): Error: const field `u` initialized multiple times
onlineapp.d(15,39):        Previous initialization is here.
---

The error message is false: there is only a single initialization of `u`.

Related: issue 21229.

--
Jul 11 2021