D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5711 - Accessing local variable of a function in which an inner class is instantiated trashes this.outer
Summary: Accessing local variable of a function in which an inner class is instantiate...
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: spec, wrong-code
Depends on:
Blocks:
 
Reported: 2011-03-06 10:02 UTC by Stewart Gordon
Modified: 2020-08-29 06:56 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Stewart Gordon 2011-03-06 10:02:59 UTC
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 Ryuichi OHORI 2017-03-31 23:55:56 UTC
I don't observe this behavior with DMD2.073.
When was it fixed?
Comment 2 Walter Bright 2020-08-29 06:56:31 UTC
Works fine now.