www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19079] New: Destructors not called in named unions


          Issue ID: 19079
           Summary: Destructors not called in named unions
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: simen.kjaras gmail.com

When a type T with a destructor is placed in a named union, T's destructor is
not called when the union goes out of scope or is destroyed with destroy().

https://dlang.org/spec/struct.html states that 'Unions may not have fields that
have destructors', so this may be undefined behavior.

struct S  { ~this() { throw new Exception(""); } }
struct S1 { union { S s; } }
 union U  { S s; }
struct S2 { U u; }

unittest {
    import std.exception;

    // Correct behavior:
    assertThrown((){ S s; }());
    assertThrown((){ S s; destroy(s); }());
    assertThrown((){ S1 s1; }());
    assertThrown((){ S1 s1; destroy(s1); }());

    // Incorrect behavior:
    assertNotThrown((){ U u; }());
    assertNotThrown((){ U u; destroy(u); }());
    assertNotThrown((){ S2 s2; }());
    assertNotThrown((){ S2 s2; destroy(s2); }());

Jul 12 2018