Bug 5711 – Accessing local variable of a function in which an inner class is instantiated trashes this.outer

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2011-03-06T10:02:59Z
Last change time
2020-08-29T06:56:31Z
Keywords
spec, wrong-code
Assigned to
No Owner
Creator
Stewart Gordon

Comments

Comment #0 by smjg — 2011-03-06T10:02:59Z
Based on a newsgroup post by Iain Buclaw. ---------- import std.stdio; class Outer { int w = 3; void method() { int x = 4; new class Object { this() { writefln("%d", x); // remove to suppress bug writefln("%d", w); writefln("%d", this.outer.w); } }; } } void main() { (new Outer).method(); } ----- DMD 1.067 ----- 4 3 4202691 ----- DMD 2.052 ----- 4 3 0 ---------- It would appear that DMD is getting confused over whether the context pointer of the inner class points to the stack frame of method or the Outer object. Which is it meant to be? Moreover, is the use of x inside the constructor meant to be legal? If the context pointer points to the Outer, it shouldn't be legal in methods, though I suppose it can still be allowed in the constructor.
Comment #1 by r.97all — 2017-03-31T23:55:56Z
I don't observe this behavior with DMD2.073. When was it fixed?
Comment #2 by bugzilla — 2020-08-29T06:56:31Z
Works fine now.