Commit 8964075e
8964075ee5d1Merged
Ori Pekelman <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): emit ivar-write arm for attr_writer on poly receiver
A setter call obj.x = v on a parameter whose static type is a
class union (one or more classes have an attr_writer :x, others
don't) silently no-ops: the dispatch loop in compile_call_expr's
SP_TAG_OBJ block recognises only explicit user methods and
attr_readers. Setters fall through with no cls_id arm, so
if (.tag == SP_TAG_OBJ) {} ends up empty and the store never
executes.
Add a third arm that recognises the attr_writer shape and emits a
direct ((sp_C *)recv.v.p)->iv_x = v for the matching cls_id.
Also extend poly_dispatch_return_type to recognise setters so the
result temp's C type matches the ivar slot type (Ruby's x = v
returns v, not int) — without this, the new arm's _t = v;
mistypes when the slot is e.g. a string.
Repro lives in test/poly_attr_writer.rb. Surfaced while building
the tep Sinatra-clone; res.body = out was a no-op when res
widened to Request | Response (both having the ivar).
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 8964075ee5d1
- Parent
- dcd875a5b754
spinel_codegen.rbmodifiedView file
@needs_rb_value = 1 return "poly" end # Setters: mname ends with "=" and at least one class has an # attr_writer for the bare name. Return type is the ivar type # (Ruby returns the rhs from `x = v`); without this, the result # tmp's C type defaults to `mrb_int` and `tmp = rhs` mismatches # for non-int slots. setter_bname = "" if mname.length > 1 && mname[mname.length - 1] == "=" setter_bname = mname[0, mname.length - 1] end common = "" ci = 0 while ci < @cls_names.length elsif cls_has_attr_reader(ci, mname) == 1 # An attr_reader returns the ivar type. Issue #119. rt = cls_ivar_type(ci, "@" + mname) elsif setter_bname != "" && cls_has_attr_writer(ci, setter_bname) == 1 # An attr_writer setter returns the ivar's type. rt = cls_ivar_type(ci, "@" + setter_bname) end if rt != "" if common == "" else emit(" if (" + recv_tmp + ".cls_id == " + i.to_s + ") " + tmp + " = " + rhs + ";") end elsif mname.length > 1 && mname[mname.length - 1] == "=" && cls_has_attr_writer(i, mname[0, mname.length - 1]) == 1 # An auto-registered attr_writer setter (`obj.x = v`) on a # poly-typed receiver. Without this arm the cls_id dispatch # finds neither a real method nor an attr_reader, falls # through silently, and the assignment never executes — # `obj.x = v` becomes a no-op. bname = mname[0, mname.length - 1] val_expr = arg_compiled.length > 0 ? arg_compiled[0] : "0" slot_t = cls_ivar_type(i, "@" + bname) if slot_t == "poly" && arg_types.length > 0 && arg_types[0] != "poly" && arg_types[0] != "" val_expr = box_value_to_poly(arg_types[0], val_expr) end ivar_lhs = "((sp_" + cname + " *)" + recv_tmp + ".v.p)->" + sanitize_ivar("@" + bname) # `x = v` returns v in Ruby; capture it in `tmp` for # expression-position use, but the primary effect is the # ivar write. rhs_for_tmp = val_expr if is_poly_ret == 1 rhs_for_tmp = box_val_to_poly(val_expr, slot_t) end emit(" if (" + recv_tmp + ".cls_id == " + i.to_s + ") { " + ivar_lhs + " = " + val_expr + "; " + tmp + " = " + rhs_for_tmp + "; }") end i = i + 1 endtest/poly_attr_writer.rbaddedView file
Loading diff…
test/poly_attr_writer.rb.expectedaddedView file
Loading diff…