Commit 71f9a202
71f9a20297b5Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): coerce return value when promote mode widens slot to bigint
In --int-overflow=promote, the analyzer rewrites return slots to
bigint, but compile_return_stmt and the body-return fallback emitted
return <int_expr>; unchanged. The C compiler then errored with
-Wint-conversion (since -Werror is on for the test suite),
turning legitimate code into build failures. The single missing
coercion accounted for ~50 of the 230 promote-mode ERRs.
Add int->bigint (sp_bigint_new_int) and bigint->int
(sp_bigint_to_int) wraps at both return sites when the inferred
expression type differs from the declared return type.
Promote-mode test count: 364 pass / 24 fail / 230 error -> 404 pass / 35 fail / 179 error. Default raise mode unchanged at 618 pass / 0 fail / 0 error. Optcarrot checksum 59662 retained.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 71f9a20297b5
- Parent
- 0cc69bdfe6d7
spinel_codegen.rbmodifiedView file
if rt == "poly" && @current_method_return != "poly" && @current_method_return != "void" && @current_method_return != "" ret_val = compile_expr_for_expected_type(arg_ids[0], @current_method_return) ret_ctype = c_type(@current_method_return) # `--int-overflow=promote` widens return slots to bigint; an # explicit `return <int_expr>` needs sp_bigint_new_int wrap. # Reverse covers callers that still want mrb_int. elsif base_type(@current_method_return) == "bigint" && rt == "int" @needs_bigint = 1 ret_val = "sp_bigint_new_int(" + ret_val + ")" ret_ctype = c_type(@current_method_return) elsif base_type(@current_method_return) == "int" && rt == "bigint" @needs_bigint = 1 ret_val = "sp_bigint_to_int((sp_Bigint *)" + ret_val + ")" ret_ctype = c_type(@current_method_return) end if @in_gc_scope == 1 # Save return value, run ensure replays (which may mutate end end else emit(" return " + val + ";") # `--int-overflow=promote` promotes int slots (incl. return # type) to bigint at analyze time, so a literal/int-typed # fall-through last expression must be wrapped before return. # The reverse direction covers callers that still want mrb_int. if base_type(return_type) == "bigint" && expr_type == "int" @needs_bigint = 1 emit(" return sp_bigint_new_int(" + val + ");") elsif base_type(return_type) == "int" && expr_type == "bigint" @needs_bigint = 1 emit(" return sp_bigint_to_int((sp_Bigint *)" + val + ");") else emit(" return " + val + ";") end end else compile_stmt(last)