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
if (body < 0) return TY_NIL; int bn = 0; const int *bb = nt_arr(nt, body, "body", &bn); return bn > 0 ? infer_type(c, bb[bn - 1]) : TY_NIL; if (bn <= 0) return TY_NIL; /* An explicit `return <expr>` tail is a ReturnNode, which infer_type does not value-type -- type the proc by the returned expression so the call site and the body's return channel agree (otherwise the value is silently lost). */ int tail = bb[bn - 1]; const char *tty = nt_type(nt, tail); if (tty && !strcmp(tty, "ReturnNode")) return return_node_type(c, tail); return infer_type(c, tail);}TyKind proc_ret_of(Compiler *c, int node) { const NodeTable *nt = c->nt;src/codegen.cmodifiedView file
Loading diff…
test/lambda_explicit_return.rbaddedView file
Loading diff…
test/lambda_explicit_return.rb.expectedaddedView file
Loading diff…