November 07, 2020
https://issues.dlang.org/show_bug.cgi?id=21367

          Issue ID: 21367
           Summary: Nameless union propagates copy constructors and
                    destructors over all members
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: regression
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: ilyayaroshenko@gmail.com

This is a very old regression.


struct RCArray(T)
{
    T* data;
    this(this){pragma(inline, false);}
    ~this()
    {
        pragma(inline, false);
    }
}

struct Variant(T...)
{
    union
    {
        T payload;
    }
    this(this){pragma(inline, false);}

    ~this()
    {
        pragma(inline, false);
    }
}

alias Ft = Variant!(RCArray!double, RCArray!int);

The quite old compilers (DMD v2.068.2 or LDC 0.17.2) generates a proper error
message

-------
<source>(11): Error: struct example.Variant!(RCArray!double,
RCArray!int).Variant destructors, postblits and invariants are not allowed in
overlapping fields __payload_field_0 and __payload_field_1
<source>(24): Error: template instance example.Variant!(RCArray!double,
RCArray!int) error instantiating
Compiler returned: 1
 Read the new cookie policy
-------


While the newer compilers generate wrong code that tries to destroy and postblit all union members:

pure nothrow @nogc @trusted void
example.Variant!(example.RCArray!(double).RCArray,
example.RCArray!(int).RCArray).Variant.__fieldDtor():
        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     rax, rdi
        mov     qword ptr [rbp - 8], rdi
        mov     rdi, rax
        call    pure nothrow @nogc @safe void
example.RCArray!(int).RCArray.__dtor()@PLT
        mov     rdi, qword ptr [rbp - 8]
        call    pure nothrow @nogc @safe void
example.RCArray!(double).RCArray.__dtor()@PLT
        add     rsp, 16
        pop     rbp
        ret

pure nothrow @nogc @safe void
example.Variant!(example.RCArray!(double).RCArray,
example.RCArray!(int).RCArray).Variant.__aggrDtor():
        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     qword ptr [rbp - 8], rdi
        call    pure nothrow @nogc @safe void
example.Variant!(example.RCArray!(double).RCArray,
example.RCArray!(int).RCArray).Variant.__dtor()@PLT
        mov     rdi, qword ptr [rbp - 8]
        call    pure nothrow @nogc @trusted void
example.Variant!(example.RCArray!(double).RCArray,
example.RCArray!(int).RCArray).Variant.__fieldDtor()@PLT
        add     rsp, 16
        pop     rbp
        ret

--