www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22563] New: Nested structs, if not escaping, shouldn't

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

          Issue ID: 22563
           Summary: Nested structs, if not escaping, shouldn't allocate
                    context (just like delegates)
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: stanislav.blinov gmail.com

void consumeThingWithContext(Dg)(scope Dg dg)
if (is(Dg == delegate))
{
    /* ... */
}

void consumeThingWithContext(S)(scope S s)
if (is(S == struct) && __traits(isNested, S))
{
    /* ... */
}

 nogc
void testDelegate() // compiles
{
    int a;
    consumeThingWithContext({ a++; });
}

 nogc
void testStruct() // Error: function `testStruct` is ` nogc` yet allocates
closures with the GC
{
    int a;
    struct Nested { ~this()  nogc { a++; } }
    consumeThingWithContext(Nested());
}

---

IOTW, if there's no need to allocate context on the GC, it shouldn't be done.

--
Dec 03 2021