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);&:symkeeps 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
elsewhere. Lets bare calls/ivar refs in the block resolve against the receiver's class during inference (codegen mirrors this via an_ie_class_id). */int *g_ie_node_class = NULL;static int g_ie_node_class_cap = 0;void mark_ie_subtree(Compiler *c, int node, int cls) { if (node < 0) return;/* (Re)build the instance_eval/exec node→class map from current receiver types. */void build_ie_map(Compiler *c) { const NodeTable *nt = c->nt; if (!g_ie_node_class) g_ie_node_class = malloc(sizeof(int) * (size_t)nt->count); /* sized to nt->count, which can grow mid-analysis when forwarded callables are desugared into synthetic blocks; resize so per-node writes stay bounded */ if (g_ie_node_class_cap < nt->count) { int *grown = realloc(g_ie_node_class, sizeof(int) * (size_t)nt->count); if (!grown) return; /* OOM: keep the old map rather than leak/deref NULL */ g_ie_node_class = grown; g_ie_node_class_cap = nt->count; } for (int i = 0; i < nt->count; i++) g_ie_node_class[i] = -1; for (int id = 0; id < nt->count; id++) { const char *ty = nt_type(nt, id); ch |= propagate_prep_params(c); ch |= infer_string_params(c); ch |= infer_default_param_types(c); ch |= desugar_value_callable_forwards(c); /* &proc -> { |x| proc.call(x) } */ ch |= infer_block_params(c); ch |= infer_for_index(c); /* Resolve constant types before ivar inference: a destructured constantsrc/analyze_internal.hmodifiedView file
Loading diff…
src/analyze_pass.cmodifiedView file
Loading diff…
test/block_forward_value_callable.rbaddedView file
Loading diff…
test/block_forward_value_callable.rb.expectedaddedView file
Loading diff…