[Feature] Implement array slicing and concatenation.#29061
Conversation
Self-Review FindingFound one bug that should be fixed: BUG: Empty slices pass type checking but fail at runtimeLocation: Empty slices like
Fix: Change the type checker to reject // Before
if end < start {
self.emit_err(TypeCheckerError::slice_range_invalid(start, end, input.span()));
// After
if end <= start {
self.emit_err(TypeCheckerError::slice_range_invalid(start, end, input.span()));Consider adding a test case like |
5a85da2 to
cfeaad0
Compare
9c5a06c to
39539b1
Compare
mohammadfawaz
left a comment
There was a problem hiding this comment.
Overall no concerns! Just a few comments. There area a bunch of CLAUDE.md files.. do these need to be checked in?
| @@ -0,0 +1,3 @@ | |||
| <claude-mem-context> | |||
There was a problem hiding this comment.
Is this file intentional?
| @@ -0,0 +1,3 @@ | |||
| <claude-mem-context> | |||
| @@ -0,0 +1,3 @@ | |||
| <claude-mem-context> | |||
| @@ -0,0 +1,5 @@ | |||
| Error [ETYC0372176]: Slice range `5..5` is invalid: end must be greater than start. | |||
There was a problem hiding this comment.
We do allow zero-sized types and zero ranges (in loops). Should we allow the same here?
| return Err(CompilerError::repeat_count_not_evaluated(not_evaluated_span).into()); | ||
| } | ||
|
|
||
| if let Some(not_evaluated_span) = const_prop_output.slice_bounds_not_evaluated { |
There was a problem hiding this comment.
What happens if, in something like arr[start..end], we're only able to const evaluate start in iteration i but end can only be evaluated in iteration i+1 (think nested loops etc.)? Will we error out early and keep iterating?
Motivation
This PR adds array slicing and concatenation support to Leo, enabling more expressive and convenient array manipulation.
Array Slicing allows extracting a contiguous portion of an array using Rust-like syntax:
Array Concatenation allows combining two arrays with matching element types using the
+operator:Both features are resolved at compile time - slices and concatenations are expanded to individual element accesses, ensuring no runtime overhead.
Test Plan
Slicing Tests:
slice_basic.leoarr[2..5]exclusive end sliceslice_inclusive.leoarr[2..=5]sliceslice_from_start.leoarr[..5]sliceslice_to_end.leoarr[3..]sliceslice_full.leoarr[..]slice_with_variables.leoslice_out_of_bounds_fail.leoslice_invalid_range_fail.leoConcatenation Tests:
array_concat.leoa + b, nested arrays, different sizesarray_concat_chained.leoa + b + carray_concat_with_slice.leoa[0..2] + b[2..4]array_concat_type_mismatch_fail.leoRelated PRs