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): emitslot = 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'sobj.attr = vsemantics forlocal = 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 = 02228422284
while j < writers.length2228522285
if writers[j] == bname22286-
return "(" + rc + arrow + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ")"22286+
# Box the rhs when the slot is poly so the assignment22287+
# type-matches at C level. Without this, an obj-typed22288+
# rhs flowing into a poly slot emits22289+
# `slot = ptr_value` with a struct LHS — rejected by22290+
# C compile. Use a comma expression so the value of22291+
# the chain is the *original* rhs (typed), preserving22292+
# Ruby's `obj.attr = v` semantics for downstream22293+
# consumers like `local = obj.attr = v` or22294+
# `puts(obj.attr = v)` where `v`'s static type drives22295+
# 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 >= 022302+
arg_ids_w = get_args(args_id_w)22303+
if arg_ids_w.length > 022304+
arg_t = infer_type(arg_ids_w[0])22305+
end22306+
end22307+
boxed = box_value_to_poly(arg_t, arg0_w)22308+
return "(" + rc + arrow + sanitize_ivar(bname) + " = " + boxed + ", " + arg0_w + ")"22309+
end22310+
return "(" + rc + arrow + sanitize_ivar(bname) + " = " + arg0_w + ")"2228722311
end2228822312
j = j + 12228922313
end@@ -26784,7 +26808,22 @@
2678426808
if is_value_type_obj(rt) == 12678526809
arrow2 = "."2678626810
end26787-
emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ";")26811+
# Box rhs when the slot is poly, same as the26812+
# 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 >= 026819+
arg_ids_w = get_args(args_id_w)26820+
if arg_ids_w.length > 026821+
arg_t = infer_type(arg_ids_w[0])26822+
end26823+
end26824+
arg0_w = box_value_to_poly(arg_t, arg0_w)26825+
end26826+
emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + arg0_w + ";")2678826827
return 12678926828
end2679026829
endtest/attr_writer_poly_box.rbaddedView file
Loading diff…