www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21912] New: Invalid stack closure when calling delegate

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

          Issue ID: 21912
           Summary: Invalid stack closure when calling delegate inside
                    lambda
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: per.nordlow gmail.com

Compiled with -dip1000 -dip25 -dip1008, the code

 safe:
import std.stdio;

auto foo(DG)(DG dg, int p) {
    return () => dg(p);
}

auto bla(int i) {
    return foo((int p)=>i*p, 66);
}

void main() {
    writeln(bla(5)());
}

should produce 5*66 = 330. But instead produces seemingly random values. See

https://run.dlang.io/?compiler=dmd&source= safe:%0Aimport%20std.stdio;%0A%0Aauto%20foo(DG)(DG%20dg,%20int%20p)%20%7B%0A%20%20%20%20return%20()%20%3D%3E%20dg(p);%0A%7D%0A%0Aauto%20bla(int%20i)%20%7B%0A%20%20%20%20return%20foo((int%20p)%3D%3Ei*p,%2066);%0A%7D%0A%0Avoid%20main()%20%7B%0A%20%20%20%20writeln(bla(5)());%0A%7D&args=-dip1000%20-dip25%20-dip1008

--
May 10