www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14862] New: Constructor of overlapped struct does not

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

          Issue ID: 14862
           Summary: Constructor of overlapped struct does not initialize
                    correctly global variables
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: blocker
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: rumbu rumbu.ro

struct S
{
    union
    {
        struct { uint hi, lo; }
        ulong data;
    }

    this(ulong data)
    {
        this.data = data;
    }
}

immutable S sglobal = S(123UL); //not run
enum S senum = S(123UL);

void main()
{
   S slocal = S(123UL);
   assert(slocal.data == 123UL);
   assert(senum.data == 123UL);
   assert(sglobal.data == 123UL); //fail, in fact it's 0
}

BUT by inversing the overlapped fields, the struct it's initialized properly:

struct S
{
    union
    {
        ulong data;     
        struct { uint hi, lo; }
    }
}

--
Aug 02 2015