Commit ab99632e

ab99632e1196Merged

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

Description

Type &block.call like yield; fix inline value/bail bugs (greens proc.rb)

A method that calls its &block param (block.call(x)) is the explicit-call equivalent of yield: route both through one yield_value_type() helper that resolves the value from a call site's block body (a void last-expr's block value is nil, per Ruby). This lets such methods type their return and their result = block.call(x) locals, so they inline in value position.

Two codegen correctness fixes this exposed:

  • emit_inline_call_x pre-checks body-local types and bails BEFORE emitting anything; the old mid-emit bail left an unbalanced { (malformed C).
  • emit_boxed evaluates a nil-typed RHS for side effects -- (expr, nil) -- instead of short-circuiting to the sp_box_nil() constant, which silently dropped a void block call's effects (e.g. the block's puts).

make test: 170 pass / 5 fail / 669 error (was 167 / 5 / 672); proc.rb plus two block-forwarding tests green, no regressions.

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

Details

Commit
ab99632e1196
Parent
e4d1b4a27f92

src/analyze.cmodifiedView file

@@ -64,9 +64,54 @@
6464 /* The return type of a call to method `mi`. A method whose body is just a
6565 bare `yield` returns the block's value -- and since it inlines per call
6666 site, use THIS site's block value type rather than the unified return. */
67+/* 1 if `node` is `<&block-param>.call(...)` / .() / [] for method mi -- the
68+ explicit-call equivalent of `yield`, inlined the same way. */
69+static int is_blk_param_call(Compiler *c, int node, int mi) {
70+ const NodeTable *nt = c->nt;
71+ if (node < 0 || !nt_type(nt, node) || strcmp(nt_type(nt, node), "CallNode")) return 0;
72+ const char *nm = nt_str(nt, node, "name");
73+ if (!nm || (strcmp(nm, "call") && strcmp(nm, "()") && strcmp(nm, "[]"))) return 0;
74+ int recv = nt_ref(nt, node, "receiver");
75+ if (recv < 0 || !nt_type(nt, recv) || strcmp(nt_type(nt, recv), "LocalVariableReadNode")) return 0;
76+ const char *rn = nt_str(nt, recv, "name");
77+ const char *bp = c->scopes[mi].blk_param;
78+ return rn && bp && bp[0] && !strcmp(rn, bp);
79+}
80+
81+/* The value type of `yield` / a `<&block-param>.call` inside method mi: the
82+ block-body value type at a (any) call site of mi. Polymorphic, resolved from
83+ the first matching caller -- matches how the rewrite inlines per call site. */
84+static TyKind yield_value_type(Compiler *c, int mi) {
85+ const NodeTable *nt = c->nt;
86+ for (int cid = 0; cid < nt->count; cid++) {
87+ const char *cty = nt_type(nt, cid);
88+ if (!cty || strcmp(cty, "CallNode")) continue;
89+ int blk = nt_ref(nt, cid, "block");
90+ if (blk < 0) continue;
91+ const char *cn = nt_str(nt, cid, "name");
92+ int crecv = nt_ref(nt, cid, "receiver");
93+ int rmi = -1;
94+ if (crecv < 0) {
95+ rmi = comp_method_index(c, cn);
96+ if (rmi < 0) { Scope *cs = comp_scope_of(c, cid); if (cs->class_id >= 0) rmi = comp_method_in_chain(c, cs->class_id, cn, NULL); }
97+ } else {
98+ TyKind crt = infer_type(c, crecv);
99+ if (ty_is_object(crt)) rmi = comp_method_in_chain(c, ty_object_class(crt), cn, NULL);
100+ }
101+ if (rmi != mi) continue;
102+ int bb = nt_ref(nt, blk, "body");
103+ int bn = 0; const int *bd = bb >= 0 ? nt_arr(nt, bb, "body", &bn) : NULL;
104+ if (bn == 0) return TY_NIL;
105+ TyKind bt = infer_type(c, bd[bn - 1]);
106+ return bt == TY_VOID ? TY_NIL : bt; /* a void last-expr's block value is nil */
107+ }
108+ return TY_UNKNOWN;
109+}
110+
67111 static TyKind method_call_ret(Compiler *c, int mi, int call_id) {
68112 int last = scope_body_last(c, mi);
69- if (last >= 0 && nt_type(c->nt, last) && !strcmp(nt_type(c->nt, last), "YieldNode")) {
113+ int is_yield = last >= 0 && nt_type(c->nt, last) && !strcmp(nt_type(c->nt, last), "YieldNode");
114+ if (is_yield || is_blk_param_call(c, last, mi)) {
70115 int blk = nt_ref(c->nt, call_id, "block");
71116 if (blk >= 0) {
72117 int bbody = nt_ref(c->nt, blk, "body");
@@ -164,6 +209,13 @@
164209 TyKind rt = recv >= 0 ? infer_type(c, recv) : TY_UNKNOWN;
165210 TyKind a0 = argc >= 1 ? infer_type(c, argv[0]) : TY_UNKNOWN;
166211
212+ /* `<&block-param>.call(...)` inside a yielding method: the explicit-call form
213+ of yield. Its value is the call-site block's value (resolved like yield). */
214+ {
215+ int emi = (int)(comp_scope_of(c, id) - c->scopes);
216+ if (emi > 0 && is_blk_param_call(c, id, emi)) return yield_value_type(c, emi);
217+ }
218+
167219 /* proc {} / lambda {} / Proc.new {} -> a first-class Proc value */
168220 if (is_proc_literal(c, id)) return TY_PROC;
169221
@@ -671,36 +723,8 @@
671723 if (hv == TY_UNKNOWN && kt == TY_STRING && vt != TY_UNKNOWN) return TY_STR_POLY_HASH;
672724 return hv;
673725 }
674- if (!strcmp(ty, "YieldNode")) {
675- /* value of yield = the block body's value at a call site of this method */
676- int mi = (int)(comp_scope_of(c, id) - c->scopes);
677- for (int cid = 0; cid < nt->count; cid++) {
678- const char *cty = nt_type(nt, cid);
679- if (!cty || strcmp(cty, "CallNode")) continue;
680- int blk = nt_ref(nt, cid, "block");
681- if (blk < 0) continue;
682- const char *cn = nt_str(nt, cid, "name");
683- int crecv = nt_ref(nt, cid, "receiver");
684- int rmi = -1;
685- if (crecv < 0) {
686- rmi = comp_method_index(c, cn);
687- if (rmi < 0) {
688- Scope *cs = comp_scope_of(c, cid);
689- if (cs->class_id >= 0) rmi = comp_method_in_chain(c, cs->class_id, cn, NULL);
690- }
691- }
692- else {
693- TyKind crt = infer_type(c, crecv);
694- if (ty_is_object(crt)) rmi = comp_method_in_chain(c, ty_object_class(crt), cn, NULL);
695- }
696- if (rmi != mi) continue;
697- int bb = nt_ref(nt, blk, "body");
698- int bn = 0;
699- const int *bd = bb >= 0 ? nt_arr(nt, bb, "body", &bn) : NULL;
700- return bn > 0 ? infer_type(c, bd[bn - 1]) : TY_NIL;
701- }
702- return TY_UNKNOWN;
703- }
726+ if (!strcmp(ty, "YieldNode"))
727+ return yield_value_type(c, (int)(comp_scope_of(c, id) - c->scopes));
704728 if (!strcmp(ty, "SuperNode") || !strcmp(ty, "ForwardingSuperNode")) {
705729 Scope *s = comp_scope_of(c, id);
706730 if (s->class_id < 0 || !s->name) return TY_UNKNOWN;