Commit e7167568

e71675689df9Merged

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

Description

fix(codegen): walk @nd_parts in scan_new_calls

A method called only inside string interpolation never widens its parameter types: def cap(s); s + "_cap"; end; puts "lv_#{cap(name)}" declares cap's s as the default mrb_int and the call site passes a const char *, yielding a C int-conversion error. The cause is that scan_new_calls' AST recursion enumerates @nd_body / @nd_stmts / @nd_receiver / @nd_arguments / @nd_args / @nd_expression / @nd_predicate / @nd_subsequent / @nd_else_clause / @nd_left / @nd_right / @nd_block / @nd_elements / @nd_conditions but not @nd_parts, so the EmbeddedStatementsNode body inside an InterpolatedStringNode is never visited and the call inside #{...} is invisible to the param-widener.

Fix is one extra recursion arm at the tail of scan_new_calls: parse @nd_parts and recurse into each component. EmbeddedStatementsNode already carries its body via @nd_stmts, so once the parts walk reaches the embedded node the existing @nd_stmts arm picks up the inner call and routes it through the same param-type unification path that other call sites use. scan_features_children was already correct here — its push_child_ids helper enumerates @nd_parts — so the bug was localized to scan_new_calls.

Test exercises four shapes inside puts "...#{...}...":

  • one-arg method whose param widens to string
  • two-arg method whose two params both widen to string
  • one-arg method whose param widens to int (regression-guard for the case where widening should preserve int against a literal int arg)
  • one-arg method whose param widens to bool

CRuby and Spinel both produce the same four output lines.

Bootstrap: gen2.c == gen3.c. Tests: 202 pass.

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

Details

Commit
e71675689df9
Parent
02005f26deba

spinel_codegen.rbmodifiedView file

@@ -6435,6 +6435,16 @@
64356435 scan_new_calls(conds[k])
64366436 k = k + 1
64376437 end
6438+ # InterpolatedStringNode and friends carry their components in @nd_parts.
6439+ # Without this, an EmbeddedStatementsNode inside `"#{...}"` is the only
6440+ # call site for a method whose param type would otherwise widen, and
6441+ # the param keeps its default `int` => C error at the call site.
6442+ parts = parse_id_list(@nd_parts[nid])
6443+ k = 0
6444+ while k < parts.length
6445+ scan_new_calls(parts[k])
6446+ k = k + 1
6447+ end
64386448 end
64396449
64406450 def update_ivar_types_from_params