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

@@ -28336,6 +28336,15 @@
2833628336 @needs_rb_value = 1
2833728337 return "poly"
2833828338 end
28339+ # Setters: mname ends with "=" and at least one class has an
28340+ # attr_writer for the bare name. Return type is the ivar type
28341+ # (Ruby returns the rhs from `x = v`); without this, the result
28342+ # tmp's C type defaults to `mrb_int` and `tmp = rhs` mismatches
28343+ # for non-int slots.
28344+ setter_bname = ""
28345+ if mname.length > 1 && mname[mname.length - 1] == "="
28346+ setter_bname = mname[0, mname.length - 1]
28347+ end
2833928348 common = ""
2834028349 ci = 0
2834128350 while ci < @cls_names.length
@@ -28345,6 +28354,9 @@
2834528354 elsif cls_has_attr_reader(ci, mname) == 1
2834628355 # An attr_reader returns the ivar type. Issue #119.
2834728356 rt = cls_ivar_type(ci, "@" + mname)
28357+ elsif setter_bname != "" && cls_has_attr_writer(ci, setter_bname) == 1
28358+ # An attr_writer setter returns the ivar's type.
28359+ rt = cls_ivar_type(ci, "@" + setter_bname)
2834828360 end
2834928361 if rt != ""
2835028362 if common == ""
@@ -28629,6 +28641,27 @@
2862928641 else
2863028642 emit(" if (" + recv_tmp + ".cls_id == " + i.to_s + ") " + tmp + " = " + rhs + ";")
2863128643 end
28644+ elsif mname.length > 1 && mname[mname.length - 1] == "=" && cls_has_attr_writer(i, mname[0, mname.length - 1]) == 1
28645+ # An auto-registered attr_writer setter (`obj.x = v`) on a
28646+ # poly-typed receiver. Without this arm the cls_id dispatch
28647+ # finds neither a real method nor an attr_reader, falls
28648+ # through silently, and the assignment never executes —
28649+ # `obj.x = v` becomes a no-op.
28650+ 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)
28655+ end
28656+ 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
28661+ if is_poly_ret == 1
28662+ rhs_for_tmp = box_val_to_poly(val_expr, slot_t)
28663+ end
28664+ emit(" if (" + recv_tmp + ".cls_id == " + i.to_s + ") { " + ivar_lhs + " = " + val_expr + "; " + tmp + " = " + rhs_for_tmp + "; }")
2863228665 end
2863328666 i = i + 1
2863428667 end