3 days ago
https://issues.dlang.org/show_bug.cgi?id=24864

          Issue ID: 24864
           Summary: hasElaborateDestructor incorrectly true for structs
                    with anonymous unions
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: druntime
          Assignee: nobody@puremagic.com
          Reporter: issues.dlang@jmdavisProg.com

This code

---
void main()
{
    import std.traits;

    static struct Member
    {
        ~this() {}
    }

    static struct S
    {
        union
        {
            Member member;
            int i;
        }
    }

    static assert(!hasElaborateDestructor!S);
}
---

fails to compile, because the static assertion fails. However, S does not have a destructor. Member does, and member is a member variable of S, but it's within a union, so S itself doesn't get a destructor, and member's destructor is not supposed to be called - and if you do something like

---
void main()
{
    static struct Member
    {
        ~this() { assert(false); }
    }

    static struct S
    {
        union
        {
            Member member;
            int i;
        }
    }

    S s;
}
---

the assertion does not fail, because S does not have a destructor, and therefore, member's destructor is not called.

Of course, well-written code would keep track of which union member is the valid one and call destroy on member when appropriate from inside a user-defined destructor on S, but that's irrelevant to the question of whether S as presented here has elaborate destruction. It doesn't, and as such, hasElaborateDestructor shouldn't claim that it does.

--