Commit a62a1ad5

a62a1ad58dfaMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

Implement self-recursive yield method lowering (is_lowered_yield)

Methods that both use implicit yield and call themselves recursively are lowered to an explicit &block (sp_Proc *) form: a synthetic yblk parameter replaces yield, and each yield site becomes sp_proc_call(lv___yblk__, ...). The lowering runs after compute_reachable so reachability is already set.

Return type: lowered methods return mrb_int (raw carrier). Call sites use per-call-site block return type (via method_call_ret) to cast the raw mrb_int to const char * (string blocks) or use it directly (int).

Details:

  • analyze.c: detect pass moved after compute_reachable; sets is_lowered_yield=1, yields=0, ret=TY_INT, blk_param=yblk
  • analyze.c: method_call_ret uses is_lowered_yield as a trigger to return the call site's block body type (per-call type dispatch)
  • codegen.c: YieldNode in lowered body emits sp_proc_call without cast; emit_proc_literal force-captures yblk when body has yield; emit_scope_decls handles TY_PROC cell with uintptr_t cast

Fixes test/recursive_implicit_yield_value.rb (was ERR, now PASS).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
a62a1ad58dfa
Parent
0f87d407fae9

src/analyze.cmodifiedView file

@@ -145,7 +145,9 @@
145145 static TyKind method_call_ret(Compiler *c, int mi, int call_id) {
146146 int last = scope_body_last(c, mi);
147147 int is_yield = last >= 0 && nt_type(c->nt, last) && !strcmp(nt_type(c->nt, last), "YieldNode");
148- if (is_yield || is_blk_param_call(c, last, mi)) {
148+ /* Lowered yield methods (self-recursive + yield) carry the block's return value:
149+ return the per-call-site block body type so puts/assign use the right type. */
150+ if (c->scopes[mi].is_lowered_yield || is_yield || is_blk_param_call(c, last, mi)) {
149151 int blk = nt_ref(c->nt, call_id, "block");
150152 if (blk >= 0) {
151153 int bbody = nt_ref(c->nt, blk, "body");
@@ -5379,6 +5381,46 @@
53795381 (e.g. a never-called method with an uninferrable param). */
53805382 compute_reachable(c);
53815383
5384+ /* Lower self-recursive yield methods: methods that use `yield` AND call
5385+ themselves recursively. Their implicit block is forwarded as a synthetic
5386+ __yblk__ sp_Proc * parameter, so the method is emitted (yields=0) and
5387+ each `yield` in its body calls sp_proc_call(__yblk__, ...). */
5388+ for (int mi = 1; mi < c->nscopes; mi++) {
5389+ Scope *m = &c->scopes[mi];
5390+ if (!m->name || !m->reachable || m->blk_param) continue;
5391+ if (!m->yields) continue;
5392+ if (m->body < 0) continue;
5393+ int has_yld = 0;
5394+ for (int id = 0; id < c->nt->count && !has_yld; id++) {
5395+ if (c->nscope[id] != mi) continue;
5396+ const char *ty = nt_type(c->nt, id);
5397+ if (ty && !strcmp(ty, "YieldNode")) has_yld = 1;
5398+ }
5399+ if (!has_yld) continue;
5400+ int has_self_call = 0;
5401+ for (int id = 0; id < c->nt->count && !has_self_call; id++) {
5402+ if (c->nscope[id] != mi) continue;
5403+ const char *ty = nt_type(c->nt, id);
5404+ if (!ty || strcmp(ty, "CallNode")) continue;
5405+ const char *nm = nt_str(c->nt, id, "name");
5406+ if (!nm || strcmp(nm, m->name)) continue;
5407+ int recv = nt_ref(c->nt, id, "receiver");
5408+ const char *rty = recv >= 0 ? nt_type(c->nt, recv) : NULL;
5409+ if (recv < 0 || (rty && !strcmp(rty, "SelfNode"))) has_self_call = 1;
5410+ }
5411+ if (!has_self_call) continue;
5412+ m->is_lowered_yield = 1;
5413+ m->yields = 0;
5414+ m->ret = TY_INT;
5415+ m->blk_param = strdup("__yblk__");
5416+ LocalVar *yblk = scope_local_intern(m, "__yblk__");
5417+ if (yblk) {
5418+ yblk->type = TY_PROC;
5419+ yblk->is_param = 1;
5420+ yblk->is_cell = 1;
5421+ }
5422+ }
5423+
53825424 /* Post-fixpoint: propagate include-copy param types back to the source
53835425 scope so the final infer_type scan (which uses comp_scope_of, mapping
53845426 body nodes to the ORIGINAL scope) sees the correctly-typed params.