www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - nogc and lazy arguments

reply "bearophile" <bearophileHUGS lycos.com> writes:
Lazy arguments in general allocate, but who is to blame for the 
allocation?
Isn't the allocation at the calling point?

This code:


void foo(lazy int x)  nogc {
     auto r = x(); // Error
}
void main() {
     foo(1);
}


Gives:

test.d(2,15): Error:  nogc function 'test.foo' cannot call 
non- nogc delegate 'x'

Is it right to refuse the  nogc annotation on foo()? I think here 
foo should be allowed to be  nogc, while the main() can't be 
 nogc. What do you think?

Bye,
bearophile
Apr 27 2014
parent reply "Dicebot" <public dicebot.lv> writes:
On Sunday, 27 April 2014 at 13:09:39 UTC, bearophile wrote:
 Lazy arguments in general allocate, but who is to blame for the 
 allocation?
 Isn't the allocation at the calling point?

 This code:


 void foo(lazy int x)  nogc {
     auto r = x(); // Error
 }
 void main() {
     foo(1);
 }


 Gives:

 test.d(2,15): Error:  nogc function 'test.foo' cannot call 
 non- nogc delegate 'x'

 Is it right to refuse the  nogc annotation on foo()? I think 
 here foo should be allowed to be  nogc, while the main() can't 
 be  nogc. What do you think?

 Bye,
 bearophile
It happens because attribute inference does not work properly on generated delegated for lazy argument. I think it is a bug "lazy int x" is effectively same as "int delegate() x" and nogc states that you can only call other nogc functions and delegates from something annotated as nogc.
Apr 27 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
Dicebot:

 It happens because attribute inference does not work properly 
 on generated delegated for lazy argument. I think it is a bug

 "lazy int x" is effectively same as "int delegate() x" and 
  nogc states that you can only call other  nogc functions and 
 delegates from something annotated as  nogc.
https://issues.dlang.org/show_bug.cgi?id=12664 Bye, bearophile
Apr 27 2014