Comment #0 by snarwin+bugzilla — 2019-06-22T12:12:38Z
auto call(alias dg)() {
return dg();
pragma(msg, typeof(dg));
}
@nogc void main() {
int acc = 0;
call!(() => acc += 1);
}
---
The program above fails to compile with the following error:
Error: function `D main` is @nogc yet allocates closures with the GC
However, if the pragma(msg) is moved before the return statement in `call`, there is no error.
Comment #1 by snarwin+bugzilla — 2019-09-29T20:15:48Z
This also affects delegates passed as arguments to template sequence parameters. A reference to a different, non-delegate element of the same sequence parameter can cause a closure to be allocated, as in the following example:
---
auto call(Args...)() {
return Args[0]();
pragma(msg, typeof(Args[1]));
}
@nogc void main() {
int acc = 0;
call!(() => acc += 1, "hello");
}
---
As before, if the pragma(msg) is moved before the return statement in `call`, the program compiles and runs without error.
Comment #2 by robert.schadek — 2024-12-13T19:04:15Z