Repository navigation
Conversation
Ruby 4.1 (Feature #22400, ruby/ruby@c1191af559) converts an Integer Struct index with NUM2LEN instead of FIX2INT, so an out-of-range index that does not fit in an int raises IndexError ("offset ... too large for struct") instead of RangeError. Only an index that does not fit in a long still raises RangeError. Convert the index with toLong in #[], #[]= and #values_at, and report the long offset in the IndexError message. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
zev
marked this pull request as ready for review
November 18, 2026 20:34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Struct#[]andStruct#[]=now raiseIndexErrorfor an Integer index past theintrange, as in Ruby 4.1 (Feature #22400, NEWS: "Compatibility issues"). JRuby converted the index to anint, so it raisedRangeErrorinstead. This item is not on the #9746 checklist yet.CRuby change: ruby/ruby@c1191af559 (
invalid_struct_posinstruct.cnow usesNUM2LENinstead ofFIX2INT).With
s = Struct.new(:a).new(1):s[2**40]IndexError: offset 1099511627776 too large for struct(size:1)RangeError: integer 1099511627776 too big to convert to 'int's[-2**40] = 2IndexError: offset -1099511627776 too small for struct(size:1)RangeError: integer -1099511627776 too small to convert to 'int's[2.0**40]IndexError: offset 1099511627776 too large ...RangeError: ... to 'int's.values_at(2**40)IndexError: offset 1099511627776 too large ...RangeError: ... to 'int's[2**64]RangeError: bignum too big to convert into 'long'Changes
RubyStruct:#[],#[]=and#values_atconvert an Integer index withtoLonginstead oftoInt. The bounds check and theIndexErrormessage use thelongvalue.values_atchanges because CRuby'sstruct_entrycallsrb_struct_aref, so it gets the new error too.@CRubyannotations toaref(rb_struct_aref),aset(rb_struct_aset) and the bounds check (invalid_struct_pos, Integer branch).Specs
No spec existed in ruby/spec or CRuby, so I added one example each to
spec/ruby/core/struct/element_reference_spec.rbandelement_set_spec.rb:ruby_version_is "4.1":2**40and-2**40raiseIndexErrorwith the CRuby message. It is guarded withplatform_is c_long_size: 64, because CRuby's index type (rb_len_t) is a 32-bitlongon 32-bit platforms and on mingw.ruby_version_is ""..."4.1": the same calls raiseRangeError.Testing
spec/ruby/core/struct(mspec ci): 0 failures, as before.test/mri/ruby/test_struct.rb: 85 tests, 0 failures, as before.intandlong) for#[],#[]=andvalues_atgives the same output as CRuby 4.1.0-dev.Not in this PR
Struct#digwith an out-of-range Integer index raisesIndexErrorin JRuby, but CRuby returnsnil(s.dig(5)). This is older than 4.1. With this change,s.dig(2**40)raisesIndexErrorinstead ofRangeError, but CRuby returnsnilfor both.s[nil]saysno implicit conversion from nil to integer. CRuby saysno implicit conversion of nil into Integer. This message comes fromConvert.toLong/toInt, which are shared.test_struct.rbadds asserts that useRbConfig::LIMITS["FIXNUM_MAX"]. JRuby doesn't defineRbConfig::LIMITS, so those asserts will error whentest_struct.rbis next synced.🤖 Generated with Claude Code