www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23627] New: lazy params don't allocateclosures

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

          Issue ID: 23627
           Summary: lazy params don't allocateclosures
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: destructionator gmail.com

This assert should NOT pass:

```
struct W {
        string delegate() magic;
        void content(lazy string s) {
                magic = &s;
        }
        string test;
}

void main() {
        int onStack;
        W w;

        w.content = w.test;

        auto diff = w.magic.ptr - cast(void*) &onStack;
        if(diff < 0) diff = -diff;

        assert(diff > 64);
}
```

Notice that if you change it to an explicit delegate:

        void content(string delegate() s) {
// snip
        w.content = () => w.test;


It will fail.

Can also confirm by using the disassembly to see the lack of _d_allocmemory for
the closure.


The spec explicitly permits taking the address of a lazy param to get the
delegate and it is not marked with any lifetime restrictions, so the
implementation is wrong to give it limited lifetime.

--
Jan 14 2023