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: [email protected]
Reporter: [email protected]
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.
--