Commit c95f194b
c95f194ba533Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): coerce ivar/cvar/gvar/lvar return + ivar write to bigint
Builds on the prior return-coercion fix to cover the remaining non-direct-expression return paths and the ivar write site.
-
compile_body_return_inner's arms forLocalVariableWriteNode,LocalVariableOperatorWriteNode,ClassVariable*WriteNode, andGlobalVariableWriteNodere-read the just-written slot back as the function's return value. When--int-overflow=promotewidens the return type to bigint but the slot stays int (cvars and gvars aren't slot-promoted; lvars may be int when the analyzer's per- body promotion missed them), the unwrapped read flowedmrb_intinto asp_Bigint *return position and-Werrorkilled the build. -
InstanceVariableWriteNode's generic emit (line 25208) handled thepolyslot viabox_expr_to_polybut otherwise emittedcompile_expr(rhs)raw. With promote-mode ivar slots widened to bigint, a literal/int rhs (@flag = 1,@n = compute_int) needssp_bigint_new_int. Thecompile_chained_ivar_writes/compile_initialize_bodypaths already had this coercion; the catch-all did not.
Add a maybe_bigint_coerce(c_expr, src_t, dst_t) helper so the
five return arms + the explicit-ReturnNode site share one rule:
int -> bigint wraps via sp_bigint_new_int, bigint -> int
unwraps via sp_bigint_to_int, otherwise pass-through.
Promote-mode test count: 409 pass / 35 fail / 174 error -> 431 pass / 38 fail / 149 error. Default raise mode unchanged at 618 / 0 / 0.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- c95f194ba533
- Parent
- 3e3066f1c821
spinel_codegen.rbmodifiedView file
val = box_expr_to_poly(expr_id) else val = compile_expr(expr_id) # `--int-overflow=promote` widens ivar slots to bigint; an # int-typed RHS (literal, arithmetic, etc.) needs the # sp_bigint_new_int wrap before assignment. if ivt == "bigint" && infer_type(expr_id) == "int" @needs_bigint = 1 val = "sp_bigint_new_int(" + val + ")" end end # Check if we're in a module class method mod_ivar = 0 # `--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 + ")" elsif (base_type(@current_method_return) == "bigint" && rt == "int") || (base_type(@current_method_return) == "int" && rt == "bigint") ret_val = maybe_bigint_coerce(ret_val, rt, @current_method_return) 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 # `--int-overflow=promote` widens return slots to bigint while # some surrounding slots (cvars, gvars, lvars in arms that don't # carry through the promotion) stay int. Coerce when the value's # source type differs from the function's declared return type. def maybe_bigint_coerce(c_expr, src_t, ret_t) if base_type(ret_t) == "bigint" && src_t == "int" @needs_bigint = 1 return "sp_bigint_new_int(" + c_expr + ")" end if base_type(ret_t) == "int" && src_t == "bigint" @needs_bigint = 1 return "sp_bigint_to_int((sp_Bigint *)" + c_expr + ")" end c_expr end def compile_body_return_inner(body_id, return_type) if body_id < 0 if return_type != "void" if lt == "ClassVariableWriteNode" || lt == "ClassVariableOperatorWriteNode" || lt == "ClassVariableOrWriteNode" || lt == "ClassVariableAndWriteNode" compile_stmt(last) if return_type != "void" emit(" return cvar_" + cvar_qname(@current_class_idx, @nd_name[last]) + ";") qname_cv = cvar_qname(@current_class_idx, @nd_name[last]) cv_slot_t = "int" ci_cv = find_cvar_idx(qname_cv) if ci_cv >= 0 cv_slot_t = @cvar_types[ci_cv] end emit(" return " + maybe_bigint_coerce("cvar_" + qname_cv, cv_slot_t, return_type) + ";") end return end if lt == "GlobalVariableWriteNode" compile_stmt(last) if return_type != "void" emit(" return " + sanitize_gvar(@nd_name[last]) + ";") gname_cv = @nd_name[last] gv_slot_t = "int" gi_cv = 0 while gi_cv < @gvar_names.length if @gvar_names[gi_cv] == gname_cv gv_slot_t = @gvar_types[gi_cv] gi_cv = @gvar_names.length else gi_cv = gi_cv + 1 end end emit(" return " + maybe_bigint_coerce(sanitize_gvar(gname_cv), gv_slot_t, return_type) + ";") end return end if lt == "LocalVariableWriteNode" compile_stmt(last) if return_type != "void" emit(" return lv_" + @nd_name[last] + ";") lv_slot_t = find_var_type(@nd_name[last]) emit(" return " + maybe_bigint_coerce("lv_" + @nd_name[last], lv_slot_t, return_type) + ";") end return end if lt == "LocalVariableOperatorWriteNode" compile_stmt(last) if return_type != "void" emit(" return lv_" + @nd_name[last] + ";") lv_slot_t = find_var_type(@nd_name[last]) emit(" return " + maybe_bigint_coerce("lv_" + @nd_name[last], lv_slot_t, return_type) + ";") end return end # 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 emit(" return " + maybe_bigint_coerce(val, expr_type, return_type) + ";") end else compile_stmt(last)