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

[email protected] changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |[email protected]

--- Comment #12 from [email protected] ---
I do not believe the linked PR a correct fix for the problem, and I do not
agree (in all cases) with what the title of this bug report states. The linked
PR fixes the problem of bad codegen, but over-constraints and limits correct
use cases.

Whether a delegate assigned to return scope parameter needs a closure or not
depends on what is done with the return value, that is exactly what the return
attribute is for. The return attribute results in the return value being scope
in the caller. If it the return value escapes from the caller, only then is a
gc-allocated closure needed.

```
// This should compile fine, without gc allocation:
auto invoker(Dlg)(scope Dlg dlg) { return dlg; }  // <--- `return` is infered
on parameter
@nogc void f(int x) { invoker({x++;}); }  // <--- return value is discarded,
hence does not escape the scope.

// This should compile fine, without gc allocation:
auto invoker(Dlg)(scope Dlg dlg) { return dlg; }  // <--- `return` is infered
on parameter
@nogc void f(int x) {
    scope inv = invoker({x++;});
    inv();
}

// This should give a compile error:
auto invoker(Dlg)(scope Dlg dlg) { return dlg; }
@nogc auto f(int x) { return invoker({x++;}); }  // <--- Error: `scope`
variable escapes scope OR allocation in @nogc 
```

The breakage of the fix is perhaps larger than expected because return is still
infered on template parameters, without -dip1000.

--

Reply via email to