import std.algorithm, std.range; auto foo(int[] a, immutable int b) @nogc //Fails { return a.map!(x => x + b); } auto bar(R)(R a, immutable int b) @nogc //OK { return a.map!(x => x + b); } auto baz(int[] a, immutable int b) @nogc //OK { return bar(a, b); } Foo cannot be made @nogc: Error: function nogctest.foo @nogc function allocates a closure with the GC
I think this is a duplicate of issue 14771. That is, bar shouldn't compile. The delegate uses a local variable and is returned from the function, so it needs a closure. Where does that closure go if not on the GC heap?
Seems to have been fixed at some point
Just to confirm, by "fixed" you mean that all three now consistently fail to compile? FWIW, the change seems to have been accidental: the second and third function compiled before and don't compile after https://github.com/dlang/dmd/pull/5271.