Skip to content

Commit

Permalink
[GR-19220] Ruby 3.2 Fix Array#[] with ArithmeticSequence argument
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/3749
  • Loading branch information
eregon committed Apr 19, 2023
2 parents 643aa8d + f84bb0d commit d24019b
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Compatibility:
* Make `Array#shuffle` produce the same results as CRuby (@rwstauner).
* Add `Process.argv0` method (@andrykonchin).
* Add support for array pattern matching. This is opt-in via `--pattern-matching` since pattern matching is not fully supported yet. (#2683, @razetime).
* Fix `Array#[]` with `ArithmeticSequence` argument when step is negative (@itarato).

Performance:

Expand Down
6 changes: 0 additions & 6 deletions spec/tags/core/array/element_reference_tags.txt

This file was deleted.

3 changes: 3 additions & 0 deletions spec/truffleruby.next-specs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
# Use spec/ruby/core/nil/nil_spec.rb as a dummy file to avoid being empty (what causes mspec to error)
spec/ruby/core/nil/nil_spec.rb

spec/ruby/core/array/slice_spec.rb
spec/ruby/core/array/element_reference_spec.rb

spec/ruby/core/hash/shift_spec.rb
6 changes: 3 additions & 3 deletions src/main/java/org/truffleruby/core/array/ArrayNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ protected Object indexRange(RubyArray array, Object range, NotProvided length,
return readSliceNode.executeReadSlice(array, startLength[0], len);
}

@Specialization(guards = "isArithmeticSequence(index, isANode)", limit = "1")
protected Object indexArithmeticSequence(RubyArray array, Object index, NotProvided length,
@Specialization(guards = "isArithmeticSequence(enumerator, isANode)", limit = "1")
protected Object indexArithmeticSequence(RubyArray array, Object enumerator, NotProvided length,
@Cached @Shared IsANode isANode,
@Cached DispatchNode callSliceArithmeticSequence) {
return callSliceArithmeticSequence.call(array, "slice_arithmetic_sequence", index);
return callSliceArithmeticSequence.call(array, "slice_arithmetic_sequence", enumerator);
}

@Specialization(
Expand Down
11 changes: 6 additions & 5 deletions src/main/ruby/truffleruby/core/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ def ==(other)
# negative indexes refer to the end of array
start += len if start < 0
stop += len if stop < 0

stop += 1 unless seq.exclude_end?
diff = stop - start
diff = stop - start + (seq.exclude_end? ? 0 : 1)

is_out_of_bound = start < 0 || start > len

Expand All @@ -184,12 +182,15 @@ def ==(other)
return self[start, diff] if step == 1 # step == 1 is a simple slice

# optimize when no step will be done and only start element is returned
return self[start, 1] if (step > 0 && step > diff) || (step < 0 && step < -diff)
return self[start, 1] if (step > 0 && step > diff)
return self[stop, 1] if (step < 0 && step < -diff)

ustep = step.abs
nlen = (diff + ustep - 1) / ustep
i = 0
j = start + (step > 0 ? 0 : diff - 1) # because we inverted negative step ranges
j = start
j += diff - (seq.exclude_end? ? 0 : 1) if step < 0 # because we inverted negative step ranges

res = Array.new(nlen)

while i < nlen
Expand Down
1 change: 0 additions & 1 deletion test/mri/excludes/TestArray.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
exclude :test_slice_out_of_range, "needs investigation"
exclude :test_sort_uncomparable, "needs investigation"
exclude :test_uniq, "needs investigation"
exclude :test_slice, "needs investigation"
exclude :test_freeze_inside_sort!, "needs investigation"
exclude :test_intersect_big_array, "needs investigation"
exclude :test_intersect?, "needs investigation"
Expand Down

0 comments on commit d24019b

Please sign in to comment.