Commit 4431c202

4431c2023866Merged

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

Description

analyze: infer proc/lambda params from their .call argument types

Details

Commit
4431c2023866
Parent
a0c12023d43e

src/analyze_pass.cmodifiedView file

@@ -1983,6 +1983,35 @@
19831983 forward whose expression names the enclosing method's block param) is left to
19841984 the inline-forward path. Runs in the inference fixpoint; once a call is
19851985 rewritten its block is a BlockNode, so it is never revisited. */
1986+/* Required-param count of a forwarded callable expression `ex`, or -1 if it
1987+ cannot be determined statically. Chooses the hash-pair calling convention: a
1988+ 1-param callable receives the [k,v] pair as one array, a 2-param one is called
1989+ positionally (matching CRuby's proc auto-splat of the yielded pair). */
1990+static int fwd_callable_arity(Compiler *c, int ex) {
1991+ NodeTable *nt = (NodeTable *)c->nt;
1992+ const char *exty = nt_type(nt, ex);
1993+ if (!exty) return -1;
1994+ int create = -1;
1995+ if (!strcmp(exty, "LambdaNode") || is_proc_create(c, ex)) create = ex;
1996+ else if (!strcmp(exty, "LocalVariableReadNode")) {
1997+ const char *vn = nt_str(nt, ex, "name");
1998+ Scope *sc = vn ? comp_scope_of(c, ex) : NULL;
1999+ for (int w = 0; vn && w < nt->count; w++) {
2000+ const char *wty = nt_type(nt, w);
2001+ if (!wty || strcmp(wty, "LocalVariableWriteNode")) continue;
2002+ const char *wn = nt_str(nt, w, "name");
2003+ if (!wn || strcmp(wn, vn) || comp_scope_of(c, w) != sc) continue;
2004+ int val = nt_ref(nt, w, "value");
2005+ if (val >= 0 && is_proc_create(c, val)) { create = val; break; }
2006+ }
2007+ }
2008+ if (create < 0) return -1;
2009+ int pn = a_proc_params_node(c, create);
2010+ if (pn < 0) return -1;
2011+ int rn = 0; nt_arr(nt, pn, "requireds", &rn);
2012+ return rn;
2013+}
2014+
19862015 /* `send(:m, args)` / `__send__("m", args)` / `public_send(:m, args)` with NO
19872016 explicit receiver -> a direct implicit-self call to `m` with the remaining
19882017 args. The literal symbol/string name resolves statically, the same model as
@@ -2030,7 +2059,6 @@
20302059 }
20312060 return changed;
20322061 }
2033-
20342062 int desugar_value_callable_forwards(Compiler *c) {
20352063 NodeTable *nt = (NodeTable *)c->nt;
20362064 int changed = 0;
@@ -2061,16 +2089,32 @@
20612089 int encl = c->nscope[id];
20622090 TyKind rt = infer_type(c, recv);
20632091 /* Hash `each`/`each_pair` yields the [k,v] pair, forwarded as a single array
2064- argument `c.call([k, v])` (built below). This is reliable for a method
2065- object, whose array-ABI trampoline takes the pair directly; a proc/lambda
2066- *value* would need its param's pair-array type inferred from the synthetic
2067- call (proc-param array-input inference, not yet supported), so those hash
2068- cases are left to their own path rather than forwarded incorrectly. */
2069- if (ty_is_hash(rt) && ct != TY_METHOD) continue;
2092+ argument `c.call([k, v])` (built below) -- correct for every arity: a
2093+ 1-param callable gets the pair, a 2-param proc auto-splats it, a 2-param
2094+ lambda raises exactly as CRuby's `Hash#each(&lambda)` does. A Method object
2095+ takes the pair via its array ABI; a proc/lambda value's param is typed as
2096+ the pair array by the call-site argument inference (a container arg
2097+ overrides the bare-int default). */
20702098 TyKind pty[4];
20712099 int arity = ty_block_yield(rt, name, pty, 4);
20722100 if (arity < 1) continue; /* not a context-free iterator (or recv unresolved) */
20732101
2102+ /* Hash forwarding. A Method object takes any hash iterator's yield directly
2103+ through its array ABI (the [k,v] pair as one array for `each`, the bare
2104+ key/value for `each_key`/`each_value`). A proc/lambda VALUE is reliable
2105+ only for the `each` pair forwarded to a single param, whose pair-array
2106+ type the call-site inference recovers (a container overriding the bare-int
2107+ default). Every other hash + proc/lambda combination -- inline lambdas
2108+ (cloned, no write to type from), multi-param procs (CRuby auto-splat), and
2109+ the scalar `each_key`/`each_value` yields -- needs cross-procedural param
2110+ typing not yet modeled, so decline to the pre-existing path. */
2111+ int wrap_pair = 0;
2112+ if (ty_is_hash(rt)) {
2113+ if (ct == TY_METHOD) wrap_pair = (arity == 2);
2114+ else if (arity == 2 && simple_ref && fwd_callable_arity(c, ex) == 1) wrap_pair = 1;
2115+ else continue;
2116+ }
2117+
20742118 int base = nt->count;
20752119 int proc_clone = nt_clone_subtree(nt, ex); /* re-read the proc per element */
20762120 if (proc_clone < 0) continue;
@@ -2093,9 +2137,7 @@
20932137 nt_node_set_ref(nt, bparams, "parameters", params);
20942138
20952139 int callargs = nt_new_node(nt, "ArgumentsNode");
2096- /* hash each/each_pair (2 yielded params) forwards the [k, v] pair as one
2097- array argument; element iterators forward their values positionally. */
2098- if (ty_is_hash(rt) && arity == 2) {
2140+ if (wrap_pair) {
20992141 int pairarr = nt_new_node(nt, "ArrayNode");
21002142 nt_node_set_arr(nt, pairarr, "elements", reads, 2);
21012143 nt_node_set_arr(nt, callargs, "arguments", &pairarr, 1);
@@ -2437,7 +2479,14 @@
24372479 if (!lv) continue;
24382480 TyKind at = infer_type(c, argv[k]);
24392481 if (at == TY_UNKNOWN || at == lv->type) continue;
2440- TyKind merged = ty_unify(lv->type, at);
2482+ /* A container arg (array/hash/string/object) overrides a bare int param:
2483+ the int is the proc/lambda default, which would otherwise unify with a
2484+ container to POLY (a scalar) and lose the container type -- breaking a
2485+ forwarded `&proc` into a poly/hash iterator, whose param is the pair
2486+ array or element. A genuine multi-type param still converges to POLY
2487+ via the other call site's unify. */
2488+ int at_container = ty_is_array(at) || ty_is_hash(at) || at == TY_STRING || ty_is_object(at);
2489+ TyKind merged = (lv->type == TY_INT && at_container) ? at : ty_unify(lv->type, at);
24412490 if (merged != lv->type) { lv->type = merged; changed = 1; }
24422491 }
24432492 }