In the following code, callBar(2) cannot be called at compile time but it should.
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);
---
Output:
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.
Comment #1 by robert.schadek — 2024-12-13T18:47:01Z