Commit bd17fb16

bd17fb16727bMerged

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

Description

parse,codegen: `**h` double-splat spreads a sym-keyed hash into kwargs

`foo(**h)` previously parsed as a no-arg call (PM_ASSOC_SPLAT_NODE wasn't surfaced), so every callee kwarg fell through to the missing-arg warning and emitted 0.

  • spinel_parse.c: emit AssocSplatNode for PM_ASSOC_SPLAT_NODE, wrapping the inner hash expression.
  • spinel_codegen.rb: detect the single-arg splat shape (`foo(**h)`) in both compile_call_args_with_defaults and compile_typed_call_args. For each callee param name, emit `splat_hash[:p_name]` via the matching sym-keyed hash get. sym_int_hash / sym_str_hash / sym_poly_hash variants wired; non-sym-keyed splats fall through unhandled.

Mixed positional + splat and multiple splats are not wired here (would need more invasive call-args plumbing).

Fixes #917 (single-arg splat; mixed shapes deferred)

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

Details

Commit
bd17fb16727b
Parent
39547bb511c8

spinel_codegen.rbmodifiedView file

@@ -29402,6 +29402,35 @@
2940229402 rest_param_idx = -1
2940329403 end
2940429404
29405+ # Splat-only call: `foo(**h)` with no other args. Emit
29406+ # `splat[:p1], splat[:p2], ...` for each callee param. Only
29407+ # sym-keyed splat hashes; issue #917.
29408+ if arg_ids.length == 1 && @nd_type[arg_ids[0]] == "KeywordHashNode"
29409+ sole_ke = parse_id_list(@nd_elements[arg_ids[0]])
29410+ if sole_ke.length == 1 && @nd_type[sole_ke[0]] == "AssocSplatNode"
29411+ sh_id = @nd_expression[sole_ke[0]]
29412+ if sh_id >= 0
29413+ sh_t = infer_type(sh_id)
29414+ if sh_t == "sym_int_hash" || sh_t == "sym_str_hash" || sh_t == "sym_poly_hash"
29415+ sh_pfx = "SymIntHash"
29416+ sh_pfx = "SymStrHash" if sh_t == "sym_str_hash"
29417+ sh_pfx = "SymPolyHash" if sh_t == "sym_poly_hash"
29418+ sh_rc = compile_expr(sh_id)
29419+ sb_w = ""
29420+ j_w = 0
29421+ while j_w < pnames.length
29422+ if j_w > 0
29423+ sb_w = sb_w + ", "
29424+ end
29425+ sb_w = sb_w + "sp_" + sh_pfx + "_get(" + sh_rc + ", " + compile_symbol_literal(pnames[j_w]) + ")"
29426+ j_w = j_w + 1
29427+ end
29428+ return sb_w
29429+ end
29430+ end
29431+ end
29432+ end
29433+
2940529434 # Check if args contain a KeywordHashNode - extract kw pairs.
2940629435 # Track kw_arg_ids parallel to kw_vals so the poly-boxing branch
2940729436 # below () can pick the right box helper from the arg's
@@ -30274,6 +30303,36 @@
3027430303 arg_ids = []
3027530304 kw_name_to_arg = "".split(",", -1)
3027630305 kw_name_keys = "".split(",", -1)
30306+ # AssocSplatNode `**h` inside the KeywordHashNode: emit
30307+ # `splat_hash[:p_name]` directly for each callee param. Only the
30308+ # single-arg shape (`foo(**h)` with no other args) and sym-keyed
30309+ # splats are supported here. Issue #917.
30310+ if raw_arg_ids.length == 1 && @nd_type[raw_arg_ids[0]] == "KeywordHashNode"
30311+ sole_kelems = parse_id_list(@nd_elements[raw_arg_ids[0]])
30312+ if sole_kelems.length == 1 && @nd_type[sole_kelems[0]] == "AssocSplatNode"
30313+ splat_h = @nd_expression[sole_kelems[0]]
30314+ if splat_h >= 0
30315+ splat_t = infer_type(splat_h)
30316+ if splat_t == "sym_int_hash" || splat_t == "sym_str_hash" || splat_t == "sym_poly_hash"
30317+ splat_pfx = "SymIntHash"
30318+ splat_pfx = "SymStrHash" if splat_t == "sym_str_hash"
30319+ splat_pfx = "SymPolyHash" if splat_t == "sym_poly_hash"
30320+ splat_rc = compile_expr(splat_h)
30321+ sb = ""
30322+ ki_sp = 0
30323+ while ki_sp < pnames_t.length
30324+ if ki_sp > 0
30325+ sb = sb + ", "
30326+ end
30327+ sname_sp = pnames_t[ki_sp]
30328+ sb = sb + "sp_" + splat_pfx + "_get(" + splat_rc + ", " + compile_symbol_literal(sname_sp) + ")"
30329+ ki_sp = ki_sp + 1
30330+ end
30331+ return sb
30332+ end
30333+ end
30334+ end
30335+ end
3027730336 kak = 0
3027830337 while kak < raw_arg_ids.length
3027930338 if @nd_type[raw_arg_ids[kak]] == "KeywordHashNode"