Commit 12f20519

12f205197ae4Merged

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

Description

Fold inject/reduce with a symbol-to-proc and seed seedless folds correctly

inject(&:sym) / reduce(&:sym) on a non-int_array receiver reached compile_reduce_block with a synthetic accumulator _acc that was never declared, failing the C build with 'lv__acc' undeclared. The seedless fold path was also wrong independently of the symbol form: it seeded the accumulator with 0 (NULL for pointer elements) and iterated from index 0, so [1,2,3,4].inject { |a,b| a*b } returned 0 instead of 24 and an array-of-arrays fold inject { |a,b| a & b } dereferenced a NULL accumulator and crashed.

Seed seedless folds with the first element and iterate from index 1 (empty array yields the element-type default, matching the int_array sym fast path); type the seedless reduce/inject result as the element type in analyze so the caller dispatches on the real value; declare the synthetic _acc via the fresh-decl block path; and lower &:sym / :sym to acc.sym(x) for the modeled element/op pairs (typed-array set ops & | -, scalar arithmetic, string concat), warning on any combination not modeled rather than emitting wrong C.

Fixes #1317

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

Details

Commit
12f205197ae4
Parent
9b184d3945d3

spinel_analyze.rbmodifiedView file

@@ -6670,6 +6670,15 @@
66706670 return infer_type(aargs[0])
66716671 end
66726672 end
6673+ # Seedless reduce/inject (explicit block or `&:sym`, no positional
6674+ # seed): the accumulator starts as the first element and a
6675+ # homogeneous fold preserves that type, so the call's result is the
6676+ # element type, not the scalar-int default. Without this an
6677+ # int_array-valued fold like `[[..],[..]].inject { |a,b| a & b }`
6678+ # typed as int and the int_array result was dispatched as a scalar.
6679+ if (mname == "reduce" || mname == "inject") && recv >= 0
6680+ return elem_type_of_array(infer_type(recv))
6681+ end
66736682 return "int"
66746683 end
66756684 if mname == "[]" || (mname == "at" && recv >= 0 && is_array_type(infer_type(recv)) == 1)