www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15741] New: A combination of union, pointer type parameter,


          Issue ID: 15741
           Summary: A combination of union,  pointer type parameter, and
                    recursive call prevent CTFEability
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ttanjo gmail.com

In the following code, callBar(2) cannot be called at compile time but it
I reproduce it with dmd v2.070-devel-03bce08 on Linux 64bit system and on
MacOSX system.

We can call callBar(1) instead of callBar(2) at compile time.
If we do not use recursive calls, it works.
If we use out parameter instead of pointer type parameter, it works.

struct Foo
    union Store
        string str;
        Foo[int] object;
    Store store;

void bar(Foo* value, int lv)
    if (lv <= 0) return;

    value.store.object = null;

    Foo member;
    bar(&member, lv-1);
    value.store.object[lv] = member; // line 19

// for static assert
auto callBar(int a)
    Foo f;
    bar(&f, a);
    return 0;

static assert(callBar(2) == 0);

error.d(19): Error: reinterpretation through overlapped field object is not
allowed in CTFE
error.d(18):        called from here: bar(& member, lv - 1)
error.d(25):        called from here: bar(& f, a)
error.d(29):        called from here: callBar(2)
error.d(29):        while evaluating: static assert(callBar(2) == 0)

This bug prevent std.json.parseJSON from being CTFEable for some inputs.

Mar 02 2016