diff --git a/cpp/src/build.cpp b/cpp/src/build.cpp index 3a715d4..df0ee77 100644 --- a/cpp/src/build.cpp +++ b/cpp/src/build.cpp @@ -236,9 +236,12 @@ namespace build // because its become a function that returns an integer, we *must* sneak in a return statement: meta_region.children.front().children.push_back(ast::node { - .payload = ast::return_statement + .payload = ast::expression { - .expr = ast::expression{.expr = ast::integer_literal{.val = 0}} + .expr = ast::return_statement + { + .expr = ast::expression{.expr = ast::integer_literal{.val = 0}} + } } }); ret.root.children.push_back(meta_region); diff --git a/cpp/src/codegen.cpp b/cpp/src/codegen.cpp index 9a58dcd..b03c0ec 100644 --- a/cpp/src/codegen.cpp +++ b/cpp/src/codegen.cpp @@ -1229,7 +1229,7 @@ namespace code const ast::node& child = node.children[i]; ast::path_t child_path = blk_path; child_path.push_back(i); - if(std::holds_alternative(child.payload)) + if(std::holds_alternative(child.payload) && std::holds_alternative(std::get(child.payload).expr)) { has_return = true; } @@ -1237,10 +1237,17 @@ namespace code codegen_thing({.ctx = new_ctx, .state = d.state}, new_ctx.node().payload); } + if(!has_return) + { + if(funcdata.return_ty.is_void()) + { + // automatically add a return :) you're welcome + builder->CreateRetVoid(); + } + d.ctx.error(error_code::codegen, "missing return value for function \"{}\"", payload.func_name); + } if(!has_return && funcdata.return_ty.is_void()) { - // automatically add a return :) you're welcome - builder->CreateRetVoid(); } } llvm::verifyFunction(*llvm_fn); diff --git a/samples/scratchpad.psy b/samples/scratchpad.psy index 02d849d..ef8158f 100644 --- a/samples/scratchpad.psy +++ b/samples/scratchpad.psy @@ -52,11 +52,13 @@ dub :: (val : i64) -> i64 { putchar(myint@i8); } + return (val * 2); } complicated :: (par1 : i64, par : i64, par3 : i64) -> i64 { putchar(-par@i8); + return 0; } poggers : i64 := 5; @@ -104,7 +106,6 @@ morb :: (par0 : i64) -> f64 return retval; //return morb(5 * complicated(dub(5))); } -morb(65); == default : build == {