diff --git a/gen/toir.cpp b/gen/toir.cpp index c8c97783..95e5ad86 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -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; diff --git a/tangotests/nested1.d b/tangotests/nested1.d new file mode 100644 index 00000000..9b901b39 --- /dev/null +++ b/tangotests/nested1.d @@ -0,0 +1,12 @@ +module tangotests.nested1; + +void main() +{ + int i = 42; + assert(i == 42); + void func() + { + assert(i == 42); + } + func(); +} diff --git a/tangotests/nested2.d b/tangotests/nested2.d new file mode 100644 index 00000000..41b2cf39 --- /dev/null +++ b/tangotests/nested2.d @@ -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