Commit 61a12a45

61a12a452f0dMerged

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

Description

fix(analyze,codegen): bridge no-recv IO calls; poly_array of procs; poly .call

Bundles three related fixes that the test fixture surfaces together:

  1. puts / print / printf reached in expression context — the last statement of a proc { ... } body, a consumed if-arm, a ternary branch. compile_no_recv_call_expr had no symmetric path for these no-recv IO methods, so the call fell through to warn_unresolved_call and emitted 0 without firing the side-effect. Fix: bridge through compile_io_call_stmt(nid, mname, -1) for the whitelist puts / print / printf (all three return nil in MRI), passing "0" up as the C expression value. Future no-recv void-IO methods need only a one-line whitelist addition. Also extracts compile_printf(nid) next to compile_print so both dispatchers share the emitter.

  2. Heterogeneous array literals of procs ([proc { ... }, ...]) were inferred as int_array and the construction emitted sp_IntArray_push(_, sp_proc_new(...)) — int-from-pointer C-compile error. Add a proc/lambda arm to infer_array_elem_type_from_ids in both spinel_analyze.rb and spinel_codegen.rb so such literals lower to poly_array and each element is boxed via sp_box_proc. Mirrors the existing is_hash_type(et) arm above.

  3. Poly .call(...) dispatch. When the per-element block param of each iterating a poly_array of procs binds with static type poly, p.call lands in compile_poly_method_call. Add an mname == "call" arm inside emit_poly_builtin_dispatch that fires for cls_id == SP_BUILTIN_PROC: unbox via (sp_Proc *) recv.v.p and invoke sp_proc_call with the same mrb_int[] arg-packing convention. Lives after the user-class loop, so classes with their own .call method (e.g. functor objects) still resolve first.

Test (test/io_call_in_proc_body.rb) iterates an array of procs and .calls each, exercising the IO bridge for puts / print / printf plus the array-of-procs inference and poly .call dispatch:

[ proc { puts "a" }, proc { print "b\n" }, proc { printf("c=%d\n", 3) } ].each { |p| p.call }

Symbol#to_proc (&:call) is still TODO in spinel (find_block_arg returns -1 for the SymbolNode shape); the explicit-block form is the closest equivalent that works today.

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

Details

Commit
61a12a452f0d
Parent
9c05e832a7c0

spinel_analyze.rbmodifiedView file

@@ -2433,6 +2433,18 @@
24332433 @needs_rb_value = 1
24342434 return "poly_array"
24352435 end
2436+ # Proc / lambda literals as elements (`[proc { ... }, proc { ... }]`):
2437+ # each element is a heap-allocated sp_Proc pointer. Spinel has no
2438+ # typed `proc_ptr_array` slot, so box via poly_array — sp_box_proc
2439+ # is called on each push, and the poly-builtin dispatch on `arr[i]`
2440+ # recovers the proc. Without this arm, the array's inferred type
2441+ # fell back to `int_array`, and `sp_IntArray_push` was called with
2442+ # a sp_Proc pointer — int-from-pointer C-compile error.
2443+ if et == "proc" || et == "lambda"
2444+ @needs_gc = 1
2445+ @needs_rb_value = 1
2446+ return "poly_array"
2447+ end
24362448 # Check if elements have mixed types
24372449 k = 1
24382450 while k < elems.length