February 15, 2019
https://issues.dlang.org/show_bug.cgi?id=19679

          Issue ID: 19679
           Summary: variable escapes unnoticed when referenced in function
                    called from function whose address is taken
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: default_357-line@yahoo.de

Consider this failing code:

void delegate() foo()
{
    size_t value = 0;

    void check()
    {
        assert(value == 0);
    }

    void nest1()
    {
        void nest2() { check(); }
        nest2();
    }
    return &nest1;
}

void main()
{
    foo()();
}

What should happen is that foo allocates its stackframe on the heap. However, this does not occur, because foo doesn't realize that value is referenced at all.

Speculation from staring at printfs for two hours: if nest2() is removed and
check() is called directly, D notices that nest1() is a sibling caller for
check(). However, since the call is hidden in a nested function of a nested
function that is never called in the first place, D seems to lose the plot
somewhere. I don't know where, the escape code is very wacky. Good luck.

--