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

@@ -26831,6 +26831,17 @@
2683126831 if rt == "poly" && @current_method_return != "poly" && @current_method_return != "void" && @current_method_return != ""
2683226832 ret_val = compile_expr_for_expected_type(arg_ids[0], @current_method_return)
2683326833 ret_ctype = c_type(@current_method_return)
26834+ # `--int-overflow=promote` widens return slots to bigint; an
26835+ # explicit `return <int_expr>` needs sp_bigint_new_int wrap.
26836+ # Reverse covers callers that still want mrb_int.
26837+ elsif base_type(@current_method_return) == "bigint" && rt == "int"
26838+ @needs_bigint = 1
26839+ ret_val = "sp_bigint_new_int(" + ret_val + ")"
26840+ ret_ctype = c_type(@current_method_return)
26841+ elsif base_type(@current_method_return) == "int" && rt == "bigint"
26842+ @needs_bigint = 1
26843+ ret_val = "sp_bigint_to_int((sp_Bigint *)" + ret_val + ")"
26844+ ret_ctype = c_type(@current_method_return)
2683426845 end
2683526846 if @in_gc_scope == 1
2683626847 # Save return value, run ensure replays (which may mutate
@@ -35315,7 +35326,19 @@
3531535326 end
3531635327 end
3531735328 else
35318- emit(" return " + val + ";")
35329+ # `--int-overflow=promote` promotes int slots (incl. return
35330+ # type) to bigint at analyze time, so a literal/int-typed
35331+ # fall-through last expression must be wrapped before return.
35332+ # The reverse direction covers callers that still want mrb_int.
35333+ if base_type(return_type) == "bigint" && expr_type == "int"
35334+ @needs_bigint = 1
35335+ emit(" return sp_bigint_new_int(" + val + ");")
35336+ elsif base_type(return_type) == "int" && expr_type == "bigint"
35337+ @needs_bigint = 1
35338+ emit(" return sp_bigint_to_int((sp_Bigint *)" + val + ");")
35339+ else
35340+ emit(" return " + val + ";")
35341+ end
3531935342 end
3532035343 else
3532135344 compile_stmt(last)