changeset 259:2e652b8ad1fd trunk

[svn r277] Fixed a nasty bug in delegate expressions. Context pointers to nested functions of the parent, from inside a nested function were invalid.
author lindquist
date Fri, 13 Jun 2008 05:47:28 +0200
parents 8dbddae09152
children d69d4543c441
files gen/toir.cpp tangotests/nested1.d tangotests/nested2.d
diffstat 3 files changed, 49 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/gen/toir.cpp	Thu Jun 12 19:59:19 2008 +0200
+++ b/gen/toir.cpp	Fri Jun 13 05:47:28 2008 +0200
@@ -2230,13 +2230,12 @@
     DValue* u = e1->toElem(p);
     LLValue* uval;
     if (DFuncValue* f = u->isFunc()) {
-        //assert(f->vthis);
-        //uval = f->vthis;
-        LLValue* nestvar = p->func()->decl->ir.irFunc->nestedVar;
-        if (nestvar)
-            uval = nestvar;
+        assert(f->func);
+        LLValue* contextptr = DtoNestedContext(f->func->toParent2()->isFuncDeclaration());
+        if (!contextptr)
+            uval = LLConstant::getNullValue(getVoidPtrType());
         else
-            uval = llvm::ConstantPointerNull::get(int8ptrty);
+            uval = DtoBitCast(contextptr, getVoidPtrType());
     }
     else {
         DValue* src = u;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tangotests/nested1.d	Fri Jun 13 05:47:28 2008 +0200
@@ -0,0 +1,12 @@
+module tangotests.nested1;
+
+void main()
+{
+    int i = 42;
+    assert(i == 42);
+    void func()
+    {
+        assert(i == 42);
+    }
+    func();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tangotests/nested2.d	Fri Jun 13 05:47:28 2008 +0200
@@ -0,0 +1,32 @@
+module tangotests.nested2;
+
+extern(C) int printf(char*, ...);
+
+void main()
+{
+    int var = 2;
+
+    void exec(void delegate() dg)
+    {
+        printf("var = %d\n", var);
+        dg();
+    }
+
+    void foo()
+    {
+        printf("var = %d\n", var);
+        assert(var == 5);
+    }
+
+    void bar()
+    {
+        printf("var = %d\n", var);
+        var += 3;
+        exec(&foo);
+    }
+
+    printf("var = %d\n", var);
+    exec(&bar);
+
+    return 0;
+}
\ No newline at end of file