Commit 3d4efd9a

3d4efd9a07a7Merged

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

Description

Merge PR #1517: carry a proc's explicit return value out through #call

A lambda/proc whose body ends in an explicit return <expr> lost its value (lambda { return 7 }.call yielded 0). The proc was typed by the tail statement, but a ReturnNode tail is not value-typed by infer_type, so the proc was typed poly while its body returned through the mrb_int channel and the call site read the never-written poly slot.

proc_node_ret now types the proc by the ReturnNode's value (return_node_type), and emit_proc_literal derives the body-return ABI from the effective tail value node, routing the pointer-launder and poly/float return-slot paths through it. Adds test/lambda_explicit_return.rb.

Combined-state gates: test 1002/0/0, bench 57/57, optcarrot checksum 59662.

Closes #1517

Co-authored-by: Ryan Seys [email protected] Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
3d4efd9a07a7
Parent
1d70498652a8
Merge parent
f1e3773cedf1

src/analyze_util.cmodifiedView file

@@ -340,7 +340,14 @@
340340 if (body < 0) return TY_NIL;
341341 int bn = 0;
342342 const int *bb = nt_arr(nt, body, "body", &bn);
343- return bn > 0 ? infer_type(c, bb[bn - 1]) : TY_NIL;
343+ if (bn <= 0) return TY_NIL;
344+ /* An explicit `return <expr>` tail is a ReturnNode, which infer_type does not
345+ value-type -- type the proc by the returned expression so the call site and
346+ the body's return channel agree (otherwise the value is silently lost). */
347+ int tail = bb[bn - 1];
348+ const char *tty = nt_type(nt, tail);
349+ if (tty && !strcmp(tty, "ReturnNode")) return return_node_type(c, tail);
350+ return infer_type(c, tail);
344351 }
345352 TyKind proc_ret_of(Compiler *c, int node) {
346353 const NodeTable *nt = c->nt;