Commit e987ab57

e987ab57f8e8Merged

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

Description

analyze,codegen: Numeric#coerce returns a typed-array pair

1.coerce(2.5) SEGV'd: the call dropped into the unresolved-call int path (emitting 0), and the a, b = ... destructure then dereferenced that int as a pointer.

CRuby's contract: recv.coerce(other) returns [other_cast, recv_cast] where both are widened to Float when either operand is Float, else both stay Integer. Materialise that pair directly into the matching typed array so the existing multi-write destructure handles it without any new plumbing.

  • analyze: infer_call_type returns float_array when either operand is float, int_array when both are int (recv is int/float).
  • codegen: arm in compile_int_method_expr and compile_float_method_expr pushes the two coerced operands into a fresh FloatArray or IntArray.

Note: assigning the destructure to the same locals across calls with different operand types still widens those locals via spinel's body-usage type inference — that's the existing typed-var policy, not a coerce-specific bug.

Fixes #961

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
e987ab57f8e8
Parent
d0938bda3d92

spinel_analyze.rbmodifiedView file

@@ -6259,6 +6259,27 @@
62596259 end
62606260 # `(a..b).step(n)` (no block) -- returns an int_array of stepped
62616261 # values. Issue #731.
6262+ if mname == "coerce"
6263+ if recv >= 0
6264+ rt_co = infer_type(recv)
6265+ if rt_co == "int" || rt_co == "float"
6266+ arg_t_co = ""
6267+ args_id_co = @nd_arguments[nid]
6268+ if args_id_co >= 0
6269+ aa_co = get_args(args_id_co)
6270+ if aa_co.length > 0
6271+ arg_t_co = infer_type(aa_co[0])
6272+ end
6273+ end
6274+ if rt_co == "float" || arg_t_co == "float"
6275+ return "float_array"
6276+ end
6277+ if arg_t_co == "int"
6278+ return "int_array"
6279+ end
6280+ end
6281+ end
6282+ end
62626283 if mname == "step"
62636284 if recv >= 0
62646285 rt_step = infer_type(recv)