Skip to content

Raise IndexError for Struct#[] and #[]= with an index past int (Feature #22400) - #10116

Open
zev wants to merge 1 commit into
jruby:ruby-4.1from
zev:struct-big-index-error
Open

zev wants to merge 1 commit into
jruby:ruby-4.1from
zev:struct-big-index-error

Conversation

@zev

@zev zev commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Struct#[] and Struct#[]= now raise IndexError for an Integer index past the int range, as in Ruby 4.1 (Feature #22400, NEWS: "Compatibility issues"). JRuby converted the index to an int, so it raised RangeError instead. This item is not on the #9746 checklist yet.

CRuby change: ruby/ruby@c1191af559 (invalid_struct_pos in struct.c now uses NUM2LEN instead of FIX2INT).

With s = Struct.new(:a).new(1):

Call CRuby 4.1 JRuby before JRuby after
s[2**40] IndexError: offset 1099511627776 too large for struct(size:1) RangeError: integer 1099511627776 too big to convert to 'int' same as CRuby
s[-2**40] = 2 IndexError: offset -1099511627776 too small for struct(size:1) RangeError: integer -1099511627776 too small to convert to 'int' same as CRuby
s[2.0**40] IndexError: offset 1099511627776 too large ... RangeError: ... to 'int' same as CRuby
s.values_at(2**40) IndexError: offset 1099511627776 too large ... RangeError: ... to 'int' same as CRuby
s[2**64] RangeError: bignum too big to convert into 'long' same same

Changes

  • RubyStruct: #[], #[]= and #values_at convert an Integer index with toLong instead of toInt. The bounds check and the IndexError message use the long value.
  • values_at changes because CRuby's struct_entry calls rb_struct_aref, so it gets the new error too.
  • Added @CRuby annotations to aref (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.rb and element_set_spec.rb:

  • ruby_version_is "4.1": 2**40 and -2**40 raise IndexError with the CRuby message. It is guarded with platform_is c_long_size: 64, because CRuby's index type (rb_len_t) is a 32-bit long on 32-bit platforms and on mingw.
  • ruby_version_is ""..."4.1": the same calls raise RangeError.

Testing

  • The two spec files pass on JRuby (interpreter and forced JIT), CRuby 4.1.0-dev and CRuby 4.0.7. On the base branch, the two new 4.1 examples error.
  • spec/ruby/core/struct (mspec ci): 0 failures, as before.
  • test/mri/ruby/test_struct.rb: 85 tests, 0 failures, as before.
  • A script with 14 indexes (Fixnum, Bignum and Float, both signs, inside and outside int and long) for #[], #[]= and values_at gives the same output as CRuby 4.1.0-dev.

Not in this PR

  • Struct#dig with an out-of-range Integer index raises IndexError in JRuby, but CRuby returns nil (s.dig(5)). This is older than 4.1. With this change, s.dig(2**40) raises IndexError instead of RangeError, but CRuby returns nil for both.
  • s[nil] says no implicit conversion from nil to integer. CRuby says no implicit conversion of nil into Integer. This message comes from Convert.toLong/toInt, which are shared.
  • CRuby's test_struct.rb adds asserts that use RbConfig::LIMITS["FIXNUM_MAX"]. JRuby doesn't define RbConfig::LIMITS, so those asserts will error when test_struct.rb is next synced.

🤖 Generated with Claude Code

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
zev marked this pull request as ready for review November 18, 2026 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant