Commit a0c12023
a0c12023d43eMerged
Ryan Seys <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
analyze: forward callable values into poly-element collections
Details
- Commit
- a0c12023d43e
- Parent
- e3360c876d88
src/analyze_pass.cmodifiedView file
@@ -2060,11 +2060,13 @@
20602060
if (!name) continue;20612061
int encl = c->nscope[id];20622062
TyKind rt = infer_type(c, recv);2063-
/* Hash `each`/`each_pair` yields the [k,v] pair (one arg to a 1-param proc,2064-
destructured to a strict 2-param lambda) -- the pair/destructure semantics2065-
don't reduce to the simple per-element call this desugar emits, so leave2066-
hash receivers to their own path. */2067-
if (ty_is_hash(rt)) continue;2063+
/* Hash `each`/`each_pair` yields the [k,v] pair, forwarded as a single array2064+
argument `c.call([k, v])` (built below). This is reliable for a method2065+
object, whose array-ABI trampoline takes the pair directly; a proc/lambda2066+
*value* would need its param's pair-array type inferred from the synthetic2067+
call (proc-param array-input inference, not yet supported), so those hash2068+
cases are left to their own path rather than forwarded incorrectly. */2069+
if (ty_is_hash(rt) && ct != TY_METHOD) continue;20682070
TyKind pty[4];20692071
int arity = ty_block_yield(rt, name, pty, 4);20702072
if (arity < 1) continue; /* not a context-free iterator (or recv unresolved) */@@ -2091,7 +2093,14 @@
20912093
nt_node_set_ref(nt, bparams, "parameters", params);20922094
20932095
int callargs = nt_new_node(nt, "ArgumentsNode");2094-
nt_node_set_arr(nt, callargs, "arguments", reads, arity);2096+
/* hash each/each_pair (2 yielded params) forwards the [k, v] pair as one2097+
array argument; element iterators forward their values positionally. */2098+
if (ty_is_hash(rt) && arity == 2) {2099+
int pairarr = nt_new_node(nt, "ArrayNode");2100+
nt_node_set_arr(nt, pairarr, "elements", reads, 2);2101+
nt_node_set_arr(nt, callargs, "arguments", &pairarr, 1);2102+
}2103+
else nt_node_set_arr(nt, callargs, "arguments", reads, arity);20952104
int callnode = nt_new_node(nt, "CallNode");20962105
nt_node_set_ref(nt, callnode, "receiver", proc_clone);20972106
nt_node_set_str(nt, callnode, "name", "call");test/block_forward_poly.rbaddedView file
Loading diff…
test/block_forward_poly.rb.expectedaddedView file
Loading diff…