Commit e51563f7

e51563f7d39eMerged

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

Description

feat(block): forward a callable value to iterators via synthetic-block desugar

A callable forwarded with & to an iterator -- recv.<iter>(&f) where f is a Proc value, a Method object, an inline ->(){} lambda, or the method's own &block parameter -- is desugared in analysis into the equivalent literal block recv.<iter> { |x| f.call(x) }. The existing literal-block emitters then lower it for ANY iterator and element type, instead of a per-callable, per-iterator special case. This mirrors Ruby's own &obj => obj.to_proc model: once a callable is wrapped as a block, it is just a block.

Covered: map/collect/select/filter/reject/each, each_with_index, sort_by/min_by/ max_by, partition/group_by, find/count/sum/flat_map/take_while, ... on array and range receivers; callables that are a local/ivar Proc, a method param, a Method object (&m / &method(:m)), an inline &->(){} lambda, or a forwarded &block. Forwarding a &block to a value-producing iterator (e.g. select) was previously a hard "unsupported call"; a.map(&proc) / a.map(&method(:m)) previously mis-compiled to nils; a.find(&->(x){...}) previously failed to compile.

Mechanism:

  • arity + param types come from ty_block_yield (the builtin block-protocol oracle), so multi-param iterators and ranges desugar uniformly;
  • the callable is re-evaluated once per element, so it is restricted to side-effect-free forms (a local/ivar read, a method(:m) lookup, or a lambda literal); &:sym keeps its own existing to_proc path;
  • hash receivers are left alone -- the [k,v] pair / destructure semantics don't reduce to the simple per-element call this emits;
  • synthetic nodes use the append-only node-construction API; per-node analysis caches resize with the node table (the iexec node->class map grows defensively rather than assuming a fixed node count).

Out of scope: a callable whose return type is not inferable (a Proc passed as a parameter, or an inline lambda over non-int elements) mistypes a non-int result -- but that is a pre-existing proc return-inference gap that affects hand-written literal blocks calling the same callable too, not this desugar.

Details

Commit
e51563f7d39e
Parent
4a3d6d932be1

src/analyze.cmodifiedView file

@@ -399,6 +399,7 @@
399399 elsewhere. Lets bare calls/ivar refs in the block resolve against the
400400 receiver's class during inference (codegen mirrors this via an_ie_class_id). */
401401 int *g_ie_node_class = NULL;
402+static int g_ie_node_class_cap = 0;
402403
403404 void mark_ie_subtree(Compiler *c, int node, int cls) {
404405 if (node < 0) return;
@@ -542,7 +543,14 @@
542543 /* (Re)build the instance_eval/exec node→class map from current receiver types. */
543544 void build_ie_map(Compiler *c) {
544545 const NodeTable *nt = c->nt;
545- if (!g_ie_node_class) g_ie_node_class = malloc(sizeof(int) * (size_t)nt->count);
546+ /* sized to nt->count, which can grow mid-analysis when forwarded callables
547+ are desugared into synthetic blocks; resize so per-node writes stay bounded */
548+ if (g_ie_node_class_cap < nt->count) {
549+ int *grown = realloc(g_ie_node_class, sizeof(int) * (size_t)nt->count);
550+ if (!grown) return; /* OOM: keep the old map rather than leak/deref NULL */
551+ g_ie_node_class = grown;
552+ g_ie_node_class_cap = nt->count;
553+ }
546554 for (int i = 0; i < nt->count; i++) g_ie_node_class[i] = -1;
547555 for (int id = 0; id < nt->count; id++) {
548556 const char *ty = nt_type(nt, id);
@@ -1526,6 +1534,7 @@
15261534 ch |= propagate_prep_params(c);
15271535 ch |= infer_string_params(c);
15281536 ch |= infer_default_param_types(c);
1537+ ch |= desugar_value_callable_forwards(c); /* &proc -> { |x| proc.call(x) } */
15291538 ch |= infer_block_params(c);
15301539 ch |= infer_for_index(c);
15311540 /* Resolve constant types before ivar inference: a destructured constant