Commit 45ac5ee6

45ac5ee6dce8Merged

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

Description

Merge pull request #95 from mame/pr/no-attr-write-shortcut

fix(codegen): don't bypass user-defined 'def x=` writers

Details

Commit
45ac5ee6dce8
Parent
d5957bb663d6
Merge parent
fd2dfc45c81e

spinel_codegen.rbmodifiedView file

@@ -3295,6 +3295,30 @@
32953295 0
32963296 end
32973297
3298+ # True when class `ci` (or any of its parents) has registered `bname` as
3299+ # an attr_writer / attr_accessor or a struct field — i.e. `obj.bname = v`
3300+ # may safely become a direct field write.
3301+ def cls_has_attr_writer(ci, bname)
3302+ if ci < 0
3303+ return 0
3304+ end
3305+ writers = @cls_attr_writers[ci].split(";")
3306+ wi = 0
3307+ while wi < writers.length
3308+ if writers[wi] == bname
3309+ return 1
3310+ end
3311+ wi = wi + 1
3312+ end
3313+ if @cls_parents[ci] != ""
3314+ pi = find_class_idx(@cls_parents[ci])
3315+ if pi >= 0
3316+ return cls_has_attr_writer(pi, bname)
3317+ end
3318+ end
3319+ 0
3320+ end
3321+
32983322 def is_value_type_obj(t)
32993323 if is_obj_type(t) == 1
33003324 cname = t[4, t.length - 4]
@@ -9176,33 +9200,41 @@
91769200
91779201 def is_simple_writer_method(mn, bid)
91789202 # Check if method is a simple attr_writer pattern: def x=(v); @x = v; end
9203+ # The RHS must be a bare reference to the parameter — `@x = v * 2`
9204+ # is NOT a simple writer and must not bypass dispatch.
91799205 if mn.length <= 1 || mn[mn.length - 1] != "="
91809206 return 0
91819207 end
91829208 if bid < 0 || bid >= @nd_count
91839209 return 0
91849210 end
9185- # Body should be a StatementsNode with a single InstanceVariableWriteNode
9211+ # Find the single InstanceVariableWriteNode body (directly or wrapped
9212+ # in a StatementsNode of length 1).
91869213 t = @nd_type[bid]
9187- if t == "StatementsNode"
9214+ iv_id = -1
9215+ if t == "InstanceVariableWriteNode"
9216+ iv_id = bid
9217+ elsif t == "StatementsNode"
91889218 stmts = @nd_stmts[bid]
91899219 if stmts != ""
91909220 parts = stmts.split(",")
91919221 if parts.length == 1
91929222 sid = parts[0].to_i
9193- if sid >= 0 && sid < @nd_count
9194- if @nd_type[sid] == "InstanceVariableWriteNode"
9195- return 1
9196- end
9223+ if sid >= 0 && sid < @nd_count && @nd_type[sid] == "InstanceVariableWriteNode"
9224+ iv_id = sid
91979225 end
91989226 end
91999227 end
92009228 end
9201- # Body might be a single InstanceVariableWriteNode directly
9202- if t == "InstanceVariableWriteNode"
9203- return 1
9229+ if iv_id < 0
9230+ return 0
92049231 end
9205- 0
9232+ # RHS must be a bare LocalVariableReadNode for the writer's single param.
9233+ rhs = @nd_value[iv_id]
9234+ if rhs < 0 || @nd_type[rhs] != "LocalVariableReadNode"
9235+ return 0
9236+ end
9237+ 1
92069238 end
92079239
92089240 def cls_has_self_mutating_methods(ci)
@@ -19554,20 +19586,31 @@
1955419586 end
1955519587
1955619588 def compile_writer_and_block_call_stmt(nid, mname, recv)
19557- # attr_writer: obj.x = val
19589+ # attr_writer: obj.x = val — only short-circuit to a direct field
19590+ # write when `x=` is actually a registered attr_writer on the class
19591+ # (or transitive parent). Otherwise fall through to method-call
19592+ # dispatch so a real `def x=(v)` method gets called.
1955819593 if recv >= 0
1955919594 if mname.length > 1
1956019595 if mname[mname.length - 1] == "="
1956119596 bname = mname[0, mname.length - 1]
1956219597 rt = infer_type(recv)
1956319598 if is_obj_type(rt) == 1
19564- rc = compile_expr_gc_rooted(recv)
19565- arrow2 = "->"
19566- if is_value_type_obj(rt) == 1
19567- arrow2 = "."
19599+ r_cname = rt[4, rt.length - 4]
19600+ r_ci = find_class_idx(r_cname)
19601+ is_writer = 0
19602+ if r_ci >= 0
19603+ is_writer = cls_has_attr_writer(r_ci, bname)
19604+ end
19605+ if is_writer == 1
19606+ rc = compile_expr_gc_rooted(recv)
19607+ arrow2 = "->"
19608+ if is_value_type_obj(rt) == 1
19609+ arrow2 = "."
19610+ end
19611+ emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ";")
19612+ return 1
1956819613 end
19569- emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ";")
19570- return 1
1957119614 end
1957219615 end
1957319616 end