www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19919] New: Incorrect initialization of union when first

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

          Issue ID: 19919
           Summary: Incorrect initialization of union when first member
                    isn't marked = void
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: simen.kjaras gmail.com

unittest {
    union U {
        int n;
        float f = 3.14f;
    }
    U u;
    assert(u.f == 3.14f);
}

The above assert should pass, as U has exactly one explicit initializer.
Instead, the value of u.f is 0.0, because int.init is used instead. Another
option is that the above should fail to compile with an 'overlapping default
initialization' error.

If n is void-initialized, f is correctly set to 3.14f. If n is a type with an
initalizer other than 0, the initial value changes. (if n's initializer is
1078523331, the assert passes)

--
May 29 2019