Skip to content

Commit d24019b

Browse files
committed
[GR-19220] Ruby 3.2 Fix Array#[] with ArithmeticSequence argument
PullRequest: truffleruby/3749
2 parents 643aa8d + f84bb0d commit d24019b

File tree

6 files changed

+13
-15
lines changed

6 files changed

+13
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Compatibility:
1212
* Make `Array#shuffle` produce the same results as CRuby (@rwstauner).
1313
* Add `Process.argv0` method (@andrykonchin).
1414
* Add support for array pattern matching. This is opt-in via `--pattern-matching` since pattern matching is not fully supported yet. (#2683, @razetime).
15+
* Fix `Array#[]` with `ArithmeticSequence` argument when step is negative (@itarato).
1516

1617
Performance:
1718

spec/tags/core/array/element_reference_tags.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/truffleruby.next-specs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99
# Use spec/ruby/core/nil/nil_spec.rb as a dummy file to avoid being empty (what causes mspec to error)
1010
spec/ruby/core/nil/nil_spec.rb
1111

12+
spec/ruby/core/array/slice_spec.rb
13+
spec/ruby/core/array/element_reference_spec.rb
14+
1215
spec/ruby/core/hash/shift_spec.rb

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ protected Object indexRange(RubyArray array, Object range, NotProvided length,
285285
return readSliceNode.executeReadSlice(array, startLength[0], len);
286286
}
287287

288-
@Specialization(guards = "isArithmeticSequence(index, isANode)", limit = "1")
289-
protected Object indexArithmeticSequence(RubyArray array, Object index, NotProvided length,
288+
@Specialization(guards = "isArithmeticSequence(enumerator, isANode)", limit = "1")
289+
protected Object indexArithmeticSequence(RubyArray array, Object enumerator, NotProvided length,
290290
@Cached @Shared IsANode isANode,
291291
@Cached DispatchNode callSliceArithmeticSequence) {
292-
return callSliceArithmeticSequence.call(array, "slice_arithmetic_sequence", index);
292+
return callSliceArithmeticSequence.call(array, "slice_arithmetic_sequence", enumerator);
293293
}
294294

295295
@Specialization(

src/main/ruby/truffleruby/core/array.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,7 @@ def ==(other)
165165
# negative indexes refer to the end of array
166166
start += len if start < 0
167167
stop += len if stop < 0
168-
169-
stop += 1 unless seq.exclude_end?
170-
diff = stop - start
168+
diff = stop - start + (seq.exclude_end? ? 0 : 1)
171169

172170
is_out_of_bound = start < 0 || start > len
173171

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

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

189188
ustep = step.abs
190189
nlen = (diff + ustep - 1) / ustep
191190
i = 0
192-
j = start + (step > 0 ? 0 : diff - 1) # because we inverted negative step ranges
191+
j = start
192+
j += diff - (seq.exclude_end? ? 0 : 1) if step < 0 # because we inverted negative step ranges
193+
193194
res = Array.new(nlen)
194195

195196
while i < nlen

test/mri/excludes/TestArray.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
exclude :test_slice_out_of_range, "needs investigation"
4747
exclude :test_sort_uncomparable, "needs investigation"
4848
exclude :test_uniq, "needs investigation"
49-
exclude :test_slice, "needs investigation"
5049
exclude :test_freeze_inside_sort!, "needs investigation"
5150
exclude :test_intersect_big_array, "needs investigation"
5251
exclude :test_intersect?, "needs investigation"

0 commit comments

Comments
 (0)