279 – Nested class can't access var in outer function scope, if nested in class

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 279 - Nested class can't access var in outer function scope, if nested in class
Summary: Nested class can't access var in outer function scope, if nested in class
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Linux
: P2 normal
Assignee: Walter Bright
URL:
Keywords: wrong-code
Depends on:
Blocks:
Reported: 2006年08月05日 14:19 UTC by Søren J. Løvborg
Modified: 2014年02月15日 13:20 UTC (History)
0 users

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Søren J. Løvborg 2006年08月05日 14:19:14 UTC
The nested class can refer to members of the enclosing class, but not variables in the enclosing function.
I assume the problem is that the context pointer of the anonymous class nested within Foo's constructor refers to the Foo instance, and not to the stack-frame of the constructor.
Whether intentional or not, the compiler accepts the program and generates buggy code.
--- Test case ---
import std.stdio;
void foo()
{
 int x = 42;
 new class Object
 {
 this() { writef("%s\n", x); }
 };
}
class Foo
{
 this()
 {
 int x = 42;
 new class Object
 {
 this() { writef("%s\n", x); }
 // Can't access 'x'. Any members of Foo are accessible, however. 
 };
 }
}
void main()
{
 foo(); // prints 42
 new Foo(); // prints garbage integer (e.g. 0, on my system.)
}
Comment 1 Walter Bright 2006年08月11日 19:19:06 UTC
Fixed DMD 0.164
Comment 2 Thomas Kühne 2006年08月14日 06:50:40 UTC
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
d-bugmail@puremagic.com schrieb am 2006年08月05日:
> http://d.puremagic.com/issues/show_bug.cgi?id=279 
> The nested class can refer to members of the enclosing class, but not variables
> in the enclosing function.
>
> I assume the problem is that the context pointer of the anonymous class nested
> within Foo's constructor refers to the Foo instance, and not to the stack-frame
> of the constructor.
>
> Whether intentional or not, the compiler accepts the program and generates
> buggy code.
>
> --- Test case ---
>
> import std.stdio;
>
> void foo()
> {
> int x = 42;
>
> new class Object
> {
> this() { writef("%s\n", x); }
> };
> }
>
> class Foo
> {
> this()
> {
> int x = 42;
>
> new class Object
> {
> this() { writef("%s\n", x); }
> // Can't access 'x'. Any members of Foo are accessible, however. 
> };
> }
> }
>
> void main()
> {
> foo(); // prints 42
>
> new Foo(); // prints garbage integer (e.g. 0, on my system.)
> }
Added to DStress as
http://dstress.kuehne.cn/run/c/class_25_A.d
http://dstress.kuehne.cn/run/c/class_25_B.d
http://dstress.kuehne.cn/run/c/class_25_C.d
http://dstress.kuehne.cn/run/c/class_25_D.d
http://dstress.kuehne.cn/run/c/class_25_E.d
Thomas
-----BEGIN PGP SIGNATURE-----
iD8DBQFE2FUpLK5blCcjpWoRAtYCAJ96qFiR3bU0Ciymjw+R/1h8Ae7CvwCfTQrB
HxI4QMG3aHRW4t9jJj8BIQA=
=fuj4
-----END PGP SIGNATURE-----


AltStyle によって変換されたページ (->オリジナル) /