Commit f1e3773c

f1e3773cedf1Merged

Ryan Seys <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

analyze,codegen: carry a proc's explicit return value out through #call

A lambda or proc whose body ends in an explicit return <expr> lost its value: lambda { return 7 }.call yielded 0. The return-type of a proc literal was computed from the tail statement's type, but a return tail is a ReturnNode that infer_type does not value-type, so the proc was typed poly while its body still returned through the mrb_int channel; the call site then read the never-written poly return slot.

Type the proc by the ReturnNode's value (return_node_type) in proc_node_ret, and in emit_proc_literal derive the body-return ABI from the effective tail value. The pointer-launder and poly/float return paths now use that value node so a returned string, array, or float round-trips like an implicit tail.

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

Details

Commit
f1e3773cedf1
Parent
123dc3bccff7

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;