digitalmars.D.bugs - [Issue 19150] New: Union member assignment causes invalid destructor
- d-bugmail puremagic.com (47/47) Aug 08 2018 https://issues.dlang.org/show_bug.cgi?id=19150
https://issues.dlang.org/show_bug.cgi?id=19150 Issue ID: 19150 Summary: Union member assignment causes invalid destructor call in safe code 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 The following code compiles without errors and produces an erroneous call to HasDtor's destructor: --- struct HasDtor { int n; ~this() safe { import std.stdio: writeln; writeln("Called destructor with n = ", n); } } union U { float n; HasDtor h; } safe void main() { U u = { n : 1 }; u.h = HasDtor(2); } --- Output: Called destructor with n = 1065353216 Because there is no way to know at compile time whether the union member being assigned to is valid, assignment to a member of a union that includes any members with destructors should be forbidden in safe code. In addition, since the language must rely on the programmer to manually destroy the appropriate union member in system code, the automatic destructor call is at best redundant and at worst undefined behavior. Therefore, it should be omitted. --
Aug 08 2018