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

@@ -27990,22 +27990,37 @@
2799027990 # (the dispatch method is large and adding scope tripped
2799127991 # self-host scan_locals; see feedback_self_host_recursive_fn_arm).
2799227992 def reduce_sym_op_for(nid)
27993+ # `reduce(:+)` -- the operator symbol as a positional argument.
2799327994 args_id = @nd_arguments[nid]
27994- if args_id < 0
27995- return ""
27996- end
27997- args = get_args(args_id)
27998- if args.length != 1
27999- return ""
27995+ if args_id >= 0
27996+ args = get_args(args_id)
27997+ if args.length == 1 && @nd_type[args[0]] == "SymbolNode"
27998+ op_a = @nd_content[args[0]]
27999+ if reduce_arith_op?(op_a) == 1
28000+ return op_a
28001+ end
28002+ end
2800028003 end
28001- if @nd_type[args[0]] != "SymbolNode"
28002- return ""
28004+ # `inject(&:+)` -- the same operator as a symbol-to-proc block. The symbol
28005+ # is the inner expression of the BlockArgumentNode, not a positional arg.
28006+ blk = @nd_block[nid]
28007+ if blk >= 0 && @nd_type[blk] == "BlockArgumentNode"
28008+ inner = @nd_expression[blk]
28009+ if inner >= 0 && @nd_type[inner] == "SymbolNode"
28010+ op_b = @nd_content[inner]
28011+ if reduce_arith_op?(op_b) == 1
28012+ return op_b
28013+ end
28014+ end
2800328015 end
28004- op = @nd_content[args[0]]
28016+ ""
28017+ end
28018+
28019+ def reduce_arith_op?(op)
2800528020 if op == "+" || op == "*" || op == "-" || op == "/" || op == "%" || op == "&" || op == "|" || op == "^"
28006- return op
28021+ return 1
2800728022 end
28008- ""
28023+ 0
2800928024 end
2801028025
2801128026 # 1 if `mname` is a Math module function taking exactly one