[Issue 2043] Closure outer variables in nested blocks are not allocated/instantiated correctly: should have multiple instances but only have one.

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Oct 2 09:09:10 PDT 2014


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

--- Comment #14 from hsteoh at quickfur.ath.cx ---
Note that the same thing applies if you make the loop counter immutable, which
the compiler also readily accepts. But the delegates will all capture the same
address which is reused over and over.

So, basically, the logical scope of the loop counter i should be restricted to
a *single* iteration (otherwise we must reject immutable loop counters!), but
currently, its actual scope is the loop body *across* all iterations. The
delegates exacerbate this problem by extending this scope even further, outside
the loop body. Ideally, the compiler should allocate each new loop counter
value on the heap, unless it can prove that there are no escaping references to
it at the end of the iteration, in which case it is then safe to put it on the
stack (and reuse the same memory for subsequent iterations).

--


More information about the Digitalmars-d-bugs mailing list