Repository navigation
reduce(0, :|) over an Array with a Bignum folds boxed - #10183
Conversation
`[28, 2**64, "x"]` with its last element popped answered RangeError for `reduce(0, :^)`, where CRuby answers 18446744073709551644. ## Background reduce(init, :op) with no block types its result from the seed. For an Integer seed, two rules make the result boxed: - the elements of a poly Array have an erased type - a literal Array holds a Bignum, a Rational, a Complex or a boxed value Both rules apply only to an arithmetic operator. For `&`, `|` and `^`, the result stayed an sp_int. The fold itself runs boxed, so the unbox at its end raised on a Bignum: ```ruby big = 2**64 pa = [28, big, "x"] pa.pop p pa.reduce(0, :^) # RangeError, CRuby 18446744073709551644 p [28, big].reduce(0, :|) # RangeError, CRuby 18446744073709551644 ``` ## Fix The two rules now take `&`, `|` and `^` too (is_bit_op). The result of such a fold is boxed. A fold whose elements have a known type does not change, so `[3, 5].reduce(0, :|)` stays an sp_int. test/reduce_seed_bitop_bignum.rb gives the CRuby answers under raise and promote. It also keeps a fold of machine words and the TypeError for a String element. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014j1eT4mbnjKDWvHdDRpp1T
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Boxed-result inference and regression coverage src/analyze_infer_recv.c, test/reduce_seed_bitop_bignum.rb, test/reduce_seed_bitop_bignum.rb.expected |
Bitwise folds now use boxed-result handling for literal poly arrays containing boxed numeric elements and for poly arrays with erased element types. The regression cases cover Bignum values, machine-word values, and the error from a remaining non-Integer element. |
Priority:
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix
Possibly related PRs
- matz/spinel#10175: Changes boxed Integer bitwise operations with Bignum operands, which are used by the seeded reductions covered here.
Merge Risk: 
Minimal · up to ae8e3
-
The change is mergeable; the modified inference paths and regression expectations are consistent.
Pre-merge checks: 
5
-
Full details: Title check
-
Explanation
The title names the primary change: reduce(0, :|) over an Array containing a Bignum now folds boxed. The raw summary and objectives confirm the change extends boxing rules to bitwise operators &, |, and ^ for reduce(init, :op) with an Integer seed. The title uses one specific example operator, so it is somewhat narrower than the full change, but it still refers to the main point and is not misleading.
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 unsupported.)
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
[28, 2**64, "x"]with its last element popped answered RangeError forreduce(0, :^), where CRuby answers 18446744073709551644.Background
reduce(init, :op) with no block types its result from the seed. For an Integer seed, two rules make the result boxed:
Both rules apply only to an arithmetic operator. For
&,|and^, the result stayed an sp_int. The fold itself runs boxed, so the unbox at its end raised on a Bignum:Fix
The two rules now take
&,|and^too (is_bit_op). The result of such a fold is boxed. A fold whose elements have a known type does not change, so[3, 5].reduce(0, :|)stays an sp_int.test/reduce_seed_bitop_bignum.rb gives the CRuby answers under raise and promote. It also keeps a fold of machine words and the TypeError for a String element.
make gate(on this branch merged with current master).expectedfiles that match CRuby 4.0 run with--enable-frozen-string-literal# spinel: int64Summary by CodeRabbit
Bug Fixes
&,|, and^reductions with an initial value when arrays contain large integers.Tests