Browse Source

Fix `defer` on explicit `return` of a procedure with no return values for llvm-api

gingerBill 5 years ago
parent
commit
4daf098a3a
1 changed files with 7 additions and 0 deletions
  1. 7 0
      src/llvm_backend.cpp

+ 7 - 0
src/llvm_backend.cpp

@@ -4219,6 +4219,9 @@ void lb_build_stmt(lbProcedure *p, Ast *node) {
 
 
 		if (return_count == 0) {
 		if (return_count == 0) {
 			// No return values
 			// No return values
+
+			lb_emit_defer_stmts(p, lbDeferExit_Return, nullptr);
+
 			LLVMBuildRetVoid(p->builder);
 			LLVMBuildRetVoid(p->builder);
 			return;
 			return;
 		} else if (return_count == 1) {
 		} else if (return_count == 1) {
@@ -6906,6 +6909,10 @@ void lb_emit_defer_stmts(lbProcedure *p, lbDeferExitKind kind, lbBlock *block) {
 	while (i --> 0) {
 	while (i --> 0) {
 		lbDefer d = p->defer_stmts[i];
 		lbDefer d = p->defer_stmts[i];
 
 
+		isize prev_context_stack_count = p->context_stack.count;
+		defer (p->context_stack.count = prev_context_stack_count);
+		p->context_stack.count = d.context_stack_count;
+
 		if (kind == lbDeferExit_Default) {
 		if (kind == lbDeferExit_Default) {
 			if (p->scope_index == d.scope_index &&
 			if (p->scope_index == d.scope_index &&
 			    d.scope_index > 0) { // TODO(bill): Which is correct: > 0 or > 1?
 			    d.scope_index > 0) { // TODO(bill): Which is correct: > 0 or > 1?