[Issue 18930] New: __ctfe fails to detect initialization of unions

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jun 1 03:23:50 UTC 2018


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

          Issue ID: 18930
           Summary: __ctfe fails to detect initialization of unions
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: johnnymarler at gmail.com

-------------------------------
union TwoInts
{
    struct
    {
        int _0;
        int _1;
    }
    int[2] array;
}
private int sum()
{
    auto twoValues = TwoInts();
    twoValues._0 = 100;
    twoValues._1 = 200;
    int x;
    foreach(i; 0..2)
    {
        x += twoValues.array[i];
    }
    return x;
}

//enum v = sum(); // ERROR

void main()
{
    import core.stdc.stdio;
    printf("sum is %d\n", sum());
}
-------------------------------

If you uncomment the `enum v = sum();` AND comment out the print line you'll
get this error:

bug.d(18): Error: `twoValues.array[cast(uint)i]` is used before initialized
bug.d(6):        originally uninitialized here
bug.d(23):        called from here: `sum()`

--


More information about the Digitalmars-d-bugs mailing list