Commit d8af8d22

d8af8d2287d6Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified

Description

fix(analyze,codegen): heterogeneous splat widens to poly_array (#666)

def test_splat(*args); test_splat(1,2,3); test_splat("a","b") lost the second call's string args: spinel's splat slot defaulted to int_array and the str args got (mrb_int)<char*> casts that round-tripped as garbage pointers through puts x.to_s.

analyze (detect_poly_in_node CallNode arm):

  • Before walking individual positional args, scan the args that land in the rest slot, aggregate their element type, and unify against the rest slot's existing array type. First non-int call site widens int_array → matching homogeneous variant (str_array / float_array / sym_array); a later disagreement promotes the slot to poly_array.

codegen (compile_typed_call_args):

  • New poly_array branch alongside the existing int_array rest path. Each positional arg is boxed via box_expr_to_poly into a fresh sp_PolyArray; SplatNode sources iterate the source array (int_array / str_array / poly_array) and box each element so the bundle reaches the callee under its sp_PolyArray * signature.
  • The single-splat fast path (compile_call_args_splat) hardcodes int_array, so detect a poly_array rest target before that branch and route through the main path's poly builder instead.

Verified the issue repro now prints 1 / 2 / 3 / a / b and the existing splat_call test (mixed-type single-splat shapes against a count(*xs) whose slot widens to poly_array) still matches expected. optcarrot checksum 59662 unchanged.

Closes #666.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
d8af8d2287d6
Parent
b245e10baf54

spinel_analyze.rbmodifiedView file

@@ -19524,6 +19524,50 @@
1952419524 arg_ids = get_args(args_id)
1952519525 ptypes = @meth_param_types[mi].split(",")
1952619526 rest_param_idx = method_rest_index(mi)
19527+ # Splat slot widening: a `def f(*args)` slot defaults to int_array
19528+ # (the splat default in collect_ptypes_str). Every call site packs
19529+ # its remaining positional args into that slot; when sites disagree
19530+ # on element type the slot must widen to poly_array, otherwise the
19531+ # str-typed args from a later call get reinterpreted as int and
19532+ # round-trip as garbage pointers. Issue #666.
19533+ if rest_param_idx >= 0 && rest_param_idx < ptypes.length
19534+ elem_arg_t_splat = ""
19535+ ak_splat = rest_param_idx
19536+ while ak_splat < arg_ids.length
19537+ if @nd_type[arg_ids[ak_splat]] != "SplatNode" && @nd_type[arg_ids[ak_splat]] != "KeywordHashNode"
19538+ at_splat = infer_type_deep(arg_ids[ak_splat])
19539+ if at_splat != "" && elem_arg_t_splat == ""
19540+ elem_arg_t_splat = at_splat
19541+ elsif at_splat != "" && elem_arg_t_splat != "" && elem_arg_t_splat != at_splat
19542+ elem_arg_t_splat = "poly"
19543+ end
19544+ end
19545+ ak_splat = ak_splat + 1
19546+ end
19547+ if elem_arg_t_splat != ""
19548+ cur_rest_t = ptypes[rest_param_idx]
19549+ cur_elem = elem_type_of_array(cur_rest_t)
19550+ new_rest_t = "int_array"
19551+ if elem_arg_t_splat == "string"
19552+ new_rest_t = "str_array"
19553+ elsif elem_arg_t_splat == "float"
19554+ new_rest_t = "float_array"
19555+ elsif elem_arg_t_splat == "symbol"
19556+ new_rest_t = "sym_array"
19557+ elsif elem_arg_t_splat == "poly"
19558+ new_rest_t = "poly_array"
19559+ end
19560+ if cur_rest_t == "int_array" && cur_elem == "int" && elem_arg_t_splat != "int"
19561+ # First non-int call site: widen the default int_array to the
19562+ # matching homogeneous variant.
19563+ ptypes[rest_param_idx] = new_rest_t
19564+ elsif cur_elem != "" && cur_elem != elem_arg_t_splat && cur_rest_t != "poly_array"
19565+ # Disagreement with the slot's existing element type → poly.
19566+ ptypes[rest_param_idx] = "poly_array"
19567+ @needs_rb_value = 1
19568+ end
19569+ end
19570+ end
1952719571 k = 0
1952819572 while k < arg_ids.length
1952919573 at = infer_type_deep(arg_ids[k])