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
032963296
end32973297
3298+
# True when class `ci` (or any of its parents) has registered `bname` as3299+
# 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 < 03303+
return 03304+
end3305+
writers = @cls_attr_writers[ci].split(";")3306+
wi = 03307+
while wi < writers.length3308+
if writers[wi] == bname3309+
return 13310+
end3311+
wi = wi + 13312+
end3313+
if @cls_parents[ci] != ""3314+
pi = find_class_idx(@cls_parents[ci])3315+
if pi >= 03316+
return cls_has_attr_writer(pi, bname)3317+
end3318+
end3319+
03320+
end3321+
32983322
def is_value_type_obj(t)32993323
if is_obj_type(t) == 133003324
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; end9203+
# 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 091819207
end91829208
if bid < 0 || bid >= @nd_count91839209
return 091849210
end9185-
# Body should be a StatementsNode with a single InstanceVariableWriteNode9211+
# Find the single InstanceVariableWriteNode body (directly or wrapped9212+
# in a StatementsNode of length 1).91869213
t = @nd_type[bid]9187-
if t == "StatementsNode"9214+
iv_id = -19215+
if t == "InstanceVariableWriteNode"9216+
iv_id = bid9217+
elsif t == "StatementsNode"91889218
stmts = @nd_stmts[bid]91899219
if stmts != ""91909220
parts = stmts.split(",")91919221
if parts.length == 191929222
sid = parts[0].to_i9193-
if sid >= 0 && sid < @nd_count9194-
if @nd_type[sid] == "InstanceVariableWriteNode"9195-
return 19196-
end9223+
if sid >= 0 && sid < @nd_count && @nd_type[sid] == "InstanceVariableWriteNode"9224+
iv_id = sid91979225
end91989226
end91999227
end92009228
end9201-
# Body might be a single InstanceVariableWriteNode directly9202-
if t == "InstanceVariableWriteNode"9203-
return 19229+
if iv_id < 09230+
return 092049231
end9205-
09232+
# 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 09236+
end9237+
192069238
end92079239
92089240
def cls_has_self_mutating_methods(ci)@@ -19554,20 +19586,31 @@
1955419586
end1955519587
1955619588
def compile_writer_and_block_call_stmt(nid, mname, recv)19557-
# attr_writer: obj.x = val19589+
# attr_writer: obj.x = val — only short-circuit to a direct field19590+
# write when `x=` is actually a registered attr_writer on the class19591+
# (or transitive parent). Otherwise fall through to method-call19592+
# dispatch so a real `def x=(v)` method gets called.1955819593
if recv >= 01955919594
if mname.length > 11956019595
if mname[mname.length - 1] == "="1956119596
bname = mname[0, mname.length - 1]1956219597
rt = infer_type(recv)1956319598
if is_obj_type(rt) == 119564-
rc = compile_expr_gc_rooted(recv)19565-
arrow2 = "->"19566-
if is_value_type_obj(rt) == 119567-
arrow2 = "."19599+
r_cname = rt[4, rt.length - 4]19600+
r_ci = find_class_idx(r_cname)19601+
is_writer = 019602+
if r_ci >= 019603+
is_writer = cls_has_attr_writer(r_ci, bname)19604+
end19605+
if is_writer == 119606+
rc = compile_expr_gc_rooted(recv)19607+
arrow2 = "->"19608+
if is_value_type_obj(rt) == 119609+
arrow2 = "."19610+
end19611+
emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ";")19612+
return 11956819613
end19569-
emit(" " + rc + arrow2 + sanitize_ivar(bname) + " = " + compile_arg0(nid) + ";")19570-
return 11957119614
end1957219615
end1957319616
endtest/no_attr_write_shortcut_complex.rbaddedView file
Loading diff…
test/no_attr_write_shortcut_multi.rbaddedView file
Loading diff…