December 27, 2018

I noticed a bug with GDC that I cannot reproduce with other compilers. I can't seem to create an account on the Bugzilla, so I'm reporting it here.

It appears that when you have a union nested within a struct which itself is within a union, the union members get placed at position 0 of the struct, which is the same position as the first struct member. This obviously causes issues.

I can reproduce it with union > struct > union, but not struct > union or union > struct.

I created a minimal example that demonstrates the issue. The align(1) isn't required for the example to work, but I put it there to make sure output is consistent.

import std.stdio;

union foo {
    long ab;
    struct { align(1):
        int a;
        union {
            int b;

void main() {
    foo x;
    writefln("Position of a: %d, b: %d", cast(ulong)(&x.a) - cast(ulong)(&x), cast(ulong)(&x.b) - cast(ulong)(&x));

LDC, DMD, and GCC (with equivalent C code) all print
Position of a: 0, b: 4

GDC prints
Position of a: 0, b: 0

My GDC version is 8.2.0, and I am testing with optimizations off.

Thanks in advance for looking at this,
Marcus Stojcevich