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 for LocalVariableWriteNode, LocalVariableOperatorWriteNode, ClassVariable*WriteNode, and GlobalVariableWriteNode re-read the just-written slot back as the function's return value. When --int-overflow=promote widens 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 flowed mrb_int into a sp_Bigint * return position and -Werror killed the build.

  • InstanceVariableWriteNode's generic emit (line 25208) handled the poly slot via box_expr_to_poly but otherwise emitted compile_expr(rhs) raw. With promote-mode ivar slots widened to bigint, a literal/int rhs (@flag = 1, @n = compute_int) needs sp_bigint_new_int. The compile_chained_ivar_writes / compile_initialize_body paths 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

@@ -25209,6 +25209,13 @@
2520925209 val = box_expr_to_poly(expr_id)
2521025210 else
2521125211 val = compile_expr(expr_id)
25212+ # `--int-overflow=promote` widens ivar slots to bigint; an
25213+ # int-typed RHS (literal, arithmetic, etc.) needs the
25214+ # sp_bigint_new_int wrap before assignment.
25215+ if ivt == "bigint" && infer_type(expr_id) == "int"
25216+ @needs_bigint = 1
25217+ val = "sp_bigint_new_int(" + val + ")"
25218+ end
2521225219 end
2521325220 # Check if we're in a module class method
2521425221 mod_ivar = 0
@@ -26834,14 +26841,9 @@
2683426841 # `--int-overflow=promote` widens return slots to bigint; an
2683526842 # explicit `return <int_expr>` needs sp_bigint_new_int wrap.
2683626843 # 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 + ")"
26844+ elsif (base_type(@current_method_return) == "bigint" && rt == "int") || (base_type(@current_method_return) == "int" && rt == "bigint")
26845+ ret_val = maybe_bigint_coerce(ret_val, rt, @current_method_return)
2684026846 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)
2684526847 end
2684626848 if @in_gc_scope == 1
2684726849 # Save return value, run ensure replays (which may mutate
@@ -34984,6 +34986,22 @@
3498434986 end
3498534987 end
3498634988
34989+ # `--int-overflow=promote` widens return slots to bigint while
34990+ # some surrounding slots (cvars, gvars, lvars in arms that don't
34991+ # carry through the promotion) stay int. Coerce when the value's
34992+ # source type differs from the function's declared return type.
34993+ def maybe_bigint_coerce(c_expr, src_t, ret_t)
34994+ if base_type(ret_t) == "bigint" && src_t == "int"
34995+ @needs_bigint = 1
34996+ return "sp_bigint_new_int(" + c_expr + ")"
34997+ end
34998+ if base_type(ret_t) == "int" && src_t == "bigint"
34999+ @needs_bigint = 1
35000+ return "sp_bigint_to_int((sp_Bigint *)" + c_expr + ")"
35001+ end
35002+ c_expr
35003+ end
35004+
3498735005 def compile_body_return_inner(body_id, return_type)
3498835006 if body_id < 0
3498935007 if return_type != "void"
@@ -35237,28 +35255,47 @@
3523735255 if lt == "ClassVariableWriteNode" || lt == "ClassVariableOperatorWriteNode" || lt == "ClassVariableOrWriteNode" || lt == "ClassVariableAndWriteNode"
3523835256 compile_stmt(last)
3523935257 if return_type != "void"
35240- emit(" return cvar_" + cvar_qname(@current_class_idx, @nd_name[last]) + ";")
35258+ qname_cv = cvar_qname(@current_class_idx, @nd_name[last])
35259+ cv_slot_t = "int"
35260+ ci_cv = find_cvar_idx(qname_cv)
35261+ if ci_cv >= 0
35262+ cv_slot_t = @cvar_types[ci_cv]
35263+ end
35264+ emit(" return " + maybe_bigint_coerce("cvar_" + qname_cv, cv_slot_t, return_type) + ";")
3524135265 end
3524235266 return
3524335267 end
3524435268 if lt == "GlobalVariableWriteNode"
3524535269 compile_stmt(last)
3524635270 if return_type != "void"
35247- emit(" return " + sanitize_gvar(@nd_name[last]) + ";")
35271+ gname_cv = @nd_name[last]
35272+ gv_slot_t = "int"
35273+ gi_cv = 0
35274+ while gi_cv < @gvar_names.length
35275+ if @gvar_names[gi_cv] == gname_cv
35276+ gv_slot_t = @gvar_types[gi_cv]
35277+ gi_cv = @gvar_names.length
35278+ else
35279+ gi_cv = gi_cv + 1
35280+ end
35281+ end
35282+ emit(" return " + maybe_bigint_coerce(sanitize_gvar(gname_cv), gv_slot_t, return_type) + ";")
3524835283 end
3524935284 return
3525035285 end
3525135286 if lt == "LocalVariableWriteNode"
3525235287 compile_stmt(last)
3525335288 if return_type != "void"
35254- emit(" return lv_" + @nd_name[last] + ";")
35289+ lv_slot_t = find_var_type(@nd_name[last])
35290+ emit(" return " + maybe_bigint_coerce("lv_" + @nd_name[last], lv_slot_t, return_type) + ";")
3525535291 end
3525635292 return
3525735293 end
3525835294 if lt == "LocalVariableOperatorWriteNode"
3525935295 compile_stmt(last)
3526035296 if return_type != "void"
35261- emit(" return lv_" + @nd_name[last] + ";")
35297+ lv_slot_t = find_var_type(@nd_name[last])
35298+ emit(" return " + maybe_bigint_coerce("lv_" + @nd_name[last], lv_slot_t, return_type) + ";")
3526235299 end
3526335300 return
3526435301 end
@@ -35330,15 +35367,7 @@
3533035367 # type) to bigint at analyze time, so a literal/int-typed
3533135368 # fall-through last expression must be wrapped before return.
3533235369 # 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
35370+ emit(" return " + maybe_bigint_coerce(val, expr_type, return_type) + ";")
3534235371 end
3534335372 else
3534435373 compile_stmt(last)