www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21165] New: Spurious nogc error with delegate taking

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

          Issue ID: 21165
           Summary: Spurious  nogc error with delegate taking `immutable
                    size_t`
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: andy.pj.hanson gmail.com

This particular code fails with a compile error that it shouldn't.

```
 nogc:

void main() {
        int x = 0;
        f((int, immutable size_t) {
                int y = x;
        });
}

void f(T)(scope void delegate(T, immutable size_t)  nogc cb) {
        cb(0, 0);
}
```

When I run `dmd app.d` this fails with:

```
app.d(3): Error: function D main is  nogc yet allocates closures with the GC
app.d(5):        app.main.__lambda1 closes over variable x at app.d(4)
```

There is no compile error if:
* The second delegate parameter is something other than `immutable size_t`
(such as `size_t` or `immutable bool`).
* An explicit type argument is given to `f`, as in `f!int`.
* The delegate parameters are named, as in `f((int a, immutable size_t b) {`.

--
Aug 15 2020