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 = -12940329403
end2940429404
29405+
# Splat-only call: `foo(**h)` with no other args. Emit29406+
# `splat[:p1], splat[:p2], ...` for each callee param. Only29407+
# 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 >= 029413+
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 = 029421+
while j_w < pnames.length29422+
if j_w > 029423+
sb_w = sb_w + ", "29424+
end29425+
sb_w = sb_w + "sp_" + sh_pfx + "_get(" + sh_rc + ", " + compile_symbol_literal(pnames[j_w]) + ")"29426+
j_w = j_w + 129427+
end29428+
return sb_w29429+
end29430+
end29431+
end29432+
end29433+
2940529434
# Check if args contain a KeywordHashNode - extract kw pairs.2940629435
# Track kw_arg_ids parallel to kw_vals so the poly-boxing branch2940729436
# 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: emit30307+
# `splat_hash[:p_name]` directly for each callee param. Only the30308+
# single-arg shape (`foo(**h)` with no other args) and sym-keyed30309+
# 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 >= 030315+
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 = 030323+
while ki_sp < pnames_t.length30324+
if ki_sp > 030325+
sb = sb + ", "30326+
end30327+
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 + 130330+
end30331+
return sb30332+
end30333+
end30334+
end30335+
end3027730336
kak = 03027830337
while kak < raw_arg_ids.length3027930338
if @nd_type[raw_arg_ids[kak]] == "KeywordHashNode"spinel_parse.cmodifiedView file
Loading diff…
test/kwargs_double_splat.rbaddedView file
Loading diff…
test/kwargs_double_splat.rb.expectedaddedView file
Loading diff…