[Issue 19122] Postblits and destructors called on members of anonymous unions

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue May 28 12:08:38 UTC 2019


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

Simen Kjaeraas <simen.kjaras at gmail.com> 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 <simen.kjaras at gmail.com> ---
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.
}

--


More information about the Digitalmars-d-bugs mailing list