www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23204] New: [REG2.097] discarded scope return still allocates

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

          Issue ID: 23204
           Summary: [REG2.097] discarded scope return still allocates GC
                    closure
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: regression
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: johanengelen weka.io

Testcase:
```
auto invoker(Dlg)(scope Dlg dlg) { return dlg; }  // <--- `return` is inferred
on parameter

// Does not compile, but should.
 nogc void f(int x) { invoker({x++;}); }  // <--- return value is discarded,
hence does not escape the scope.

// This compiles fine, without gc allocation:
 nogc void g(int x) {
    scope inv = invoker({x++;});
    inv();
}
```

Compilation succeeds with 2.096, fails with 2.097 with the error:
<source>(3): Error: function `example.f` is ` nogc` yet allocates closures with
the GC
<source>(3):        example.f.__lambda2 closes over variable x at <source>(3)
C

The regression is related to the fix for
https://issues.dlang.org/show_bug.cgi?id=21912

--
Jun 22 2022