This program fails to compile in the "ignore!..." line.
@nogc nothrow void ignore(alias F)() {}
@nogc nothrow void call(alias F)() { F(); }
@nogc
void main() {
int x;
ignore!(()=>x)(); // delegate causes "x" to come from GC
call!(()=>x)(); // is fine
}
It compiles fine if the "ignore!" line is commented out.
If a delegate is unused, surely its scope is at least as narrow as a delegate that is used.
Why is @nogc inference not done on the template instantiation result?
Comment #1 by snarwin+bugzilla — 2021-08-10T14:02:45Z
Simplified example:
---
void main() @nogc
{
int x;
void ignore() { alias F = () => x; }
}
---
Interestingly, replacing the lambda with a named function gets rid of the closure allocation:
---
void main() @nogc
{
int x;
void ignore() { auto F() { return x; } }
}
---
The above workaround also works for the original example.
Comment #2 by robert.schadek — 2024-12-13T19:15:44Z