[Issue 18058] @nogc and forwarding lazy argument, particularly with scope

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Dec 12 02:37:07 UTC 2017


https://issues.dlang.org/show_bug.cgi?id=18058

--- Comment #4 from Shachar Shemesh <shachar at weka.io> ---
(In reply to Seb from comment #2)
> FWIW that attributes are set wrongly on lazy parameters and their function
> is a well-known bug and has been around for a long time.

True, but it wouldn't be a problem had the overly complex forwarding not take
place.
> 
> For yours:
> 
> ---
> import object;
> @nogc void func1(lazy scope string msg)
> {
> }
> void func2(lazy scope string msg)
> {
> 	func1(delegate string() => msg()); // <- lambda doesn't have @nogc
> }
> ---

The problem is not that the lambda doesn't have @nogc. The problem is that if
we're going to provide a lambda there, it has no choice *but* to GC. The lambda
has to have access to the frame (because "scope" was not honored), and so must
allocate.

The issue here is that, once you've started lowering, the code the compiler
emits is way under-optimized. It essentially creates a lambda whose sole
purpose is evaluating another delegate that has the precise same interface. Had
that redundant delegate not been generated, there would not be a need to
allocate func2's frame with a GC, even without scope on the lazy.

--


More information about the Digitalmars-d-bugs mailing list