www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 279] New: Nested class can't access var in outer function scope, if nested in class

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=279

           Summary: Nested class can't access var in outer function scope,
                    if nested in class
           Product: D
           Version: 0.163
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: web kwi.dk


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.)
}


-- 
Aug 05 2006
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=279


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED




------- Comment #1 from bugzilla digitalmars.com  2006-08-11 19:19 -------
Fixed DMD 0.164


-- 
Aug 11 2006
prev sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----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-----
Aug 14 2006