From f4dee58f03774d033c55d890356cd93c3e2462fb Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 4 Jul 2024 18:27:08 -0400 Subject: Check for functions that don't return when they need to, as well as a fix for 'when' statement typing --- compile.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'compile.c') diff --git a/compile.c b/compile.c index e9648c3a..09314c82 100644 --- a/compile.c +++ b/compile.c @@ -597,6 +597,10 @@ CORD compile_statement(env_t *env, ast_t *ast) }; body_scope->fn_ctx = &fn_ctx; + + if (ret_t->tag != VoidType && ret_t->tag != AbortType && get_type(body_scope, fndef->body)->tag != AbortType) + code_err(ast, "This function can reach the end without returning a %T value!", ret_t); + CORD body = compile_statement(body_scope, fndef->body); if (CORD_fetch(body, 0) != '{') body = CORD_asprintf("{\n%r\n}", body); @@ -733,6 +737,9 @@ CORD compile_statement(env_t *env, ast_t *ast) } if (ret) { + if (env->fn_ctx->return_type->tag == VoidType || env->fn_ctx->return_type->tag == AbortType) + code_err(ast, "This function is not supposed to return any values, according to its type signature"); + env = with_enum_scope(env, env->fn_ctx->return_type); type_t *ret_t = get_type(env, ret); CORD value = compile(env, ret); @@ -742,7 +749,7 @@ CORD compile_statement(env_t *env, ast_t *ast) return CORD_all(code, "return ", value, ";"); } else { if (env->fn_ctx->return_type->tag != VoidType) - code_err(ast, "This function expects a return value of type %T", env->fn_ctx->return_type->tag); + code_err(ast, "This function expects you to return a %T value", env->fn_ctx->return_type); return CORD_all(code, "return;"); } } -- cgit v1.2.3