Commit c4d4d683

c4d4d683f472Merged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): default-arg inlining honors callee class scope

compile_typed_call_args substitutes the callee's default-arg expression for omitted trailing args directly into the caller's C code. The default was authored in the callee's class scope — def grab(target = @base * 10) reads @base against the callee's instance — but the inlined expression was compiled under the caller's @current_class_idx and emitted self->iv_base against the caller's self, which either fails to compile (caller has no iv_base) or reads the wrong slot.

Save and switch @current_class_idx to the callee for the duration of the inline expansion, and introduce @self_override that self_arrow consults so self->iv_X rewrites as (<recv>)->iv_X. The recv is materialized via compile_expr_gc_rooted (parens-wrapped) so any cast or operator chain stays self-contained when followed by ->.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
c4d4d683f472
Parent
78de785dc7bf

spinel_codegen.rbmodifiedView file

@@ -177,6 +177,12 @@
177177 @instance_eval_self_var = ""
178178 @instance_eval_self_type = ""
179179
180+ # During default-arg substitution, when the callee's default
181+ # expression is inlined into the caller, `self_arrow` consults
182+ # this override to route `self->iv_X` against the call's
183+ # explicit receiver instead of the caller's self.
184+ @self_override = ""
185+
180186 # Yield/block tracking (parallel with meth_names / cls_meth_names)
181187 @meth_has_yield = []
182188 @cls_meth_has_yield = "".split(",")
@@ -10553,6 +10559,18 @@
1055310559 end
1055410560
1055510561 def self_arrow
10562+ # `@self_override` lets default-arg substitution redirect `self`
10563+ # to an explicit receiver expression: when a callee's default
10564+ # expression is inlined into the caller, `self` would otherwise
10565+ # resolve to the caller's `self`, but the default was authored
10566+ # against the callee's instance. Store the bare expression
10567+ # (e.g. `lv_f`) and append the arrow / dot here.
10568+ if @self_override != nil && @self_override != ""
10569+ if @current_class_idx >= 0 && @cls_is_value_type[@current_class_idx] == 1
10570+ return @self_override + "."
10571+ end
10572+ return @self_override + "->"
10573+ end
1055610574 if @current_class_idx >= 0
1055710575 if @cls_is_value_type[@current_class_idx] == 1
1055810576 return "self."
@@ -10561,6 +10579,17 @@
1056110579 "self->"
1056210580 end
1056310581
10582+ # The C expression that should be used wherever bare `self`
10583+ # would normally appear (e.g. as the first arg to a same-class
10584+ # method dispatch). Defaults to `"self"`; default-arg inlining
10585+ # overrides it via `@self_override`.
10586+ def self_expr
10587+ if @self_override != nil && @self_override != ""
10588+ return @self_override
10589+ end
10590+ "self"
10591+ end
10592+
1056410593 def subtree_has_ivar_write(nid)
1056510594 if nid < 0 || nid >= @nd_count
1056610595 return 0
@@ -15651,7 +15680,7 @@
1565115680 bp = "0"
1565215681 end
1565315682 end
15654- return "sp_" + owner + "_" + sanitize_name(mname) + "(self" + build_call_tail(ca, bp) + ")"
15683+ return "sp_" + owner + "_" + sanitize_name(mname) + "(" + self_expr + build_call_tail(ca, bp) + ")"
1565515684 end
1565615685 # Check attr_readers (bare method call like `x` meaning self.x)
1565715686 readers = @cls_attr_readers[@current_class_idx].split(";")
@@ -20246,11 +20275,31 @@
2024620275 end
2024720276 result = result + aexpr
2024820277 else
20249- # Caller omitted this trailing arg — emit the method's default.
20278+ # Caller omitted this trailing arg — emit the method's
20279+ # default expression. The default was authored in the
20280+ # callee's class scope, so any `self` / `@ivar` references
20281+ # need to resolve against the call's receiver, not the
20282+ # caller's self. Switch @current_class_idx and override
20283+ # `self_arrow` for the duration of the inline expansion.
2025020284 if k < defaults.length
2025120285 def_id = defaults[k].to_i
2025220286 if def_id >= 0
20287+ recv_for_default = @nd_receiver[nid]
20288+ saved_ci = @current_class_idx
20289+ saved_override = @self_override
20290+ if recv_for_default >= 0
20291+ # Materialize the recv into a C expression. Wrap in
20292+ # parens so any cast/operator chain stays self-
20293+ # contained when consumed as `(<recv>)->iv_X` (via
20294+ # `self_arrow`) or as the first arg of a same-class
20295+ # method call (via `self_expr`).
20296+ recv_c = compile_expr_gc_rooted(recv_for_default)
20297+ @self_override = "(" + recv_c + ")"
20298+ @current_class_idx = target_ci
20299+ end
2025320300 result = result + compile_expr(def_id)
20301+ @current_class_idx = saved_ci
20302+ @self_override = saved_override
2025420303 else
2025520304 result = result + "0"
2025620305 end