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
arg_ids = get_args(args_id) ptypes = @meth_param_types[mi].split(",") rest_param_idx = method_rest_index(mi) # Splat slot widening: a `def f(*args)` slot defaults to int_array # (the splat default in collect_ptypes_str). Every call site packs # its remaining positional args into that slot; when sites disagree # on element type the slot must widen to poly_array, otherwise the # str-typed args from a later call get reinterpreted as int and # round-trip as garbage pointers. Issue #666. if rest_param_idx >= 0 && rest_param_idx < ptypes.length elem_arg_t_splat = "" ak_splat = rest_param_idx while ak_splat < arg_ids.length if @nd_type[arg_ids[ak_splat]] != "SplatNode" && @nd_type[arg_ids[ak_splat]] != "KeywordHashNode" at_splat = infer_type_deep(arg_ids[ak_splat]) if at_splat != "" && elem_arg_t_splat == "" elem_arg_t_splat = at_splat elsif at_splat != "" && elem_arg_t_splat != "" && elem_arg_t_splat != at_splat elem_arg_t_splat = "poly" end end ak_splat = ak_splat + 1 end if elem_arg_t_splat != "" cur_rest_t = ptypes[rest_param_idx] cur_elem = elem_type_of_array(cur_rest_t) new_rest_t = "int_array" if elem_arg_t_splat == "string" new_rest_t = "str_array" elsif elem_arg_t_splat == "float" new_rest_t = "float_array" elsif elem_arg_t_splat == "symbol" new_rest_t = "sym_array" elsif elem_arg_t_splat == "poly" new_rest_t = "poly_array" end if cur_rest_t == "int_array" && cur_elem == "int" && elem_arg_t_splat != "int" # First non-int call site: widen the default int_array to the # matching homogeneous variant. ptypes[rest_param_idx] = new_rest_t elsif cur_elem != "" && cur_elem != elem_arg_t_splat && cur_rest_t != "poly_array" # Disagreement with the slot's existing element type → poly. ptypes[rest_param_idx] = "poly_array" @needs_rb_value = 1 end end end k = 0 while k < arg_ids.length at = infer_type_deep(arg_ids[k])spinel_codegen.rbmodifiedView file
Loading diff…
test/splat_heterogeneous_call.rbaddedView file
Loading diff…