www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16030] New: bad error message when trying to use union in CTFE


          Issue ID: 16030
           Summary: bad error message when trying to use union in CTFE
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Keywords: diagnostic
          Severity: minor
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ag0aep6g gmail.com

Encountered by Stefan who posted to D.learn:
http://forum.dlang.org/post/gsnvzfrglquebmwqcmib forum.dlang.org

Reduced code:
union U
    int i;
    byte[4] b;

byte[4] f(int val)
    U u;
    u.i = val;
    return u.b;

static byte[4] forceCtfe = f(1); /* line 14 */

Fails compilation with:
test.d(14): Error: uninitialized variable 'i' cannot be returned from CTFE

I guess it's expected that unions don't work in CTFE, but the error message is

* There is no variable `i` on line 14. The function that actually fails CTFE
may be hidden anywhere in the call graph of `f`.
* The function tries to return `u.b`, not `u.i`.
* It's not obvious that "uninitialized" means "from a union".

May 16 2016