Commit 1b86a411

1b86a411fe89Merged

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

Description

First-class Proc: void-bodied proc returns

A proc whose body's last expression has no value (e.g. proc { puts x }) now compiles: emit the body as plain statements and return nil (0) over the mrb_int slot, instead of deferring as an unsupported return type.

make test: 171 pass / 5 fail / 668 error (was 170 / 5 / 669), no regressions.

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

Details

Commit
1b86a411fe89
Parent
ab99632e1196

src/codegen.cmodifiedView file

@@ -3928,7 +3928,8 @@
39283928 prelude (not available in this manual return path) and defer for now, as do
39293929 float/poly/range/time, which don't fit the slot. */
39303930 int ret_ptr = (ret == TY_STRING);
3931- if (!proc_slot_is_direct(ret) && !ret_ptr) {
3931+ int ret_void = (ret == TY_VOID); /* body's last expr has no value (e.g. puts) */
3932+ if (!proc_slot_is_direct(ret) && !ret_ptr && !ret_void) {
39323933 free(params.v); free(used.v); free(locals.v); free(caps.v);
39333934 unsupported(c, create, "proc with array/hash/object/float/poly return (later slice)");
39343935 return;
@@ -4007,6 +4008,11 @@
40074008 if (bn > 0) emit_expr(c, bb[bn - 1], pb); else buf_puts(pb, "0");
40084009 buf_puts(pb, ");\n");
40094010 }
4011+ else if (ret_void) {
4012+ /* no usable value: run the body as plain statements, return nil (0) */
4013+ emit_stmts(c, body, pb, 1);
4014+ buf_puts(pb, " return 0;\n");
4015+ }
40104016 else {
40114017 emit_stmts_tail(c, body, pb, 1);
40124018 buf_puts(pb, " return 0;\n");