Skip to content

Conversation

@fglock
Copy link
Owner

@fglock fglock commented Jan 28, 2026

Problem

Array literal slices containing range operators like ('a','b','c','d','e','f')[0..5] were returning only the first element instead of the full slice.

Root Cause

The handleArrowArrayDeref method was using single-element optimization for all single-element array literal indices, but range operators need to be evaluated in LIST context to expand to multiple indices.

Solution

  • Detect when an array literal slice contains a range operator in the indices
  • Route such cases through the slice method instead of single-element optimization
  • Target only true array literals (containing only StringNode/NumberNode elements) to avoid breaking existing functionality

Changes

  • Modified src/main/java/org/perlonjava/codegen/Dereference.java to detect range operators in array literal indices
  • Added logic to use slice method for array literals with range operators

Test Results

  • ✅ Array literal slices with ranges now work: ('a','b','c','d','e','f')[0..5] returns 'abcdef'
  • ✅ Tests 27 and 28 in op/array.t now pass
  • ✅ Hash.t test count preserved: 26942 tests (no regression)
  • ✅ All other array slice types continue to work correctly

Trade-offs

Expression slices like (@foo,@bar)[0..5] are not fixed by this change to avoid breaking hash.t functionality. Those would require a separate, more careful approach.

- Fix array literal slices like ('a','b','c','d','e','f')[0..5] to return full slice instead of first element
- Target only true array literals (containing only StringNode/NumberNode elements) to avoid breaking hash.t
- Tests 27 and 28 in op/array.t now pass
- Preserve hash.t test count (26942 tests)
@fglock fglock merged commit 975d14b into master Jan 28, 2026
2 checks passed
@fglock fglock deleted the test-array-fix branch January 28, 2026 10:14
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.

2 participants