diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-07-04 18:27:08 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-07-04 18:27:08 -0400 |
| commit | f4dee58f03774d033c55d890356cd93c3e2462fb (patch) | |
| tree | 55ae39786762084625051a17b94dd99b5970f6ab /compile.c | |
| parent | 2c89f3385f0863c83267b50400832a81de07538c (diff) | |
Check for functions that don't return when they need to, as well as a
fix for 'when' statement typing
Diffstat (limited to 'compile.c')
| -rw-r--r-- | compile.c | 9 |
1 files changed, 8 insertions, 1 deletions
@@ -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;"); } } |
