# HG changeset patch # User Christian Kamm # Date 1232200412 -3600 # Node ID 7f6eeb7b003e91d4a260008e6c74e59afd623e61 # Parent 7ddd03a2ce020ba18bae6631ef98fa26c05d8195 Fix #163. diff -r 7ddd03a2ce02 -r 7f6eeb7b003e gen/toir.cpp --- a/gen/toir.cpp Fri Jan 16 21:06:33 2009 +0100 +++ b/gen/toir.cpp Sat Jan 17 14:53:32 2009 +0100 @@ -2099,9 +2099,15 @@ Type* dtype = type->toBasetype(); const LLType* resty = DtoType(dtype); - // allocate a temporary for the final result. failed to come up with a better way :/ - LLValue* resval = DtoAlloca(resty,"condtmp"); - DVarValue* dvv = new DVarValue(type, resval); + DValue* dvv; + // voids returns will need no storage + if (dtype->ty != Tvoid) { + // allocate a temporary for the final result. failed to come up with a better way :/ + LLValue* resval = DtoAlloca(resty,"condtmp"); + dvv = new DVarValue(type, resval); + } else { + dvv = new DConstValue(type, getNullValue(DtoTypeNotVoid(dtype))); + } llvm::BasicBlock* oldend = p->scopeend(); llvm::BasicBlock* condtrue = llvm::BasicBlock::Create("condtrue", gIR->topfunc(), oldend); @@ -2114,12 +2120,14 @@ p->scope() = IRScope(condtrue, condfalse); DValue* u = e1->toElem(p); - DtoAssign(loc, dvv, u); + if (dtype->ty != Tvoid) + DtoAssign(loc, dvv, u); llvm::BranchInst::Create(condend,p->scopebb()); p->scope() = IRScope(condfalse, condend); DValue* v = e2->toElem(p); - DtoAssign(loc, dvv, v); + if (dtype->ty != Tvoid) + DtoAssign(loc, dvv, v); llvm::BranchInst::Create(condend,p->scopebb()); p->scope() = IRScope(condend, oldend); diff -r 7ddd03a2ce02 -r 7f6eeb7b003e tests/mini/bug163_void_condexp.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/mini/bug163_void_condexp.d Sat Jan 17 14:53:32 2009 +0100 @@ -0,0 +1,18 @@ + +static foocalled = false; +static barcalled = false; +void foo() { foocalled = true; } +void bar() { barcalled = true; } + +void f(bool b) +{ + return b ? foo() : bar(); +} + +void main() +{ + f(true); + assert(foocalled && !barcalled); + f(false); + assert(foocalled && barcalled); +}