Thread overview
Is this a bug in Nullable or in compiler?
Nov 28, 2022
Andrey Zherikov
Nov 28, 2022
Andrey Zherikov
November 28, 2022

The following code doesn't compile:

struct S
{
    Nullable!S delegate() f;
}

Output from DMD:

/dlang/dmd/linux/bin64/../../src/druntime/import/core/internal/traits.d(345): Error: unable to determine fields of `S` because of forward references
/dlang/dmd/linux/bin64/../../src/phobos/std/traits.d(3327): Error: template instance `core.internal.traits._hasIndirections!(S)` error instantiating
/dlang/dmd/linux/bin64/../../src/phobos/std/typecons.d(2768):        instantiated from here: `hasIndirections!(S)`
onlineapp.d(4):        instantiated from here: `Nullable!(S)`
/dlang/dmd/linux/bin64/../../src/druntime/import/core/internal/traits.d(16): Error: unable to determine fields of `S` because of forward references
/dlang/dmd/linux/bin64/../../src/druntime/import/core/internal/traits.d(269): Error: template instance `core.internal.traits.Fields!(S)` error instantiating
/dlang/dmd/linux/bin64/../../src/phobos/std/typecons.d(2790):        instantiated from here: `hasElaborateDestructor!(S)`
onlineapp.d(4):        instantiated from here: `Nullable!(S)`

I'm not sure whether it's a big in Nullable or in compiler because pragma(msg, S.sizeof) doesn't work either.

I tried different variants like function instead of delegate and putting Nullable to parameters like void function(Nullable!S) f; - but they don't work either.

November 28, 2022

If I replace Nullable with SumType!(None,S) then everything works:

struct None {}
struct S
{
    SumType!(None,S) delegate() f;
}
November 28, 2022

On 11/28/22 6:07 AM, Andrey Zherikov wrote:

>

The following code doesn't compile:

struct S
{
     Nullable!S delegate() f;
}
>

I tried different variants like function instead of delegate and putting Nullable to parameters like void function(Nullable!S) f; - but they don't work either.

It's just a forward reference bug.

This seems to fix it:

alias NS = Nullable!S;
struct S
{
   Nullable!S delegate() f;
}

-Steve