Skip to content

Commit

Permalink
Fix a crash when returning void.
Browse files Browse the repository at this point in the history
  • Loading branch information
deadalnix committed Dec 3, 2024
1 parent 229c066 commit 7083718
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/d/llvm/backend.d
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ public:
}
}

import d.llvm.global;
GlobalGen(pass).checkModule();

runLLVMPasses();

// Now that we generated the IR, we run the unittests.
Expand Down
11 changes: 10 additions & 1 deletion src/d/llvm/statement.d
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,17 @@ struct StatementGen {
break;

case Return:
LLVMValueRef ret;
if (bb.value) {
auto ret = genExpression(bb.value);
auto v = genExpression(bb.value);

// LLVM IR does not support void return.
if (LLVMGetTypeKind(LLVMTypeOf(v)) != LLVMTypeKind.Void) {
ret = v;
}
}

if (ret) {
LLVMBuildRet(builder, ret);
} else {
LLVMBuildRetVoid(builder);
Expand Down
16 changes: 16 additions & 0 deletions test/unit/dg.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
unittest voiddg {
uint a = 0;
void increment() {
a++;
}

increment();
assert(a == 1);

void forward(void delegate() dg) {
return dg();
}

forward(increment);
assert(a == 2);
}

0 comments on commit 7083718

Please sign in to comment.