Commit 5f840526

5f84052665c6Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

Support splat parameters (def f(*a)) on instance methods

def f(*a) on an instance method compiled to a fixed-arity function, so a call passing more args than declared params failed with "too many arguments to function". collect_ptypes_str already seeds the rest slot as int_array, but the call-site widening collapsed it to scalar poly and compile_typed_call_args spilled each arg into its own slot.

Track the splat position in a sparse, name-keyed map (@cls_rest_keys/@cls_rest_idxs, keyed by "#") so it adds no alignment surface to the per-method arrays; it's IR-serialized so codegen sees it. Then:

  • analyze: widen_ptypes_from_args gained a rest_idx parameter; at the rest slot it widens the element type across the trailing args and keeps the slot an array (int_array for ints, poly_array for objects / mixed) instead of unifying scalar-vs-array down to poly.
  • codegen: compile_typed_call_args packs the args from the rest position into the rest slot's array (a fresh, GC-rooted int_array or poly_array) and passes it as one argument. Gated to the last-param splat with no kwargs / no block-forward omission.

Covers def f(*a) and def f(x, *a), element access (a[0], a.each, a.size), and call-site splat (f(*xs)), for int and heap-object args. Value-type-object splat args remain unsupported (same value-type-collection gap as #1056).

Fixes #1037

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

Details

Commit
5f84052665c6
Parent
8b834b6364b4

spinel_analyze.rbmodifiedView file

@@ -266,6 +266,13 @@
266266 @cls_meth_returns = "".split(",", -1)
267267 @cls_meth_bodies = "".split(",", -1)
268268 @cls_meth_defaults = "".split(",", -1)
269+ # Splat-param ("def f(*a)") rest-slot tracking for instance methods.
270+ # Sparse, name-keyed (NOT position-aligned with the per-method
271+ # arrays) so it adds no desync surface: @cls_rest_keys holds
272+ # "<cname>#<mname>" and @cls_rest_idxs the rest param's position.
273+ # Only methods with a splat get an entry; lookup returns -1 otherwise.
274+ @cls_rest_keys = "".split(",", -1)
275+ @cls_rest_idxs = []
269276 # Mirror of @meth_param_empty for class methods. Pipe-separated by
270277 # method, comma-separated by param. .
271278 @cls_meth_ptypes_empty = "".split(",", -1)
@@ -1964,6 +1971,36 @@
19641971 ""
19651972 end
19661973
1974+ # Record / look up the splat ("*a") rest-param position for an
1975+ # instance method, keyed by "<cname>#<mname>". Last write wins
1976+ # (mirrors append_cls_meth's last-def-wins). Returns -1 when the
1977+ # method has no splat param.
1978+ def record_cls_meth_rest(cname, mname, idx)
1979+ key = cname + "#" + mname
1980+ k = 0
1981+ while k < @cls_rest_keys.length
1982+ if @cls_rest_keys[k] == key
1983+ @cls_rest_idxs[k] = idx
1984+ return
1985+ end
1986+ k = k + 1
1987+ end
1988+ @cls_rest_keys.push(key)
1989+ @cls_rest_idxs.push(idx)
1990+ end
1991+
1992+ def cls_meth_rest_idx(cname, mname)
1993+ key = cname + "#" + mname
1994+ k = 0
1995+ while k < @cls_rest_keys.length
1996+ if @cls_rest_keys[k] == key
1997+ return @cls_rest_idxs[k]
1998+ end
1999+ k = k + 1
2000+ end
2001+ -1
2002+ end
2003+
19672004 def cls_method_return(ci, mname)
19682005 ck = ci.to_s + ":" + mname
19692006 if @cls_meth_return_cache.key?(ck)
@@ -9976,6 +10013,12 @@
997610013 defaults_str = collect_defaults_str(nid)
997710014 has_y = body_has_yield(body_id)
997810015 append_cls_meth(ci, mname, params_str, ptypes_str, "int", body_id, defaults_str)
10016+ # Record a splat param's position so call-site arg packing and the
10017+ # widen-protection can find it by (class, method) name.
10018+ rest_i_cm = collect_rest_index(nid)
10019+ if rest_i_cm >= 0
10020+ record_cls_meth_rest(@cls_names[ci], mname, rest_i_cm)
10021+ end
997910022 # Track yield info
998010023 if @cls_meth_has_yield[ci] != ""
998110024 @cls_meth_has_yield[ci] = @cls_meth_has_yield[ci] + ";" + has_y.to_s
@@ -13706,7 +13749,46 @@
1370613749 # unify a `KeywordHashNode` into `ptypes[0]` (the AST presents
1370713750 # kwargs as a single trailing hash arg), leaving the callee's
1370813751 # actual kwarg slots un-widened.
13709- def widen_ptypes_from_args(arg_ids, pnames, ptypes)
13752+ # Element type of a typed array, for splat rest-slot widening.
13753+ def rest_slot_elem_type(t)
13754+ if t == "int_array"
13755+ return "int"
13756+ end
13757+ if t == "str_array"
13758+ return "string"
13759+ end
13760+ if t == "float_array"
13761+ return "float"
13762+ end
13763+ if t == "sym_array"
13764+ return "symbol"
13765+ end
13766+ if t == "poly_array"
13767+ return "poly"
13768+ end
13769+ "int"
13770+ end
13771+
13772+ # Array type that collects elements of type `elem` (for a splat
13773+ # rest slot). Heterogeneous / object elements -> poly_array.
13774+ def rest_slot_array_type(elem)
13775+ if elem == "int"
13776+ return "int_array"
13777+ end
13778+ if elem == "string" || elem == "mutable_str"
13779+ return "str_array"
13780+ end
13781+ if elem == "float"
13782+ return "float_array"
13783+ end
13784+ if elem == "symbol"
13785+ return "sym_array"
13786+ end
13787+ @needs_rb_value = 1
13788+ "poly_array"
13789+ end
13790+
13791+ def widen_ptypes_from_args(arg_ids, pnames, ptypes, rest_idx = -1)
1371013792 pos_idx = 0
1371113793 ai = 0
1371213794 while ai < arg_ids.length
@@ -13767,6 +13849,28 @@
1376713849 end
1376813850 pos_idx = pos_idx + 1
1376913851 end
13852+ elsif rest_idx >= 0 && pos_idx == rest_idx
13853+ # Splat rest slot ("def f(*a)"): every remaining positional arg
13854+ # feeds it. Widen the element type across them and keep the slot
13855+ # an array (int_array seed -> int_array for int args, poly_array
13856+ # for objects / mixed) rather than collapsing it to scalar poly,
13857+ # which is what the per-arg unify below would do.
13858+ elem_rest = "int"
13859+ if pos_idx < ptypes.length
13860+ elem_rest = rest_slot_elem_type(ptypes[pos_idx])
13861+ end
13862+ rj = ai
13863+ while rj < arg_ids.length
13864+ ar = arg_ids[rj]
13865+ if @nd_type[ar] != "KeywordHashNode"
13866+ elem_rest = unify_call_types(elem_rest, infer_type(ar), ar)
13867+ end
13868+ rj = rj + 1
13869+ end
13870+ if pos_idx < ptypes.length
13871+ ptypes[pos_idx] = rest_slot_array_type(elem_rest)
13872+ end
13873+ ai = arg_ids.length
1377013874 else
1377113875 if pos_idx < ptypes.length
1377213876 at = infer_type(aid)
@@ -14101,7 +14205,8 @@
1410114205 ptypes = cls_meth_ptypes_get(owner_ci, midx)
1410214206 if ptypes.length > 0
1410314207 pnames = cls_meth_pnames_get(owner_ci, midx)
14104- widen_ptypes_from_args(arg_ids, pnames, ptypes)
14208+ rest_i_w = cls_meth_rest_idx(@cls_names[owner_ci], mname)
14209+ widen_ptypes_from_args(arg_ids, pnames, ptypes, rest_i_w)
1410514210 cls_meth_ptypes_put(owner_ci, midx, ptypes)
1410614211 end
1410714212 end
@@ -30573,6 +30678,8 @@
3057330678 buf = ir_emit_sa(buf, "@cls_meth_ptypes", @cls_meth_ptypes)
3057430679 buf = ir_emit_sa(buf, "@cls_meth_returns", @cls_meth_returns)
3057530680 buf = ir_emit_sa(buf, "@cls_meth_bodies", @cls_meth_bodies)
30681+ buf = ir_emit_sa(buf, "@cls_rest_keys", @cls_rest_keys)
30682+ buf = ir_emit_ia(buf, "@cls_rest_idxs", @cls_rest_idxs)
3057630683 buf = ir_emit_sa(buf, "@cls_meth_defaults", @cls_meth_defaults)
3057730684 buf = ir_emit_sa(buf, "@cls_meth_ptypes_empty", @cls_meth_ptypes_empty)
3057830685 buf = ir_emit_sa(buf, "@cls_meth_prep_chain", @cls_meth_prep_chain)