Commit 8b564e01

8b564e01c786Merged

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

Description

fix(analyze,codegen): reduce(:op) / inject(:op) on int_array

Closes #506. [1,2,3,4].reduce(:*) printed : instead of 24: the codegen had no handler for the symbol-arg form (only the block form was supported), so the call fell through to the "unresolved on int_array" warning and emitted 0. Analyze inferred the return type from the first arg (:* -> symbol), so the surrounding p then inspected the (truncated) "symbol" result as :<name> instead of formatting an integer.

Two coordinated changes:

  • analyze: in the reduce / inject / each_with_object arm of infer_call_type, recognise reduce(:op) with an inlinable binary arithmetic / bitwise op and return the array's element type (int for int_array, float for float_array) instead of the symbol arg's type.

  • codegen: in the int_array dispatch, fold the symbol-arg form inline:

    ({ sp_IntArray *_arr = rc; mrb_int _acc = _arr->len > 0 ? sp_IntArray_get(_arr, 0) : 0; for (mrb_int _i = 1; _i < _arr->len; _i++) _acc = _acc sp_IntArray_get(_arr, _i); _acc; })

    Supported ops: + * - / % & | ^. Pulled the op-name probe into reduce_sym_op_for(nid) since adding a fresh local to the (very large) array-dispatch emit method tripped self-host scan_locals (see feedback_self_host_recursive_fn_arm).

Empty-array case returns 0 rather than nil (int_array can't carry nil). The block form continues to route through compile_reduce_expr at the generic poly entry.

Local: 474 tests pass.

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

Details

Commit
8b564e01c786
Parent
d7cd05e37b34

spinel_analyze.rbmodifiedView file

@@ -4549,6 +4549,26 @@
45494549 if args_id >= 0
45504550 aargs = get_args(args_id)
45514551 if aargs.length > 0
4552+ # `arr.reduce(:op)` / `inject(:op)` (binary-op symbol form):
4553+ # the result is the array's element type, not the symbol type.
4554+ # Issue #506: without this arm, `[1,2,3,4].reduce(:*)` typed as
4555+ # `symbol` and `p`-inspected as `:<sym-name-of-24>` instead of
4556+ # printing `24`.
4557+ if aargs.length == 1 && @nd_type[aargs[0]] == "SymbolNode"
4558+ sop = @nd_content[aargs[0]]
4559+ if sop == "+" || sop == "*" || sop == "-" || sop == "/" || sop == "%" || sop == "&" || sop == "|" || sop == "^"
4560+ if recv >= 0
4561+ rt_inj = infer_type(recv)
4562+ if rt_inj == "int_array"
4563+ return "int"
4564+ end
4565+ if rt_inj == "float_array"
4566+ return "float"
4567+ end
4568+ end
4569+ return "int"
4570+ end
4571+ end
45524572 return infer_type(aargs[0])
45534573 end
45544574 end