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
return infer_type(aargs[0]) end end # Seedless reduce/inject (explicit block or `&:sym`, no positional # seed): the accumulator starts as the first element and a # homogeneous fold preserves that type, so the call's result is the # element type, not the scalar-int default. Without this an # int_array-valued fold like `[[..],[..]].inject { |a,b| a & b }` # typed as int and the int_array result was dispatched as a scalar. if (mname == "reduce" || mname == "inject") && recv >= 0 return elem_type_of_array(infer_type(recv)) end return "int" end if mname == "[]" || (mname == "at" && recv >= 0 && is_array_type(infer_type(recv)) == 1)spinel_codegen.rbmodifiedView file
Loading diff…
test/inject_symbol_to_proc.rbaddedView file
Loading diff…
test/inject_symbol_to_proc.rb.expectedaddedView file
Loading diff…