Commit 612af6c4

612af6c4b6f2Merged

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

Description

feat(iexec): spread a sole splat arg across direct instance_exec block params

recv.instance_exec(*arr) { |a, b| } on a direct (explicit-receiver) lift rejected the *arr with "unsupported expression: SplatNode". CRuby spreads a single splat across the block's params exactly as passing the array directly does (the auto-splat path).

Unwrap a sole SplatNode arg to its array operand in both the analyze-side block-param typing and the codegen splice, then reuse the existing auto-splat logic (bind each param to an element, typed from the array's element type). The directly-passed-array form (instance_exec([3, 4]) { |a, b| }) is unchanged.

A splat also spreads across a single block param: instance_exec(*arr) { |a| } binds a to arr[0] (the first spread positional arg), whereas a directly passed array binds the whole array to a lone param. Gate the auto-splat path on rnp >= 1 (analyze) / npar >= 1 (codegen) when the arg is explicitly splatted, so the lone-param splat is spread rather than emitted as a raw SplatNode (which would crash codegen).

Adds test/instance_exec_direct_splat.rb (int-array splat across two params, a directly-passed array regression anchor, a string-array splat, and the single-param splat for both int and string arrays).

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

Details

Commit
612af6c4b6f2
Parent
884b6f664d8e

src/analyze_pass.cmodifiedView file

@@ -2045,9 +2045,16 @@
20452045 trampoline param read), not the caller's args. */
20462046 int tramp_argc = strcmp(cname, "instance_exec") ? ie_tramp_effective_argc(c, id) : -1;
20472047 /* auto-splat: a single array arg destructured across N>=2 params binds
2048- each to the element type. */
2049- if (tramp_argc < 0 && iac == 1 && rnp >= 2) {
2050- TyKind a0 = infer_type(c, iav[0]);
2048+ each to the element type. A sole splat (`instance_exec(*arr) { |a, b| }`)
2049+ spreads the same way -- unwrap it to its array operand. A splat also
2050+ spreads across a single param (`instance_exec(*arr) { |a| }` binds `a`
2051+ to `arr[0]`), unlike a directly-passed array (which binds the whole array
2052+ to a lone param), so allow `rnp >= 1` when explicitly splatted. */
2053+ int arg0 = (iac == 1 && iav) ? iav[0] : -1;
2054+ int is_splat = arg0 >= 0 && nt_type(nt, arg0) && !strcmp(nt_type(nt, arg0), "SplatNode");
2055+ if (is_splat) arg0 = nt_ref(nt, arg0, "expression");
2056+ if (tramp_argc < 0 && iac == 1 && (rnp >= 2 || (rnp >= 1 && is_splat)) && arg0 >= 0) {
2057+ TyKind a0 = infer_type(c, arg0);
20512058 if (ty_is_array(a0)) {
20522059 TyKind et = ty_array_elem(a0);
20532060 for (int k = 0; k < rnp; k++) {