Commit e294590a
e294590a0b6eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Recognize inject(&:op) symbol-to-proc for the arithmetic fold
[1,2,3].inject(&:+) emitted an empty accumulation loop with an
undeclared accumulator: reduce_sym_op_for only inspected positional
arguments, so the operator symbol passed as a &:+ block (a
BlockArgumentNode wrapping the SymbolNode) was missed and the call fell
through to a stub path. Detect the block form too, reusing the existing
arithmetic-fold lowering. The positional inject(:+) / inject(init, :op)
forms are unchanged.
The non-arithmetic method-dispatch form over object arrays
([[1,2],[2,3]].inject(&:&)) is a separate, larger mechanism and remains.
Refs #1317
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- e294590a0b6e
- Parent
- f35e0d36da11
spinel_codegen.rbmodifiedView file
# (the dispatch method is large and adding scope tripped # self-host scan_locals; see feedback_self_host_recursive_fn_arm). def reduce_sym_op_for(nid) # `reduce(:+)` -- the operator symbol as a positional argument. args_id = @nd_arguments[nid] if args_id < 0 return "" end args = get_args(args_id) if args.length != 1 return "" if args_id >= 0 args = get_args(args_id) if args.length == 1 && @nd_type[args[0]] == "SymbolNode" op_a = @nd_content[args[0]] if reduce_arith_op?(op_a) == 1 return op_a end end end if @nd_type[args[0]] != "SymbolNode" return "" # `inject(&:+)` -- the same operator as a symbol-to-proc block. The symbol # is the inner expression of the BlockArgumentNode, not a positional arg. blk = @nd_block[nid] if blk >= 0 && @nd_type[blk] == "BlockArgumentNode" inner = @nd_expression[blk] if inner >= 0 && @nd_type[inner] == "SymbolNode" op_b = @nd_content[inner] if reduce_arith_op?(op_b) == 1 return op_b end end end op = @nd_content[args[0]] "" end def reduce_arith_op?(op) if op == "+" || op == "*" || op == "-" || op == "/" || op == "%" || op == "&" || op == "|" || op == "^" return op return 1 end "" 0 end # 1 if `mname` is a Math module function taking exactly onetest/inject_symbol_op.rbaddedView file
Loading diff…
test/inject_symbol_op.rb.expectedaddedView file
Loading diff…