Thread overview
[Issue 19903] postblit called for uninitialised elements of unions
May 26, 2019
Manu
May 27, 2019
Manu
May 28, 2019
RazvanN
May 28, 2019
Simen Kjaeraas
May 26, 2019
https://issues.dlang.org/show_bug.cgi?id=19903

--- Comment #1 from Manu <turkeyman@gmail.com> ---
Oh my, I just noticed that the DESTRUCTOR is also called for each element in
the union!
That's completely wrong, since only one item in the union may be valid at a
time.

--
May 27, 2019
https://issues.dlang.org/show_bug.cgi?id=19903

Manu <turkeyman@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|major                       |blocker

--- Comment #2 from Manu <turkeyman@gmail.com> ---
Raising this to blocker. unions are completely useless in D with this issue in place, and I can't continue.

--
May 28, 2019
https://issues.dlang.org/show_bug.cgi?id=19903

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305@gmail.com

--- Comment #3 from RazvanN <razvan.nitu1305@gmail.com> ---
(In reply to Manu from comment #0)

> This assignment calls the T postblit, which is a `void` initialised member
> of a union.
> This is just a hard crash waiting to happen!
> 
> This pattern should be supplanted with copy-ctors.
> I suggest a reasonable resolution is:
>   1. no such implicit non-trivial assignment is attempted inside a union
>   2. if a union contains an element with a non-trivial copy, then a
> copy-ctor must be defined otherwise user receives a compile error informing
> them such.
>     2a. perhaps rather than emitting a compile error, it might be better to
> emit an automatic @disable copy constructor for that object.
> 
> I think it's the case that any union with elaborate copy semantics can only be correctly composed by the author of the union.

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."

It seems that it has a problem with anonymous unions. Slightly changing your program results in corect behavior:

import std.stdio;

    struct T
    {
        this(this) { writeln("dun postblit\n"); }
        float f = 0;
    }

    struct S
    {
        union U {    // the union is named now
            int x = 0;
            T y = void; // <- void initialised
        }
        U u;  // and we have a field
        int which = 0;
    }

    S a;
    S b = a; // <- assignment calls postblit

--
May 28, 2019
https://issues.dlang.org/show_bug.cgi?id=19903

Simen Kjaeraas <simen.kjaras@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |simen.kjaras@gmail.com
         Resolution|---                         |DUPLICATE

--- Comment #4 from Simen Kjaeraas <simen.kjaras@gmail.com> ---


*** This issue has been marked as a duplicate of issue 19122 ***

--