diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-08-19 12:39:45 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-08-19 12:39:45 -0400 |
| commit | 8c4210ba01d7ef2b754593f38a296af004343133 (patch) | |
| tree | 1e701a6a39bef3a450f901b0db0b64c933349bf5 | |
| parent | 752be14eed4c56186b0a814980445b279ea88661 (diff) | |
Support demoting int literals
| -rw-r--r-- | compile.c | 15 |
1 files changed, 10 insertions, 5 deletions
@@ -838,11 +838,16 @@ CORD compile_statement(env_t *env, ast_t *ast) 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); - if (!promote(env, &value, ret_t, env->fn_ctx->return_type)) - code_err(ast, "This function expects a return value of type %T, but this return has type %T", - env->fn_ctx->return_type, ret_t); + CORD value; + if (env->fn_ctx->return_type->tag == IntType && ret->tag == Int) { + value = compile_int_to_type(env, ret, env->fn_ctx->return_type); + } else { + type_t *ret_value_t = get_type(env, ret); + value = compile(env, ret); + if (!promote(env, &value, ret_value_t, env->fn_ctx->return_type)) + code_err(ast, "This function expects a return value of type %T, but this return has type %T", + env->fn_ctx->return_type, ret_value_t); + } return CORD_all(code, "return ", value, ";"); } else { if (env->fn_ctx->return_type->tag != VoidType) |
