[Issue 21912] delegate assigned to return scope variable needs closure

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Dec 22 14:38:11 UTC 2021


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

johanengelen at weka.io changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |johanengelen at weka.io

--- Comment #12 from johanengelen at weka.io ---
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.

--


More information about the Digitalmars-d-bugs mailing list