Commit 40095ebf

40095ebf47dbMerged

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

Description

analyze: desugar bare Enumerable calls through __enum_to_a

With enum_for/to_enum materialization in place, a bare Enumerable call on a user class that defines #each but not that method -- bag.map { ... }, bag.to_a, bag.sort -- still had no target and folded to nothing. CRuby serves these from Enumerable by driving #each.

Redirect them through the same materialized array: in the inference fixpoint, rewrite recv.<m>{blk} to recv.__enum_to_a.<m>{blk} when <m> is an array-supported Enumerable method, the receiver's class defines __enum_to_a (i.e. it has a yielding #each), and the class does not define <m> itself. The existing array path then handles map/select/reject/to_a/sort/count/include?/ sum/min/max/find/any?/all?/... Running in the fixpoint gives the rewrite the receiver's type and re-infers the inserted node; the rewrite is idempotent (a receiver already routed through __enum_to_a is skipped) and only fires for methods the class does not define, so a user override still wins.

A class used with more than one element type materializes to a poly array, on which a few Enumerable methods are still unsupported -- unchanged from any other poly-array receiver, and a loud reject rather than wrong output.

Builds on the enum_for/to_enum materialization in #210.

Gates: 953 tests pass; bootstrap IR+C fixpoint byte-identical for analyze and codegen.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
40095ebf47db
Parent
5f96b1460568

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 node
592+ 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 to
1453+ one of these on a user `#each` class (that does not define it) is redirected
1454+ 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 a
1469+ user class that defines `#each` but not that method, through the synthesized
1470+ `__enum_to_a` helper: `obj.<m>{blk}` becomes `obj.__enum_to_a.<m>{blk}`, so
1471+ the array path handles it without an explicit `enum_for(:each)`. Runs in the
1472+ inference fixpoint (needs the receiver's type, and the inserted node is typed
1473+ 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);