Commit 5d47ba89

5d47ba89882bMerged

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

Description

feat(proc): return a Float from a proc/lambda through .call

A proc/lambda whose body's value is a Float silently truncated to the int that the sp_proc_call ABI slot defaults to (or, before, deferred with an "unsupported proc with float/range/time return" diagnostic).

Box a float return through the existing _sp_proc_poly_ret poly slot: emit_proc_literal stores the boxed float (via the poly-result path, sp_box_float), and the <proc>.call / .() / [] site reads it back with sp_poly_to_f. This reuses the machinery already in place for poly returns; range/time still defer (no fixture exercises them yet).

Covers the direct-literal, proc-variable, and method-tail .call forms. The poly-receiver .call path (a proc stored in a poly container, e.g. [proc { 3.5 }][0].call) still reads the raw slot and is unchanged -- that path is already lossy for non-int returns (a stored string proc mis-prints there too); unifying it is separate work.

Adds test/proc_float_return.rb.

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

Details

Commit
5d47ba89882b
Parent
34f8cd5461a2

src/codegen.cmodifiedView file

@@ -870,15 +870,17 @@
870870 }
871871 /* The proc fn returns mrb_int (the ABI); heap-pointer values (strings,
872872 arrays, hashes, objects) are laundered through (mrb_int)(uintptr_t).
873- TY_POLY values are stored in _sp_proc_poly_ret (file-static sp_RbVal)
874- before return; the call site reads it back.
875- Float/range/time don't fit the slot and defer. */
873+ TY_POLY and float values are stored in _sp_proc_poly_ret (file-static
874+ sp_RbVal) before return -- float boxed via sp_box_float -- and the call
875+ site reads it back (unboxing float with sp_poly_to_f).
876+ Range/time don't fit the slot and defer. */
876877 int ret_ptr = proc_slot_is_ptr(ret);
877878 int ret_void = (ret == TY_VOID); /* body's last expr has no value (e.g. puts) */
878879 int ret_poly = (ret == TY_POLY);
879- if (!proc_slot_is_direct(ret) && !ret_ptr && !ret_void && !ret_poly) {
880+ int ret_fbox = (ret == TY_FLOAT); /* boxed through the poly return slot */
881+ if (!proc_slot_is_direct(ret) && !ret_ptr && !ret_void && !ret_poly && !ret_fbox) {
880882 free(params.v); free(used.v); free(locals.v); free(caps.v);
881- unsupported(c, create, "proc with float/range/time return (later slice)");
883+ unsupported(c, create, "proc with range/time return (later slice)");
882884 return;
883885 }
884886
@@ -969,9 +971,10 @@
969971 }
970972 else buf_puts(pb, " return 0;\n");
971973 }
972- else if (ret_poly) {
973- /* Store the sp_RbVal result in the file-static _sp_proc_poly_ret slot;
974- the call site reads it back after sp_proc_call returns. */
974+ else if (ret_poly || ret_fbox) {
975+ /* Store the result in the file-static _sp_proc_poly_ret slot (boxed:
976+ a float tail becomes sp_box_float via g_result_poly); the call site
977+ reads it back after sp_proc_call returns. */
975978 if (!g_needs_proc_poly_retslot) {
976979 g_needs_proc_poly_retslot = 1;
977980 buf_puts(&g_proc_protos, "static sp_RbVal _sp_proc_poly_ret;\n");