Commit 2bcd25fa

2bcd25fac325Merged

Ori Pekelman <[email protected]> · 6 months ago · 3 files · ~3 modified

Description

fix(codegen): unbox setter rhs when slot type is concrete (poly arm)

Follow-up to the previous attr_writer-on-poly-receiver fix, surfaced by review feedback (gemini-code-assist on PR #388).

When the new arm fires for a setter call whose function parameter widened to poly -- because the same setter is called with two differently-typed args from divergent call sites -- the rhs needs to be unboxed to the slot's concrete C type for each cls_id arm. Without this, a slot of type int (mrb_int iv_slot) ended up with the poly's sp_RbVal lv_v assigned directly, which fails C compilation.

Three rhs-fitting cases now:

slot poly + arg concrete -> box the arg (existing path) slot concrete + arg poly -> unbox the arg via unbox_poly_to (new) else -> direct assign

Test (test/poly_attr_writer.rb) gains a third subcase: IBox#slot:int and SBox#slot:String sharing the slot name, with set_slot(o, v) called for both. Pre-fix this didn't even compile; with the unbox path, each cls_id arm picks the right concrete shape.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
2bcd25fac325
Parent
91917fd23f4c

spinel_codegen.rbmodifiedView file

@@ -28648,20 +28648,32 @@
2864828648 # through silently, and the assignment never executes —
2864928649 # `obj.x = v` becomes a no-op.
2865028650 bname = mname[0, mname.length - 1]
28651- val_expr = arg_compiled.length > 0 ? arg_compiled[0] : "0"
28652- slot_t = cls_ivar_type(i, "@" + bname)
28653- if slot_t == "poly" && arg_types.length > 0 && arg_types[0] != "poly" && arg_types[0] != ""
28654- val_expr = box_value_to_poly(arg_types[0], val_expr)
28651+ raw_val = arg_compiled.length > 0 ? arg_compiled[0] : "0"
28652+ raw_t = arg_types.length > 0 ? arg_types[0] : ""
28653+ slot_t = cls_ivar_type(i, "@" + bname)
28654+ # Match the rhs to the slot's concrete C type for *this arm*.
28655+ # Three cases:
28656+ # slot poly + arg concrete -> box the arg.
28657+ # slot concrete + arg poly -> unbox the arg.
28658+ # else -> direct assign.
28659+ # Different arms in the dispatch may take different branches
28660+ # (e.g. one slot is `int`, another is `string`).
28661+ store_val = raw_val
28662+ if slot_t == "poly" && raw_t != "poly" && raw_t != ""
28663+ store_val = box_value_to_poly(raw_t, raw_val)
28664+ elsif slot_t != "poly" && raw_t == "poly"
28665+ store_val = unbox_poly_to(slot_t, raw_val)
2865528666 end
2865628667 ivar_lhs = "((sp_" + cname + " *)" + recv_tmp + ".v.p)->" + sanitize_ivar("@" + bname)
28657- # `x = v` returns v in Ruby; capture it in `tmp` for
28658- # expression-position use, but the primary effect is the
28659- # ivar write.
28660- rhs_for_tmp = val_expr
28668+ # `x = v` returns v in Ruby. The result tmp's type is set by
28669+ # poly_dispatch_return_type (poly when slot types diverge,
28670+ # otherwise the common slot type). Box / coerce the stored
28671+ # value into the tmp's expected shape.
28672+ rhs_for_tmp = store_val
2866128673 if is_poly_ret == 1
28662- rhs_for_tmp = box_val_to_poly(val_expr, slot_t)
28674+ rhs_for_tmp = box_val_to_poly(store_val, slot_t)
2866328675 end
28664- emit(" if (" + recv_tmp + ".cls_id == " + i.to_s + ") { " + ivar_lhs + " = " + val_expr + "; " + tmp + " = " + rhs_for_tmp + "; }")
28676+ emit(" if (" + recv_tmp + ".cls_id == " + i.to_s + ") { " + ivar_lhs + " = " + store_val + "; " + tmp + " = " + rhs_for_tmp + "; }")
2866528677 end
2866628678 i = i + 1
2866728679 end