Commit b578174c

b578174c6c6aMerged

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

Description

codegen: fix yield/block handling in inlined methods

  • Skip yield statements (emit nothing) when inlining a yield-method without a block (dead code under if (0) guard)
  • Yield as expression emits SP_INT_NIL when no block provided
  • Treat &block param as a condition (like block_given?) in emit_cond
  • Use result-var pattern for as_expr inlining so GCC statement expressions get a proper value instead of a void if/else
  • Inline class method (Cls.method) yield calls by looking up cmethod via ConstantReadNode receiver; skip self-binding for class methods

Fixes block2, block_optional_unused, block_param_call_block_given.

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

Details

Commit
b578174c6c6a
Parent
05e95ac2addb

src/codegen.cmodifiedView file

@@ -4443,10 +4443,20 @@
44434443 }
44444444 }
44454445 else {
4446- TyKind rt = comp_ntype(c, recv); /* instance method */
4447- if (!ty_is_object(rt)) return 0;
4448- recv_class = ty_object_class(rt);
4449- mi = comp_method_in_chain(c, recv_class, name, NULL);
4446+ TyKind rt = comp_ntype(c, recv);
4447+ const char *rty = nt_type(nt, recv);
4448+ if (rty && !strcmp(rty, "ConstantReadNode")) {
4449+ /* Cls.method with a yield block: look up as a class method */
4450+ const char *cname = nt_str(nt, recv, "name");
4451+ int ci = cname ? comp_class_index(c, cname) : -1;
4452+ if (ci < 0) return 0;
4453+ mi = comp_cmethod_in_chain(c, ci, name, NULL);
4454+ }
4455+ else if (ty_is_object(rt)) {
4456+ recv_class = ty_object_class(rt);
4457+ mi = comp_method_in_chain(c, recv_class, name, NULL);
4458+ }
4459+ else return 0;
44504460 }
44514461 (void)implicit_self;
44524462 if (mi < 0) return 0;
@@ -4477,7 +4487,7 @@
44774487 if (as_expr) buf_puts(b, "({\n");
44784488 else { emit_indent(b, indent); buf_puts(b, "{\n"); }
44794489 /* instance method: bind self to the receiver (a pointer) */
4480- if (recv >= 0) {
4490+ if (recv >= 0 && recv_class >= 0) {
44814491 int st = ++g_tmp;
44824492 emit_indent(b, indent + 1);
44834493 buf_printf(b, "sp_%s *_t%d = ", c->classes[recv_class].name, st);
@@ -4515,7 +4525,21 @@
45154525 buf_puts(b, ";\n");
45164526 }
45174527
4518- emit_stmts(c, m->body, b, din);
4528+ if (as_expr) {
4529+ /* Use a result var so the tail uses assignment, not `return`, in the
4530+ GCC statement-expression ({ ... result_var; }) context. */
4531+ TyKind rt = comp_ntype(c, id);
4532+ int rtag = ++g_tmp;
4533+ char rvbuf[32]; snprintf(rvbuf, sizeof rvbuf, "_t%d", rtag);
4534+ emit_indent(b, din); emit_ctype(c, rt, b);
4535+ buf_printf(b, " _t%d = %s;\n", rtag, default_value(rt));
4536+ const char *sv_rv = g_result_var; g_result_var = rvbuf;
4537+ int sp = g_result_poly; g_result_poly = (rt == TY_POLY);
4538+ emit_stmts_tail(c, m->body, b, din);
4539+ g_result_var = sv_rv; g_result_poly = sp;
4540+ emit_indent(b, din); buf_printf(b, "_t%d;\n", rtag);
4541+ }
4542+ else emit_stmts(c, m->body, b, din);
45194543 if (as_expr) { emit_indent(b, indent); buf_puts(b, "})"); }
45204544 else { emit_indent(b, indent); buf_puts(b, "}\n"); }
45214545
@@ -5087,7 +5111,7 @@
50875111 }
50885112 if (!strcmp(ty, "LocalVariableReadNode")) { emit_local_ref(c, id, nt_str(nt, id, "name"), b); return; }
50895113 if (!strcmp(ty, "YieldNode")) {
5090- if (g_block_id < 0) unsupported(c, id, "yield (no block in scope)");
5114+ if (g_block_id < 0) { buf_puts(b, "SP_INT_NIL"); return; } /* no block: yield is nil */
50915115 emit_block_invoke(c, nt_ref(nt, id, "arguments"), b, 0, 1);
50925116 return;
50935117 }
@@ -5705,6 +5729,18 @@
57055729 if (t == TY_INT) { buf_puts(b, "(("); emit_expr(c, id, b); buf_puts(b, ") != SP_INT_NIL)"); return; }
57065730 if (t == TY_FLOAT) { buf_puts(b, "(!sp_float_is_nil("); emit_expr(c, id, b); buf_puts(b, "))"); return; }
57075731 if (t == TY_SYMBOL) { buf_puts(b, "(("); emit_expr(c, id, b); buf_puts(b, "), 1)"); return; }
5732+ /* &block parameter used as condition — same semantics as block_given? */
5733+ if (t == TY_UNKNOWN) {
5734+ const char *nty = nt_type(c->nt, id);
5735+ if (nty && !strcmp(nty, "LocalVariableReadNode")) {
5736+ const char *nm = nt_str(c->nt, id, "name");
5737+ Scope *s = nm ? comp_scope_of(c, id) : NULL;
5738+ if (s && s->blk_param && nm && !strcmp(s->blk_param, nm)) {
5739+ buf_puts(b, g_block_id >= 0 ? "1" : "0");
5740+ return;
5741+ }
5742+ }
5743+ }
57085744 if (t != TY_BOOL) unsupported(c, id, "condition (non-bool)");
57095745 emit_expr(c, id, b);
57105746 }
@@ -6175,7 +6211,7 @@
61756211 if (!ty) unsupported(c, id, "statement (no type)");
61766212
61776213 if (!strcmp(ty, "YieldNode")) {
6178- if (g_block_id < 0) unsupported(c, id, "yield (no block in scope)");
6214+ if (g_block_id < 0) return; /* inlined without block: yield is dead code */
61796215 emit_block_invoke(c, nt_ref(nt, id, "arguments"), b, indent, 0);
61806216 return;
61816217 }