Commit 8f76ecfb
8f76ecfb2b2eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #1514 from ryanseys/rs-enumerable-via-each
Desugar bare Enumerable calls on a user #each class
Details
- Commit
- 8f76ecfb2b2e
- Parent
- fc138d1779ea
- Merge parent
- 40095ebf47db
src/analyze.cmodifiedView file
@@ -588,7 +588,10 @@
588588
/* The receiver class for a node inside an instance_eval/exec block, or -1. */589589
int ie_class_of(Compiler *c, int node) {590590
(void)c;591-
return (g_ie_node_class && node >= 0) ? g_ie_node_class[node] : -1;591+
/* g_ie_node_class is sized to nt->count per fixpoint iteration; a node592+
synthesized mid-iteration (id >= cap) has no instance_eval receiver yet. */593+
return (g_ie_node_class && node >= 0 && node < g_ie_node_class_cap)594+
? g_ie_node_class[node] : -1;592595
}593596
594597
/* Register an ivar first assigned inside an instance_exec/instance_eval block on@@ -1446,6 +1449,59 @@
14461449
free(cls); free(eachdef);14471450
}14481451
1452+
/* Enumerable methods that work on an array receiver in Spinel; a bare call to1453+
one of these on a user `#each` class (that does not define it) is redirected1454+
through the materialized array. Kept to methods the array path supports. */1455+
static int is_array_enum_method(const char *nm) {1456+
static const char *const names[] = {1457+
"map", "collect", "select", "filter", "reject", "to_a", "entries",1458+
"find", "detect", "find_index", "count", "sum", "min", "max",1459+
"include?", "first", "sort", "sort_by", "min_by", "max_by",1460+
"reduce", "inject", "each_with_index", "flat_map", "collect_concat",1461+
"any?", "all?", "none?", "one?", "take", "drop", "take_while", "drop_while",1462+
"filter_map", "partition", "group_by", "each_with_object", "tally",1463+
"find_all", "zip", "grep", "grep_v", NULL };1464+
for (int k = 0; names[k]; k++) if (!strcmp(nm, names[k])) return 1;1465+
return 0;1466+
}1467+
1468+
/* Redirect a bare Enumerable call -- `obj.map { }`, `obj.to_a`, ... -- on a1469+
user class that defines `#each` but not that method, through the synthesized1470+
`__enum_to_a` helper: `obj.<m>{blk}` becomes `obj.__enum_to_a.<m>{blk}`, so1471+
the array path handles it without an explicit `enum_for(:each)`. Runs in the1472+
inference fixpoint (needs the receiver's type, and the inserted node is typed1473+
on a later iteration). Returns 1 if anything changed. */1474+
int desugar_enum_method_recv(Compiler *c) {1475+
NodeTable *nt = (NodeTable *)c->nt;1476+
int changed = 0;1477+
int n0 = nt->count;1478+
for (int id = 0; id < n0; id++) {1479+
if (!nt_type(nt, id) || strcmp(nt_type(nt, id), "CallNode")) continue;1480+
const char *nm = nt_str(nt, id, "name");1481+
if (!nm || !is_array_enum_method(nm)) continue;1482+
int recv = nt_ref(nt, id, "receiver");1483+
if (recv < 0) continue;1484+
/* already redirected through __enum_to_a -> leave it (idempotent) */1485+
if (nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode")) {1486+
const char *rn = nt_str(nt, recv, "name");1487+
if (rn && !strcmp(rn, "__enum_to_a")) continue;1488+
}1489+
TyKind rt = infer_type(c, recv);1490+
if (!ty_is_object(rt)) continue;1491+
int cid = ty_object_class(rt);1492+
if (comp_method_in_chain(c, cid, "__enum_to_a", NULL) < 0) continue; /* not an #each class */1493+
if (comp_method_in_chain(c, cid, nm, NULL) >= 0) continue; /* class defines it */1494+
int wrap = nt_new_node(nt, "CallNode");1495+
nt_node_set_str(nt, wrap, "name", "__enum_to_a");1496+
nt_node_set_ref(nt, wrap, "receiver", recv);1497+
nt_node_set_ref(nt, id, "receiver", wrap);1498+
comp_grow_node_arrays(c);1499+
c->nscope[wrap] = c->nscope[id];1500+
changed = 1;1501+
}1502+
return changed;1503+
}1504+
14491505
void analyze_program(Compiler *c) {14501506
/* scope 0 = top level */14511507
Scope *top = comp_scope_new(c, NULL, -1);@@ -1671,6 +1727,7 @@
16711727
ch |= infer_string_params(c);16721728
ch |= infer_default_param_types(c);16731729
ch |= desugar_enum_chain_to_a(c); /* each_slice(n).to_a -> .map{|s|s} */1730+
ch |= desugar_enum_method_recv(c); /* obj.map{} -> obj.__enum_to_a.map{} */16741731
ch |= desugar_implicit_send(c); /* send(:m, a) -> m(a) on self */16751732
ch |= desugar_value_callable_forwards(c); /* &proc -> { |x| proc.call(x) } */16761733
ch |= infer_block_params(c);test/enumerable_via_each.rbaddedView file
Loading diff…
test/enumerable_via_each.rb.expectedaddedView file
Loading diff…