https://issues.dlang.org/show_bug.cgi?id=19079
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: nob...@puremagic.com Reporter: simen.kja...@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); }()); } --