www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19141] New: Destructors are called for multiple anonymous

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

          Issue ID: 19141
           Summary: Destructors are called for multiple anonymous union
                    members
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: snarwin+bugzilla gmail.com

Example code:

---
import std.stdio: writeln;

struct S1
{
        ~this() { writeln("Destroying S1"); }
}

struct S2
{
        ~this() { writeln("Destroying S2"); }
}

struct S3
{
        union
        {
                S1 one;
                S2 two;
        }

        ~this() { writeln("Destroying S3"); }
}

void main()
{
        S3 three;
}
---

Output:

Destroying S3
Destroying S2
Destroying S1


Since `one` and `two` are members of the same union, calling both destructors
will always lead to undefined behavior.

Changing the anonymous union to a named union causes both the S1 and S2
destructor calls to be omitted.

Possibly related to Issue 4421.

--
Aug 05 2018