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

@@ -9116,33 +9116,41 @@
91169116
91179117 def is_simple_writer_method(mn, bid)
91189118 # Check if method is a simple attr_writer pattern: def x=(v); @x = v; end
9119+ # The RHS must be a bare reference to the parameter — `@x = v * 2`
9120+ # is NOT a simple writer and must not bypass dispatch.
91199121 if mn.length <= 1 || mn[mn.length - 1] != "="
91209122 return 0
91219123 end
91229124 if bid < 0 || bid >= @nd_count
91239125 return 0
91249126 end
9125- # Body should be a StatementsNode with a single InstanceVariableWriteNode
9127+ # Find the single InstanceVariableWriteNode body (directly or wrapped
9128+ # in a StatementsNode of length 1).
91269129 t = @nd_type[bid]
9127- if t == "StatementsNode"
9130+ iv_id = -1
9131+ if t == "InstanceVariableWriteNode"
9132+ iv_id = bid
9133+ elsif t == "StatementsNode"
91289134 stmts = @nd_stmts[bid]
91299135 if stmts != ""
91309136 parts = stmts.split(",")
91319137 if parts.length == 1
91329138 sid = parts[0].to_i
9133- if sid >= 0 && sid < @nd_count
9134- if @nd_type[sid] == "InstanceVariableWriteNode"
9135- return 1
9136- end
9139+ if sid >= 0 && sid < @nd_count && @nd_type[sid] == "InstanceVariableWriteNode"
9140+ iv_id = sid
91379141 end
91389142 end
91399143 end
91409144 end
9141- # Body might be a single InstanceVariableWriteNode directly
9142- if t == "InstanceVariableWriteNode"
9143- return 1
9145+ if iv_id < 0
9146+ return 0
9147+ end
9148+ # RHS must be a bare LocalVariableReadNode for the writer's single param.
9149+ rhs = @nd_value[iv_id]
9150+ if rhs < 0 || @nd_type[rhs] != "LocalVariableReadNode"
9151+ return 0
91449152 end
9145- 0
9153+ 1
91469154 end
91479155
91489156 def cls_has_self_mutating_methods(ci)