Commit fd2dfc45
fd2dfc45c81eMerged
Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): tighten is_simple_writer_method to require a bare param ref on the RHS
is_simple_writer_method auto-classifies a user-written
def x=(v); @x = v; end as behaviorally equivalent to attr_writer :x
and registers it in @cls_attr_writers, so the call site can take the
field-write fast path. The classification only checked the body's outer
shape (single InstanceVariableWriteNode) without looking at the RHS,
so
def doubled=(v)
@doubled = v * 2
end
was misclassified — its body is still one InstanceVariableWriteNode,
just with a CallNode as the value. After the prior commit added the
cls_has_attr_writer check at the call site, obj.doubled = 5 looked
up the (wrongly-)registered writer and still short-circuited to the
field write, dropping the * 2.
Add an RHS check: the assignment value must be a bare
LocalVariableReadNode. Anything else (literals, computations, method
calls) is no longer auto-registered as a simple writer. Existing
hand-written def x=(v); @x = v; end writers are unaffected.
Test: test/no_attr_write_shortcut_complex.rb covers the single-stmt
complex case (@doubled = v * 2).
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- fd2dfc45c81e
- Parent
- a19f71d4a254
spinel_codegen.rbmodifiedView file
def is_simple_writer_method(mn, bid) # Check if method is a simple attr_writer pattern: def x=(v); @x = v; end # The RHS must be a bare reference to the parameter — `@x = v * 2` # is NOT a simple writer and must not bypass dispatch. if mn.length <= 1 || mn[mn.length - 1] != "=" return 0 end if bid < 0 || bid >= @nd_count return 0 end # Body should be a StatementsNode with a single InstanceVariableWriteNode # Find the single InstanceVariableWriteNode body (directly or wrapped # in a StatementsNode of length 1). t = @nd_type[bid] if t == "StatementsNode" iv_id = -1 if t == "InstanceVariableWriteNode" iv_id = bid elsif t == "StatementsNode" stmts = @nd_stmts[bid] if stmts != "" parts = stmts.split(",") if parts.length == 1 sid = parts[0].to_i if sid >= 0 && sid < @nd_count if @nd_type[sid] == "InstanceVariableWriteNode" return 1 end if sid >= 0 && sid < @nd_count && @nd_type[sid] == "InstanceVariableWriteNode" iv_id = sid end end end end # Body might be a single InstanceVariableWriteNode directly if t == "InstanceVariableWriteNode" return 1 if iv_id < 0 return 0 end # RHS must be a bare LocalVariableReadNode for the writer's single param. rhs = @nd_value[iv_id] if rhs < 0 || @nd_type[rhs] != "LocalVariableReadNode" return 0 end 0 1 end def cls_has_self_mutating_methods(ci)test/no_attr_write_shortcut_complex.rbaddedView file
Loading diff…