ox

The Ox programming language, compiler and tools (WIP)
Log | Files | Refs | README | LICENSE

commit 710ef7e5f77568afc7e2e5bb659c11aa0bef99ef
parent b66bbb03eb01c17a08e732e45be2c32e9b1c86f1
Author: citbl <citbl@citbl.org>
Date:   Sun, 16 Nov 2025 19:03:01 +1000

handle returns better

Diffstat:
M.zed/debug.json | 2+-
Msrc/gen/gen.c | 6++++--
Mtests/ex-return-given-expr.ox | 2+-
Mtests/ex-return-int.ox | 9+++++----
4 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/.zed/debug.json b/.zed/debug.json @@ -11,7 +11,7 @@ "cwd": "$ZED_WORKTREE_ROOT" }, "program": "$ZED_WORKTREE_ROOT/oxc", - "args": ["$ZED_WORKTREE_ROOT/tests/ex-mul.ox"], + "args": ["$ZED_WORKTREE_ROOT/tests/ex-return-int.ox"], "request": "launch", "adapter": "CodeLLDB" } diff --git a/src/gen/gen.c b/src/gen/gen.c @@ -1116,10 +1116,12 @@ build_func_decl(Gen* gen, Node* node) build_block(gen, node->data.function_decl.body); - if (gen->curr_block && ret_type == type_int) { + if (gen->curr_block == NULL) { + // no open block: all paths already terminated, don't add an implicit return + } else if (ret_type == type_int) { gcc_jit_rvalue* ret_value = gcc_jit_context_new_rvalue_from_int(gen->ctx, type_int, 0); gcc_jit_block_end_with_return(gen->curr_block, loc, ret_value); - } else if (gen->curr_block && ret_type == type_void) { + } else if (ret_type == type_void) { gcc_jit_block_end_with_void_return(gen->curr_block, loc); } else { printf("build_func_decl unhandled return type in func: %s - " diff --git a/tests/ex-return-given-expr.ox b/tests/ex-return-given-expr.ox @@ -5,5 +5,5 @@ int gimme(int ret_value) { void main() { print("expected returned value of 20 = (6 + 6) * 2 - 4:"); - print(gimme(6 + 6) - 4);q + print(gimme(6 + 6) - 4); } diff --git a/tests/ex-return-int.ox b/tests/ex-return-int.ox @@ -1,8 +1,9 @@ -int gimme5() { - return 5; +int gimme55() { + print("heloy"); + return 5 * 5; } void main() { - print("expected returned value of 5:"); - print(gimme5()); + print("expected returned value of 25:"); + print(gimme55()); }