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 (intfor int_array,floatfor 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
if args_id >= 0 aargs = get_args(args_id) if aargs.length > 0 # `arr.reduce(:op)` / `inject(:op)` (binary-op symbol form): # the result is the array's element type, not the symbol type. # Issue #506: without this arm, `[1,2,3,4].reduce(:*)` typed as # `symbol` and `p`-inspected as `:<sym-name-of-24>` instead of # printing `24`. if aargs.length == 1 && @nd_type[aargs[0]] == "SymbolNode" sop = @nd_content[aargs[0]] if sop == "+" || sop == "*" || sop == "-" || sop == "/" || sop == "%" || sop == "&" || sop == "|" || sop == "^" if recv >= 0 rt_inj = infer_type(recv) if rt_inj == "int_array" return "int" end if rt_inj == "float_array" return "float" end end return "int" end end return infer_type(aargs[0]) end endspinel_codegen.rbmodifiedView file
Loading diff…
test/reduce_sym_op.rbaddedView file
Loading diff…
test/reduce_sym_op.rb.expectedaddedView file
Loading diff…