May 18, 2023
https://issues.dlang.org/show_bug.cgi?id=23923

          Issue ID: 23923
           Summary: `this` not captured by lazy expression
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: accepts-invalid, wrong-code
          Severity: normal
          Priority: P3
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: dlang-bugzilla@thecybershadow.net

////////// test.d //////////
@safe:

void delegate() later;

void lazyFun(lazy void expr)
{
    later = { expr; };
}

class C
{
    void virtualFunc() {}

    final void run()
    {
        lazyFun({
            virtualFunc();
        }());
    }
}

void main()
{
    auto c = new C;
    c.run();
    later();
}
////////////////////////////

This program segfaults. The `this` pointer used by the `virtualFunc` call is gone by the time it is evaluated.

I am not sure if this is accepts-invalid or wrong-code. On one hand, the lazy expression does capture regular locals. On the other hand, one would think that `lazy` would imply `scope`, so the implicit reference to `this` (or other locals) would not be allowed.

--