DIP60: @nogc attribute

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 25 05:07:00 PDT 2014


On Fri, 25 Apr 2014 07:28:27 -0400, Ola Fosheim Grøstad  
<ola.fosheim.grostad+dlang at gmail.com> wrote:

> On Thursday, 24 April 2014 at 13:35:39 UTC, bearophile wrote:
>>     immutable int x = 3;
>>     auto result = data[].map!(y => y * x);
>> }
>>
>>
>> test.d(1,6): Error: function D main @nogc function allocates a closure  
>> with the GC
>>
>> Such kind of code is common, so a good amount of range-based code can't  
>> be @nogc.
>
> Why can't this be on the stack if the referenced local function (lambda)  
> does not outlive the stack frame?

It could. I don't think the compiler is smart enough, as it would need to  
verify result doesn't go anywhere (flow analysis).

I wonder if LDC/GDC could do it.

One interesting thing about this is that the compiler implementation may  
make some @nogc code valid on some compilers, and invalid on others, even  
though the resulting execution is the same.

-Steve


More information about the Digitalmars-d mailing list