[Issue 21912] Invalid stack closure when calling delegate inside lambda

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat May 15 08:18:54 UTC 2021


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

--- Comment #7 from Sebastiaan Koppe <mail at skoppe.eu> ---
I read the post, and it sure looks plausible, I just don't understand it.

If this has to do with "pure bypassing scope checks", then making one of the
above examples un-pure would solve it. Although it does work with your reduced
case, not with the ones where foo has a DG template parameter.

That still crashes:

```
void unpure() @trusted { static int g; g = 5; }

DG foo(DG)(DG dg) {
    unpure();
    return dg;
}

auto bla(int i) {
    return foo((){ unpure(); return i;});
}

void main() {
    pragma(msg, typeof(bla)); // @safe int delegate() @safe(int i)
    assert(bla(5)() == 5); // Assertion failure
}
```

What does work though, is to assign the delegate to a variable first. This
compiles (also with the other DG template examples):

```
int delegate() pure foo(int delegate() pure dg) pure {
    return dg;
}

int delegate() bla(int i) pure {
    auto dg = () => i;
    return foo(dg);
}

void main() {
    assert(bla(5)() == 5); // Pass!
}
```

--


More information about the Digitalmars-d-bugs mailing list