[Issue 15205] Incorrect struct init via union array

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Nov 5 17:33:44 UTC 2024


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

Steven Schveighoffer <schveiguy at gmail.com> changed:

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

--- Comment #4 from Steven Schveighoffer <schveiguy at 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.

--


More information about the Digitalmars-d-bugs mailing list