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

Simen Kjaeraas <[email protected]> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Multiple destruction of     |Postblits and destructors
                   |union members               |called on members of
                   |                            |anonymous unions
           Severity|critical                    |blocker

--- Comment #4 from Simen Kjaeraas <[email protected]> ---
As pointed out in issue 19903, from the spec:

"Unions may have fields that have postblits. However, a union itself never has
a postblit. Copying a union does not result in postblit calls for any fields.
If those calls are desired, they must be inserted explicitly by the
programmer."

Also (https://dlang.org/spec/struct.html#struct-destructor):

"Unions may have fields that have destructors. However, a union itself never
has a destructor. When a union goes out of scope, destructors for it's fields
are not called. If those calls are desired, they must be inserted explicitly by
the programmer."

None of these hold for anonymous unions. I expect because their members are
placed in the struct definition (that has a destructor), rather than in a union
definition (that has no destructor).

Example showing both postblit and destructor being erroneously called:

struct HasDestructor {
    ~this() {
        ++d;
    }
    this(this) {
        ++p;
    }
}

struct S {
    union {
        int i;
        HasDestructor h;
    }
}

int d, p;

unittest {
    {
        S s;
        s = s;
        assert(p == 1); // Should fail.
    }
    assert(d > 1); // Should fail.
}

--

Reply via email to