[Issue 22563] New: Nested structs, if not escaping, shouldn't allocate context (just like delegates)

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Dec 3 13:30:19 UTC 2021


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 at puremagic.com
          Reporter: stanislav.blinov at 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.

--


More information about the Digitalmars-d-bugs mailing list