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

@@ -3257,6 +3257,30 @@
32573257 0
32583258 end
32593259
3260+ # True when class `ci` (or any of its parents) has registered `bname` as
3261+ # an attr_writer / attr_accessor or a struct field — i.e. `obj.bname = v`
3262+ # may safely become a direct field write.
3263+ def cls_has_attr_writer(ci, bname)
3264+ if ci < 0
3265+ return 0
3266+ end
3267+ writers = @cls_attr_writers[ci].split(";")
3268+ wi = 0
3269+ while wi < writers.length
3270+ if writers[wi] == bname
3271+ return 1
3272+ end
3273+ wi = wi + 1
3274+ end
3275+ if @cls_parents[ci] != ""
3276+ pi = find_class_idx(@cls_parents[ci])
3277+ if pi >= 0
3278+ return cls_has_attr_writer(pi, bname)
3279+ end
3280+ end
3281+ 0
3282+ end
3283+
32603284 def is_value_type_obj(t)
32613285 if is_obj_type(t) == 1
32623286 cname = t[4, t.length - 4]
@@ -19392,20 +19416,31 @@
1939219416 end
1939319417
1939419418 def compile_writer_and_block_call_stmt(nid, mname, recv)
19395- # attr_writer: obj.x = val
19419+ # attr_writer: obj.x = val — only short-circuit to a direct field
19420+ # write when `x=` is actually a registered attr_writer on the class
19421+ # (or transitive parent). Otherwise fall through to method-call
19422+ # dispatch so a real `def x=(v)` method gets called.
1939619423 if recv >= 0
1939719424 if mname.length > 1
1939819425 if mname[mname.length - 1] == "="
1939919426 bname = mname[0, mname.length - 1]
1940019427 rt = infer_type(recv)
1940119428 if is_obj_type(rt) == 1
19402- rc = compile_expr_gc_rooted(recv)
19403- arrow2 = "->"
19404- if is_value_type_obj(rt) == 1
19405- arrow2 = "."
19429+ r_cname = rt[4, rt.length - 4]
19430+ r_ci = find_class_idx(r_cname)
19431+ is_writer = 0
19432+ if r_ci >= 0
19433+ is_writer = cls_has_attr_writer(r_ci, bname)
19434+ end
19435+ if is_writer == 1
19436+ rc = compile_expr_gc_rooted(recv)
19437+ arrow2 = "->"
19438+ if is_value_type_obj(rt) == 1
19439+ arrow2 = "."
19440+ end
19441+ emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ";")
19442+ return 1
1940619443 end
19407- emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ";")
19408- return 1
1940919444 end
1941019445 end
1941119446 end