Commit 05c46587
05c46587ca97Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Add each_cons/each_cons.with_index .map chain fusion with |(a,b)| destructure
Analyzer:
- Add block_param_is_multi/count/leaf helpers to detect and walk MultiTargetNode params (|(a, b)| style destructuring)
- Extend infer_block_params to seed leaf locals from each_cons/each_slice element type when the first block param is a MultiTargetNode
- Handle the each_cons.with_index.map 3-level chain in infer_block_params so |(a,b),i| and |pair,i| get correct types
- Add result type inference for each_cons.map and each_cons.with_index.map chains in infer_call (mirrors existing each_slice.map handler)
Refs #566
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 05c46587ca97
- Parent
- 361843a0e73e
src/analyze.cmodifiedView file
@@ -878,6 +878,37 @@
878878
}879879
}880880
881+
/* each_cons(n).map/collect { |...| } chain: return array of block result type */882+
if (recv >= 0 && rt == TY_UNKNOWN && (!strcmp(name, "map") || !strcmp(name, "collect")) &&883+
nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&884+
nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "each_cons") &&885+
nt_ref(nt, recv, "block") < 0) {886+
int blk_ec = nt_ref(nt, id, "block");887+
if (blk_ec >= 0) {888+
int body_ec = nt_ref(nt, blk_ec, "body");889+
int bn_ec = 0; const int *bb_ec = body_ec >= 0 ? nt_arr(nt, body_ec, "body", &bn_ec) : NULL;890+
return ty_array_of(bn_ec > 0 ? infer_type(c, bb_ec[bn_ec - 1]) : TY_UNKNOWN);891+
}892+
}893+
894+
/* each_cons(n).with_index(off).map/collect { |...| } chain */895+
if (recv >= 0 && rt == TY_UNKNOWN && (!strcmp(name, "map") || !strcmp(name, "collect")) &&896+
nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&897+
nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "with_index") &&898+
nt_ref(nt, recv, "block") < 0) {899+
int wi_recv = nt_ref(nt, recv, "receiver");900+
if (wi_recv >= 0 && nt_type(nt, wi_recv) && !strcmp(nt_type(nt, wi_recv), "CallNode") &&901+
nt_str(nt, wi_recv, "name") && !strcmp(nt_str(nt, wi_recv, "name"), "each_cons") &&902+
nt_ref(nt, wi_recv, "block") < 0) {903+
int blk_wi = nt_ref(nt, id, "block");904+
if (blk_wi >= 0) {905+
int body_wi = nt_ref(nt, blk_wi, "body");906+
int bn_wi = 0; const int *bb_wi = body_wi >= 0 ? nt_arr(nt, body_wi, "body", &bn_wi) : NULL;907+
return ty_array_of(bn_wi > 0 ? infer_type(c, bb_wi[bn_wi - 1]) : TY_UNKNOWN);908+
}909+
}910+
}911+
881912
/* array receiver methods */882913
if (recv >= 0 && ty_is_array(rt)) {883914
int block = nt_ref(nt, id, "block");@@ -3696,6 +3727,45 @@
36963727
return NULL;36973728
}36983729
3730+
int block_param_is_multi(Compiler *c, int block, int idx) {3731+
int bp = nt_ref(c->nt, block, "parameters");3732+
if (bp < 0) return 0;3733+
int pn = nt_ref(c->nt, bp, "parameters");3734+
if (pn < 0) return 0;3735+
int n = 0;3736+
const int *reqs = nt_arr(c->nt, pn, "requireds", &n);3737+
if (idx >= n) return 0;3738+
const char *ty = nt_type(c->nt, reqs[idx]);3739+
return (ty && !strcmp(ty, "MultiTargetNode"));3740+
}3741+
3742+
int block_param_multi_count(Compiler *c, int block, int idx) {3743+
int bp = nt_ref(c->nt, block, "parameters");3744+
if (bp < 0) return 0;3745+
int pn = nt_ref(c->nt, bp, "parameters");3746+
if (pn < 0) return 0;3747+
int n = 0;3748+
const int *reqs = nt_arr(c->nt, pn, "requireds", &n);3749+
if (idx >= n) return 0;3750+
int lc = 0;3751+
nt_arr(c->nt, reqs[idx], "lefts", &lc);3752+
return lc;3753+
}3754+
3755+
const char *block_param_multi_leaf(Compiler *c, int block, int idx, int leaf_idx) {3756+
int bp = nt_ref(c->nt, block, "parameters");3757+
if (bp < 0) return NULL;3758+
int pn = nt_ref(c->nt, bp, "parameters");3759+
if (pn < 0) return NULL;3760+
int n = 0;3761+
const int *reqs = nt_arr(c->nt, pn, "requireds", &n);3762+
if (idx >= n) return NULL;3763+
int lc = 0;3764+
const int *lefts = nt_arr(c->nt, reqs[idx], "lefts", &lc);3765+
if (!lefts || leaf_idx >= lc) return NULL;3766+
return nt_str(c->nt, lefts[leaf_idx], "name");3767+
}3768+
36993769
/* First YieldNode belonging to scope `si`, or -1. */37003770
static int first_yield(Compiler *c, int si) {37013771
for (int id = 0; id < c->nt->count; id++) {@@ -3837,10 +3907,10 @@
38373907
if (recv < 0) continue;38383908
TyKind rt = infer_type(c, recv);38393909
const char *p0 = block_param_name(c, block, 0);3840-
if (!p0) continue;3910+
if (!p0 && !block_param_is_multi(c, block, 0)) continue;38413911
38423912
/* then / yield_self: block param receives the receiver value */3843-
if (!strcmp(name, "then") || !strcmp(name, "yield_self")) {3913+
if ((!strcmp(name, "then") || !strcmp(name, "yield_self")) && p0) {38443914
Scope *bs = comp_scope_of(c, block);38453915
LocalVar *lv = scope_local_intern(bs, p0); lv->is_block_param = 1;38463916
TyKind m = ty_unify(lv->type, rt);@@ -3917,23 +3987,38 @@
39173987
pt = TY_POLY;39183988
39193989
/* array.each_cons(n) / each_slice(n) { |a, b, ...| } -- a single param3920-
binds the n-element sub-array; multiple params destructure elements */3990+
binds the n-element sub-array; multiple params destructure elements.3991+
Also handles |(a, b)| destructuring: leaves bind to element type. */39213992
if ((!strcmp(name, "each_cons") || !strcmp(name, "each_slice")) && ty_is_array(rt)) {39223993
Scope *es = comp_scope_of(c, block);39233994
int np = 0; while (block_param_name(c, block, np)) np++;3924-
for (int pj = 0; pj < np; pj++) {3925-
const char *pn = block_param_name(c, block, pj);3926-
LocalVar *lp = scope_local_intern(es, pn); lp->is_block_param = 1;3927-
TyKind want = (np == 1) ? rt : ty_array_elem(rt);3928-
TyKind m = ty_unify(lp->type, want);3929-
if (m != lp->type) { lp->type = m; changed = 1; }3995+
if (np == 0 && block_param_is_multi(c, block, 0)) {3996+
TyKind elem = ty_array_elem(rt);3997+
int lc = block_param_multi_count(c, block, 0);3998+
for (int li = 0; li < lc; li++) {3999+
const char *ln = block_param_multi_leaf(c, block, 0, li);4000+
if (!ln) continue;4001+
LocalVar *lp = scope_local_intern(es, ln); lp->is_block_param = 1;4002+
TyKind m = ty_unify(lp->type, elem);4003+
if (m != lp->type) { lp->type = m; changed = 1; }4004+
}4005+
}4006+
else {4007+
for (int pj = 0; pj < np; pj++) {4008+
const char *pn = block_param_name(c, block, pj);4009+
LocalVar *lp = scope_local_intern(es, pn); lp->is_block_param = 1;4010+
TyKind want = (np == 1) ? rt : ty_array_elem(rt);4011+
TyKind m = ty_unify(lp->type, want);4012+
if (m != lp->type) { lp->type = m; changed = 1; }4013+
}39304014
}39314015
continue;39324016
}39334017
39344018
/* array.each_slice(n).map/collect { |x, y, ...| } chain: each block param39354019
gets the element type of the original array (slice elements).3936-
array.each_cons(n).map { |pair| } chain: block param gets the array type. */4020+
array.each_cons(n).map { |pair| } chain: block param gets the array type.4021+
Also handles |(a, b)| destructuring as the first param. */39374022
if ((!strcmp(name, "map") || !strcmp(name, "collect")) && rt == TY_UNKNOWN &&39384023
nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&39394024
nt_str(nt, recv, "name") && (!strcmp(nt_str(nt, recv, "name"), "each_slice") ||@@ -3943,24 +4028,84 @@
39434028
TyKind arr_t2 = es_recv2 >= 0 ? infer_type(c, es_recv2) : TY_UNKNOWN;39444029
int is_cons2 = !strcmp(nt_str(nt, recv, "name"), "each_cons");39454030
if (ty_is_array(arr_t2)) {3946-
/* each_slice: block params get element type; each_cons: single param3947-
gets the array type (a sub-array slice of the original). */39484031
TyKind bp_t2 = is_cons2 ? arr_t2 : ty_array_elem(arr_t2);39494032
if (bp_t2 != TY_UNKNOWN) {39504033
Scope *es2 = comp_scope_of(c, block);39514034
int np2 = 0; while (block_param_name(c, block, np2)) np2++;3952-
for (int pj2 = 0; pj2 < np2; pj2++) {3953-
const char *pn2 = block_param_name(c, block, pj2);3954-
if (!pn2) break;3955-
LocalVar *lp2 = scope_local_intern(es2, pn2); lp2->is_block_param = 1;3956-
TyKind m2 = ty_unify(lp2->type, bp_t2);3957-
if (m2 != lp2->type) { lp2->type = m2; changed = 1; }4035+
if (np2 == 0 && is_cons2 && block_param_is_multi(c, block, 0)) {4036+
/* |(a, b)| destructuring: each leaf gets element type */4037+
TyKind elem2 = ty_array_elem(arr_t2);4038+
if (elem2 != TY_UNKNOWN) {4039+
int lc2 = block_param_multi_count(c, block, 0);4040+
for (int li = 0; li < lc2; li++) {4041+
const char *ln = block_param_multi_leaf(c, block, 0, li);4042+
if (!ln) continue;4043+
LocalVar *lp = scope_local_intern(es2, ln); lp->is_block_param = 1;4044+
TyKind m2 = ty_unify(lp->type, elem2);4045+
if (m2 != lp->type) { lp->type = m2; changed = 1; }4046+
}4047+
}39584048
}4049+
else {4050+
for (int pj2 = 0; pj2 < np2; pj2++) {4051+
const char *pn2 = block_param_name(c, block, pj2);4052+
if (!pn2) break;4053+
LocalVar *lp2 = scope_local_intern(es2, pn2); lp2->is_block_param = 1;4054+
TyKind m2 = ty_unify(lp2->type, bp_t2);4055+
if (m2 != lp2->type) { lp2->type = m2; changed = 1; }4056+
}4057+
}39594058
continue;39604059
}39614060
}39624061
}39634062
4063+
/* array.each_cons(n).with_index(off).map { |pair, i| } or { |(a,b), i| } chain */4064+
if ((!strcmp(name, "map") || !strcmp(name, "collect")) && rt == TY_UNKNOWN &&4065+
nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&4066+
nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "with_index") &&4067+
nt_ref(nt, recv, "block") < 0) {4068+
int wi_recv = nt_ref(nt, recv, "receiver");4069+
if (wi_recv >= 0 && nt_type(nt, wi_recv) && !strcmp(nt_type(nt, wi_recv), "CallNode") &&4070+
nt_str(nt, wi_recv, "name") && !strcmp(nt_str(nt, wi_recv, "name"), "each_cons") &&4071+
nt_ref(nt, wi_recv, "block") < 0) {4072+
int ec_recv = nt_ref(nt, wi_recv, "receiver");4073+
TyKind ec_arr_t = ec_recv >= 0 ? infer_type(c, ec_recv) : TY_UNKNOWN;4074+
if (ty_is_array(ec_arr_t)) {4075+
Scope *wi_es = comp_scope_of(c, block);4076+
TyKind elem_t = ty_array_elem(ec_arr_t);4077+
/* p0 is the pair (array) or |(a,b)| multi-target; p1 is the int index */4078+
const char *idx_p = block_param_name(c, block, 1);4079+
if (idx_p) {4080+
LocalVar *ip = scope_local_intern(wi_es, idx_p); ip->is_block_param = 1;4081+
TyKind im = ty_unify(ip->type, TY_INT);4082+
if (im != ip->type) { ip->type = im; changed = 1; }4083+
}4084+
if (block_param_is_multi(c, block, 0)) {4085+
/* |(a, b), i|: destructure first multi-target param */4086+
int lc3 = block_param_multi_count(c, block, 0);4087+
for (int li = 0; li < lc3; li++) {4088+
const char *ln = block_param_multi_leaf(c, block, 0, li);4089+
if (!ln) continue;4090+
LocalVar *lp = scope_local_intern(wi_es, ln); lp->is_block_param = 1;4091+
TyKind m3 = ty_unify(lp->type, elem_t);4092+
if (m3 != lp->type) { lp->type = m3; changed = 1; }4093+
}4094+
}4095+
else {4096+
/* |pair, i|: pair gets the sub-array type */4097+
const char *pair_p = block_param_name(c, block, 0);4098+
if (pair_p) {4099+
LocalVar *pp = scope_local_intern(wi_es, pair_p); pp->is_block_param = 1;4100+
TyKind m3 = ty_unify(pp->type, ec_arr_t);4101+
if (m3 != pp->type) { pp->type = m3; changed = 1; }4102+
}4103+
}4104+
continue;4105+
}4106+
}4107+
}4108+
39644109
/* array.combination(k) { |c| } binds the k-element sub-array (same kind) */39654110
if (!strcmp(name, "combination") && ty_is_array(rt)) {39664111
LocalVar *lp = scope_local_intern(comp_scope_of(c, block), p0); lp->is_block_param = 1;@@ -4196,6 +4341,7 @@
41964341
continue;41974342
}41984343
}4344+
if (!p0) continue;41994345
LocalVar *lv = scope_local_intern(s, p0); lv->is_block_param = 1;42004346
/* Don't widen an array-typed variable to a scalar via block-param42014347
inference. When the variable already holds an array (set by a writesrc/analyze.hmodifiedView file
Loading diff…