Commit a19f71d4
a19f71d4a254Merged
Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): don't short-circuit obj.x = v to a direct field write unless x is a registered attr_writer
compile_writer_and_block_call_stmt was unconditionally lowering every
obj.x = v to obj->iv_x = v, regardless of whether x= was actually
declared as a writer. A user-defined def x=(v); …; end whose body did
real work (multiple statements, side effects on other ivars, calls to
helpers, etc.) was silently bypassed — only the field assignment ran.
Add cls_has_attr_writer(ci, bname) (transitive through @cls_parents)
and gate the field-write shortcut on it. When the writer isn't
registered, fall through to method-call dispatch so the user's
def x= body actually executes.
Test: test/no_attr_write_shortcut_multi.rb covers a multi-statement
def logged=(v); @logged = "set:" + v; @real = v.length; end. Such a
body never matched is_simple_writer_method's single-IvarWrite shape
in the first place, so it was never auto-registered as an attr_writer
— meaning this commit alone is enough to fix the multi-statement case.
The single-statement complex case (@x = v * 2) is fixed by the
follow-up commit that tightens the auto-classification.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- a19f71d4a254
- Parent
- 2c82182b70bb
spinel_codegen.rbmodifiedView file
0 end # True when class `ci` (or any of its parents) has registered `bname` as # an attr_writer / attr_accessor or a struct field — i.e. `obj.bname = v` # may safely become a direct field write. def cls_has_attr_writer(ci, bname) if ci < 0 return 0 end writers = @cls_attr_writers[ci].split(";") wi = 0 while wi < writers.length if writers[wi] == bname return 1 end wi = wi + 1 end if @cls_parents[ci] != "" pi = find_class_idx(@cls_parents[ci]) if pi >= 0 return cls_has_attr_writer(pi, bname) end end 0 end def is_value_type_obj(t) if is_obj_type(t) == 1 cname = t[4, t.length - 4] end def compile_writer_and_block_call_stmt(nid, mname, recv) # attr_writer: obj.x = val # attr_writer: obj.x = val — only short-circuit to a direct field # write when `x=` is actually a registered attr_writer on the class # (or transitive parent). Otherwise fall through to method-call # dispatch so a real `def x=(v)` method gets called. if recv >= 0 if mname.length > 1 if mname[mname.length - 1] == "=" bname = mname[0, mname.length - 1] rt = infer_type(recv) if is_obj_type(rt) == 1 rc = compile_expr_gc_rooted(recv) arrow2 = "->" if is_value_type_obj(rt) == 1 arrow2 = "." r_cname = rt[4, rt.length - 4] r_ci = find_class_idx(r_cname) is_writer = 0 if r_ci >= 0 is_writer = cls_has_attr_writer(r_ci, bname) end if is_writer == 1 rc = compile_expr_gc_rooted(recv) arrow2 = "->" if is_value_type_obj(rt) == 1 arrow2 = "." end emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ";") return 1 end emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ";") return 1 end end endtest/no_attr_write_shortcut_multi.rbaddedView file
Loading diff…