Commit 83f9bd86

83f9bd866c8eMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): attr-writer boxes rhs when slot is poly

obj.attr = v codegen emitted slot = arg0 regardless of slot type. When the slot is poly (sp_RbVal — widened by heterogeneous writes) and the rhs is typed (int / string / obj_X / ...), C rejects the assignment as a struct-from-scalar/pointer mismatch.

Both forms of the attr-writer call now look up the slot's class type via cls_ivar_type and route the rhs through box_value_to_poly when the slot is poly:

  • Statement form (compile_call_stmt): emit slot = box_value_to_poly(arg_t, arg0);
  • Expression form (compile_object_method_expr): emit (slot = box_value_to_poly(arg_t, arg0), arg0) so the chain's value remains the original (typed) rhs — preserving Ruby's obj.attr = v semantics for local = obj.attr = v / puts(obj.attr = v) etc.

Non-poly slots keep the existing direct assignment (and the existing return-rhs shape).

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
83f9bd866c8e
Parent
a8c8ac8156dc

spinel_codegen.rbmodifiedView file

@@ -22283,7 +22283,31 @@
2228322283 j = 0
2228422284 while j < writers.length
2228522285 if writers[j] == bname
22286- return "(" + rc + arrow + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ")"
22286+ # Box the rhs when the slot is poly so the assignment
22287+ # type-matches at C level. Without this, an obj-typed
22288+ # rhs flowing into a poly slot emits
22289+ # `slot = ptr_value` with a struct LHS — rejected by
22290+ # C compile. Use a comma expression so the value of
22291+ # the chain is the *original* rhs (typed), preserving
22292+ # Ruby's `obj.attr = v` semantics for downstream
22293+ # consumers like `local = obj.attr = v` or
22294+ # `puts(obj.attr = v)` where `v`'s static type drives
22295+ # the formatter / cast.
22296+ slot_t = cls_ivar_type(ci, "@" + bname)
22297+ args_id_w = @nd_arguments[nid]
22298+ arg0_w = compile_arg0(nid)
22299+ if slot_t == "poly"
22300+ arg_t = "int"
22301+ if args_id_w >= 0
22302+ arg_ids_w = get_args(args_id_w)
22303+ if arg_ids_w.length > 0
22304+ arg_t = infer_type(arg_ids_w[0])
22305+ end
22306+ end
22307+ boxed = box_value_to_poly(arg_t, arg0_w)
22308+ return "(" + rc + arrow + sanitize_ivar(bname) + " = " + boxed + ", " + arg0_w + ")"
22309+ end
22310+ return "(" + rc + arrow + sanitize_ivar(bname) + " = " + arg0_w + ")"
2228722311 end
2228822312 j = j + 1
2228922313 end
@@ -26784,7 +26808,22 @@
2678426808 if is_value_type_obj(rt) == 1
2678526809 arrow2 = "."
2678626810 end
26787- emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ";")
26811+ # Box rhs when the slot is poly, same as the
26812+ # expression-form attr_writer above.
26813+ slot_t = cls_ivar_type(r_ci, "@" + bname)
26814+ arg0_w = compile_arg0(nid)
26815+ if slot_t == "poly"
26816+ args_id_w = @nd_arguments[nid]
26817+ arg_t = "int"
26818+ if args_id_w >= 0
26819+ arg_ids_w = get_args(args_id_w)
26820+ if arg_ids_w.length > 0
26821+ arg_t = infer_type(arg_ids_w[0])
26822+ end
26823+ end
26824+ arg0_w = box_value_to_poly(arg_t, arg0_w)
26825+ end
26826+ emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + arg0_w + ";")
2678826827 return 1
2678926828 end
2679026829 end