Commit d99fb490
d99fb4909e70Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
Add ForwardingParameterNode/ForwardingArgumentsNode to parse output
Support def foo(...) and bar(...) forwarding syntax in the parser
and legacy Ruby pipeline. Lowers to synthetic *__sp_fwd_args,
**__sp_fwd_kw, &__sp_fwd_block slots. Refs #1288.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- d99fb4909e70
- Parent
- db4cb88035fe
legacy/spinel_analyze.rbmodifiedView file
@@ -12669,7 +12669,17 @@
1266912669
# carry a slot.1267012670
kwrest = @nd_keyword_rest[params]1267112671
if kwrest >= 012672-
if @nd_type[kwrest] == "KeywordRestParameterNode"12672+
if @nd_type[kwrest] == "ForwardingParameterNode"12673+
# `def foo(...)` lowers to three synthetic slots that capture each12674+
# forwarded channel: *__sp_fwd_args, **__sp_fwd_kw, &__sp_fwd_block12675+
# (issue #1288). Prism stores the forwarding param in keyword_rest.12676+
forwarding_synthetic_names.each { |nm|12677+
if result != ""12678+
result = result + ","12679+
end12680+
result = result + nm12681+
}12682+
elsif @nd_type[kwrest] == "KeywordRestParameterNode"1267312683
if result != ""1267412684
result = result + ","1267512685
end@@ -12702,6 +12712,25 @@
1270212712
result1270312713
end1270412714
12715+
# The synthetic parameter names a `...` forwarding parameter lowers to,12716+
# in slot order: positional rest, keyword rest, block. Issue #1288.12717+
def forwarding_synthetic_names12718+
["__sp_fwd_args", "__sp_fwd_kw", "__sp_fwd_block"]12719+
end12720+
12721+
# True when a method's ParametersNode carries a `...` forwarding param.12722+
def params_have_forwarding(nid)12723+
params = @nd_parameters[nid]12724+
if params < 012725+
return 012726+
end12727+
kwr = @nd_keyword_rest[params]12728+
if kwr >= 0 && @nd_type[kwr] == "ForwardingParameterNode"12729+
return 112730+
end12731+
012732+
end12733+
1270512734
def collect_rest_index(nid)1270612735
params = @nd_parameters[nid]1270712736
if params < 0@@ -12709,6 +12738,13 @@
1270912738
end1271012739
rest = @nd_rest[params]1271112740
if rest < 0 || @nd_type[rest] != "RestParameterNode"12741+
# A `...` forwarding param contributes a synthetic *__sp_fwd_args12742+
# rest slot right after reqs/opts/kws. Issue #1288.12743+
if params_have_forwarding(nid) == 112744+
return parse_id_list(@nd_requireds[params]).length +12745+
parse_id_list(@nd_optionals[params]).length +12746+
parse_id_list(@nd_keywords[params]).length12747+
end1271212748
return -11271312749
end1271412750
idx = 0@@ -12728,6 +12764,13 @@
1272812764
end1272912765
kwrest = @nd_keyword_rest[params]1273012766
if kwrest < 0 || @nd_type[kwrest] != "KeywordRestParameterNode"12767+
# `...` forwarding: **__sp_fwd_kw sits one slot after the synthetic12768+
# *__sp_fwd_args rest. Issue #1288.12769+
if params_have_forwarding(nid) == 112770+
return parse_id_list(@nd_requireds[params]).length +12771+
parse_id_list(@nd_optionals[params]).length +12772+
parse_id_list(@nd_keywords[params]).length + 112773+
end1273112774
return -11273212775
end1273312776
idx = 0@@ -12813,7 +12856,16 @@
1281312856
# `f(a: 1)` keyword hash construction), so the slot is sym_poly_hash.1281412857
kwrest = @nd_keyword_rest[params]1281512858
if kwrest >= 012816-
if @nd_type[kwrest] == "KeywordRestParameterNode"12859+
if @nd_type[kwrest] == "ForwardingParameterNode"12860+
# `...` slots: positional rest (int_array), keyword rest12861+
# (sym_poly_hash), block (proc). Issue #1288.12862+
["int_array", "sym_poly_hash", "proc"].each { |tp|12863+
if result != ""12864+
result = result + ","12865+
end12866+
result = result + tp12867+
}12868+
elsif @nd_type[kwrest] == "KeywordRestParameterNode"1281712869
if result != ""1281812870
result = result + ","1281912871
end@@ -29982,6 +30034,29 @@
2998230034
if mn == "proc" || mn == "lambda"2998330035
collect_proc_param_sym_names(local, @nd_block[i], mn == "lambda" ? 1 : 0)2998430036
end30037+
# `inner(...)` forwarding: the codegen lowering looks each callee30038+
# param up by name in **__sp_fwd_kw, so register those param names30039+
# as symbols (static SPS_ ids, not a dynamic intern). Issue #1288.30040+
a_fwd_sn = @nd_arguments[i]30041+
if a_fwd_sn >= 030042+
a_fwd_list = get_args(a_fwd_sn)30043+
has_fwd_sn = 030044+
a_fwd_list.each { |aid|30045+
if @nd_type[aid] == "ForwardingArgumentsNode"30046+
has_fwd_sn = 130047+
end30048+
}30049+
if has_fwd_sn == 130050+
fmi_sn = find_method_idx(mn)30051+
if fmi_sn >= 0 && fmi_sn < @meth_param_names.length30052+
@meth_param_names[fmi_sn].split(",", -1).each { |pn_sn|30053+
if pn_sn != ""30054+
collect_sym_name_into(local, pn_sn)30055+
end30056+
}30057+
end30058+
end30059+
end2998530060
if mn == "new"2998630061
r_proc_sym = @nd_receiver[i]2998730062
if r_proc_sym >= 0 && (@nd_type[r_proc_sym] == "ConstantReadNode" || @nd_type[r_proc_sym] == "ConstantPathNode")legacy/spinel_codegen.rbmodifiedView file
Loading diff…
spinel_parse.cmodifiedView file
Loading diff…