Thread overview
[Issue 15205] Incorrect struct init via union array
Oct 15, 2015
ag0aep6g@gmail.com
Oct 27, 2015
Vladimir Panteleev
Dec 17, 2022
Iain Buclaw
October 15, 2015
https://issues.dlang.org/show_bug.cgi?id=15205

ag0aep6g@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
                 CC|                            |ag0aep6g@gmail.com
           Hardware|x86_64                      |All
                 OS|Windows                     |All

--
October 27, 2015
https://issues.dlang.org/show_bug.cgi?id=15205

Vladimir Panteleev <thecybershadow@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |thecybershadow@gmail.com
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=11269

--- Comment #1 from Vladimir Panteleev <thecybershadow@gmail.com> ---
Note: before https://github.com/D-Programming-Language/dmd/pull/2681 this caused a compile-time error:

test.d(29): Error: duplicate union initialization for v

Perhaps this should still result in an error due to incomplete implementation of unions in CTFE.

--
July 21, 2019
https://issues.dlang.org/show_bug.cgi?id=15205

skocznymr@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |skocznymr@gmail.com

--- Comment #2 from skocznymr@gmail.com ---
Encountered what seems to be the same issue. Reproduces on DMD 2.084.0 and LDC 1.13.0

import std.stdio;

struct Foo
{
    static Foo initializedFoo()
    {
        Foo x;
        x.a_array[0] = 1;
        return x;
    }

    union
    {
        float a;
        float[1] a_array;
    }
}

Foo bar = Foo.initializedFoo();

void main()
{
    writeln(bar.a);
    writeln(bar.a_array);
}

returns nan [nan] instead of 1 [1]

--
November 24, 2019
https://issues.dlang.org/show_bug.cgi?id=15205

--- Comment #3 from skocznymr@gmail.com ---
*** Issue 18313 has been marked as a duplicate of this issue. ***

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=15205

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--
November 05
https://issues.dlang.org/show_bug.cgi?id=15205

Steven Schveighoffer <schveiguy@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy@gmail.com

--- Comment #4 from Steven Schveighoffer <schveiguy@gmail.com> ---
Just helping out someone in discord, and ran into what looks like the same issue.

What seems to happen is if the non-first union member is a static array, and that array is set via indexing items, then it does not set the internal tag for the union during CTFE, so it doesn't think the second member is set, and instead writes out the first member.

Change option B to:

```d
v = v.init;
v[0] = x_;
v[1] = y_;
```

and it works. What I think happens is the assignment to v sets the union tag.

If you set v all at once, it also works:

```d
v = [x_, y_];
```

I think the compiler should detect the initialization via the indexed usage and set the tag properly.

--
December 13
https://issues.dlang.org/show_bug.cgi?id=15205

--- Comment #5 from dlangBugzillaToGithub <robert.schadek@posteo.de> ---
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/17728

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB

--