Commit 5bba0e44
5bba0e4447c4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified
Description
Add M10b: map/select/reject + fresh-recompute fixpoint fix
- analyze: array.map/collect returns an array of the block-body type; select/reject/filter return the source array type. block params get an is_block_param flag (typed by block-param inference, not assignments).
- analyze fixpoint: infer_write_types now recomputes each non-param,
non-block-param local's type FRESH each iteration (reset + unify all
write RHS) instead of monotonically widening. This lets a local narrow
as block-param/return inference improves (e.g.
b = a.map { |x| x*2 }resolves int_array instead of locking to a stale poly). - codegen: emit_collect_expr lowers map/select/reject to a result-array build loop in the statement prelude (receiver evaluated once; block body emitted into the loop; map pushes the body value, select/reject push the element on a true/false predicate).
map/select/reject over int and string arrays match CRuby. make test: 14.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 5bba0e4447c4
- Parent
- a4ae9d3001ed
src/analyze.cmodifiedView file
@@ -87,6 +87,19 @@
8787
8888
/* array receiver methods */8989
if (recv >= 0 && ty_is_array(rt)) {90+
int block = nt_ref(nt, id, "block");91+
if (block >= 0) {92+
if (!strcmp(name, "map") || !strcmp(name, "collect")) {93+
int body = nt_ref(nt, block, "body");94+
int bn = 0;95+
const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;96+
return ty_array_of(bn > 0 ? infer_type(c, bb[bn - 1]) : TY_UNKNOWN);97+
}98+
if (!strcmp(name, "select") || !strcmp(name, "reject") ||99+
!strcmp(name, "filter") || !strcmp(name, "sort_by") ||100+
!strcmp(name, "take_while") || !strcmp(name, "drop_while"))101+
return rt;102+
}90103
if (!strcmp(name, "[]")) return ty_array_elem(rt);91104
if (!strcmp(name, "length") || !strcmp(name, "size") ||92105
!strcmp(name, "count") || !strcmp(name, "index")) return TY_INT;@@ -442,6 +455,19 @@
442455
static int infer_write_types(Compiler *c) {443456
const NodeTable *nt = c->nt;444457
int changed = 0;458+
459+
/* Recompute non-param local types FRESH each iteration: reset to UNKNOWN460+
(saving the old value), then unify all write-site RHS types. This lets461+
a local NARROW as block-param/return inference improves, instead of462+
monotonically widening to POLY from a stale early estimate. */463+
for (int s = 0; s < c->nscopes; s++)464+
for (int i = 0; i < c->scopes[s].nlocals; i++) {465+
LocalVar *lv = &c->scopes[s].locals[i];466+
/* stash old type in gc_root (unused by codegen) so we can detect467+
change; block params are typed elsewhere, so leave them alone */468+
if (!lv->is_param && !lv->is_block_param) { lv->gc_root = (int)lv->type; lv->type = TY_UNKNOWN; }469+
}470+
445471
for (int id = 0; id < nt->count; id++) {446472
const char *ty = nt_type(nt, id);447473
if (!ty) continue;@@ -455,7 +481,7 @@
455481
Scope *s = comp_scope_of(c, id);456482
LocalVar *cur = nm ? scope_local(s, nm) : NULL;457483
TyKind vt = infer_type(c, nt_ref(nt, id, "value"));458-
TyKind ct = cur ? cur->type : TY_UNKNOWN;484+
TyKind ct = cur ? (TyKind)cur->gc_root : TY_UNKNOWN; /* old type */459485
if (ct == TY_STRING) newt = TY_STRING;460486
else if (ty_is_numeric(ct) && ty_is_numeric(vt))461487
newt = (ct == TY_FLOAT || vt == TY_FLOAT) ? TY_FLOAT : TY_INT;@@ -465,10 +491,16 @@
465491
}466492
if (!nm) continue;467493
LocalVar *lv = scope_local(comp_scope_of(c, id), nm);468-
if (!lv) continue;469-
TyKind merged = ty_unify(lv->type, newt);470-
if (merged != lv->type) { lv->type = merged; changed = 1; }494+
if (!lv || lv->is_param || lv->is_block_param) continue;495+
lv->type = ty_unify(lv->type, newt);471496
}497+
498+
/* detect change vs the stashed old types */499+
for (int s = 0; s < c->nscopes; s++)500+
for (int i = 0; i < c->scopes[s].nlocals; i++) {501+
LocalVar *lv = &c->scopes[s].locals[i];502+
if (!lv->is_param && !lv->is_block_param && (TyKind)lv->gc_root != lv->type) changed = 1;503+
}472504
return changed;473505
}474506
@@ -566,12 +598,12 @@
566598
/* hash.each { |k, v| } binds two params */567599
if (!strcmp(name, "each") && ty_is_hash(rt)) {568600
Scope *hs = comp_scope_of(c, block);569-
LocalVar *kp = scope_local_intern(hs, p0);601+
LocalVar *kp = scope_local_intern(hs, p0); kp->is_block_param = 1;570602
TyKind km = ty_unify(kp->type, ty_hash_key(rt));571603
if (km != kp->type) { kp->type = km; changed = 1; }572604
const char *p1 = block_param_name(c, block, 1);573605
if (p1) {574-
LocalVar *vp = scope_local_intern(hs, p1);606+
LocalVar *vp = scope_local_intern(hs, p1); vp->is_block_param = 1;575607
TyKind vm = ty_unify(vp->type, ty_hash_val(rt));576608
if (vm != vp->type) { vp->type = vm; changed = 1; }577609
}@@ -580,7 +612,7 @@
580612
581613
if (pt == TY_UNKNOWN) continue;582614
Scope *s = comp_scope_of(c, block);583-
LocalVar *lv = scope_local_intern(s, p0);615+
LocalVar *lv = scope_local_intern(s, p0); lv->is_block_param = 1;584616
TyKind merged = ty_unify(lv->type, pt);585617
if (merged != lv->type) { lv->type = merged; changed = 1; }586618
}src/codegen.cmodifiedView file
Loading diff…
src/compiler.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…