From c5c2fd2d1cf34b9b9387041284c148464a74268b Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 10 Dec 2022 16:36:41 +0100 Subject: [PATCH] handle non-void fn without return --- semantics.ml | 10 ++++++---- tests/16_err-no-return-int-fn.test | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 tests/16_err-no-return-int-fn.test diff --git a/semantics.ml b/semantics.ml index 103a98f..777fe34 100644 --- a/semantics.ml +++ b/semantics.ml @@ -70,14 +70,16 @@ let analyze_instr env ua ret_t = function Return ae, env, [] ;; -let rec analyze_block env ua ret_t = function - | [] -> [], ua +let rec analyze_block env ua ret_t pos = function + | [] -> + if ret_t != Void_t then warn "Non-void function without return" pos; + [], ua | instr :: new_block -> let new_instr, new_env, ua1 = analyze_instr env ua ret_t instr in (match new_instr with | Return _ -> [ new_instr ], ua1 | _ -> - let new_block, ua2 = analyze_block new_env ua1 ret_t new_block in + let new_block, ua2 = analyze_block new_env ua1 ret_t pos new_block in new_instr :: new_block, ua2) ;; @@ -89,7 +91,7 @@ let analyze_func env ua = function (match h with | Syntax.Arg a -> add_args (Env.add a.name a.type_t env) t) in - let block, _ = analyze_block (add_args env f.args) ua f.type_t f.code in + let block, _ = analyze_block (add_args env f.args) ua f.type_t f.pos f.code in ( Func ( f.func , List.map diff --git a/tests/16_err-no-return-int-fn.test b/tests/16_err-no-return-int-fn.test new file mode 100644 index 0000000..c8bf1e1 --- /dev/null +++ b/tests/16_err-no-return-int-fn.test @@ -0,0 +1 @@ +int main () {} # Warning on line 1 col 4: Non-void function without return.