struct S { enum N = 1; } void foo(S v) @nogc { static foreach(_; 0 .. v.N) { } } ---- Error: function mir.ndslice.slice.foo is @nogc yet allocates closures with the GC source\mir\ndslice\slice.d(290,23): foo.__lambda2.__lambda1 closes over variable v
struct S { enum N = 1; } S foo(S v) @nogc { static foreach(_; 0 .. typeof(return).N) { } return S.init; } ---- Error: cannot use typeof(return) inside function __lambda1 with inferred return type
The closure allocation error is not a bug in the `static foreach` implementation. Reduced test case: --- struct S{ enum N=1; } void foo(S v) @nogc{ enum x=()=>v.N; } --- Workaround: --- int foo(S v)@nogc{ enum x=typeof(v).N; } --- The second case is due to an oversight on my side. It can be worked around using an alias: struct S{ enum N = 1; } S foo(S v)@nogc{ alias R=typeof(return); static foreach(_;0..R.N){ } return S.init; }
https://github.com/dlang/dmd/pull/7118 The fix for the second case is at the same time a workaround for the first case. I'll file another bug report for the underlying cause.
(In reply to timon.gehr from comment #3) > case. I'll file another bug report for the underlying cause. https://issues.dlang.org/show_bug.cgi?id=17804
Commits pushed to stable at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/17a1effdb48c0d31bb3ba3611d8a22ef2ce8abed fix Issue 17800 - [2.076] "static foreach" allocates closures in GC without reason https://github.com/dlang/dmd/commit/365e392f67aa66bab08a40fcb4336b6c600df511 Merge pull request #7118 from tgehr/fix17800 fix Issue 17800 - [2.076] "static foreach" allocates closures in GC without reason merged-on-behalf-of: Walter Bright <WalterBright@users.noreply.github.com>
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/17a1effdb48c0d31bb3ba3611d8a22ef2ce8abed fix Issue 17800 - [2.076] "static foreach" allocates closures in GC without reason https://github.com/dlang/dmd/commit/365e392f67aa66bab08a40fcb4336b6c600df511 Merge pull request #7118 from tgehr/fix17800