[svn r277] Fixed a nasty bug in delegate expressions. Context pointers to nested functions of the parent, from inside a nested function were

invalid.
This commit is contained in:
Tomas Lindquist Olsen
2008-06-13 05:47:28 +02:00
parent 9a28d083f8
commit b13a5646ca
3 changed files with 49 additions and 6 deletions

View File

@@ -2230,13 +2230,12 @@ DValue* DelegateExp::toElem(IRState* p)
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;

12
tangotests/nested1.d Normal file
View File

@@ -0,0 +1,12 @@
module tangotests.nested1;
void main()
{
int i = 42;
assert(i == 42);
void func()
{
assert(i == 42);
}
func();
}

32
tangotests/nested2.d Normal file
View File

@@ -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;
}