Commit cecd16e7

cecd16e7960aMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

Merge pull request #253 from mame/pr/attr-writer-box-poly

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

Details

Commit
cecd16e7960a
Parent
9f6587ba9e30
Merge parent
83f9bd866c8e

spinel_codegen.rbmodifiedView file

@@ -22298,7 +22298,31 @@
2229822298 j = 0
2229922299 while j < writers.length
2230022300 if writers[j] == bname
22301- return "(" + rc + arrow + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ")"
22301+ # Box the rhs when the slot is poly so the assignment
22302+ # type-matches at C level. Without this, an obj-typed
22303+ # rhs flowing into a poly slot emits
22304+ # `slot = ptr_value` with a struct LHS — rejected by
22305+ # C compile. Use a comma expression so the value of
22306+ # the chain is the *original* rhs (typed), preserving
22307+ # Ruby's `obj.attr = v` semantics for downstream
22308+ # consumers like `local = obj.attr = v` or
22309+ # `puts(obj.attr = v)` where `v`'s static type drives
22310+ # the formatter / cast.
22311+ slot_t = cls_ivar_type(ci, "@" + bname)
22312+ args_id_w = @nd_arguments[nid]
22313+ arg0_w = compile_arg0(nid)
22314+ if slot_t == "poly"
22315+ arg_t = "int"
22316+ if args_id_w >= 0
22317+ arg_ids_w = get_args(args_id_w)
22318+ if arg_ids_w.length > 0
22319+ arg_t = infer_type(arg_ids_w[0])
22320+ end
22321+ end
22322+ boxed = box_value_to_poly(arg_t, arg0_w)
22323+ return "(" + rc + arrow + sanitize_ivar(bname) + " = " + boxed + ", " + arg0_w + ")"
22324+ end
22325+ return "(" + rc + arrow + sanitize_ivar(bname) + " = " + arg0_w + ")"
2230222326 end
2230322327 j = j + 1
2230422328 end
@@ -26804,7 +26828,22 @@
2680426828 if is_value_type_obj(rt) == 1
2680526829 arrow2 = "."
2680626830 end
26807- emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ";")
26831+ # Box rhs when the slot is poly, same as the
26832+ # expression-form attr_writer above.
26833+ slot_t = cls_ivar_type(r_ci, "@" + bname)
26834+ arg0_w = compile_arg0(nid)
26835+ if slot_t == "poly"
26836+ args_id_w = @nd_arguments[nid]
26837+ arg_t = "int"
26838+ if args_id_w >= 0
26839+ arg_ids_w = get_args(args_id_w)
26840+ if arg_ids_w.length > 0
26841+ arg_t = infer_type(arg_ids_w[0])
26842+ end
26843+ end
26844+ arg0_w = box_value_to_poly(arg_t, arg0_w)
26845+ end
26846+ emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + arg0_w + ";")
2680826847 return 1
2680926848 end
2681026849 end