RangeCheck error with empty slice: arr[1..0]
Original Reporter info from Mantis: hirt
-
Reporter name: Martin Hirt
Original Reporter info from Mantis: hirt
- Reporter name: Martin Hirt
Description:
For an array arr and a procedure proc, one can invoke proc(arr[3..5]) on a slice of arr. This works perfectly, but invoking proc with an empty slice at times causes a range check error (if enabled). For example, when arr is an array of length 1, then proc(arr[0..-1]) invokes proc with an empty array, whereas proc(arr[1..0]) raises a range check error. The latter is often used in recursive procedures, when the first element arr[0] is consumed, and recursion is invoked with the remaining elements arr[1..High(arr)].
Note that without RangeChecks, the code runs correctly.
Steps to reproduce:
See attached minimal working example. The Right-to-Left-Recursion (PROCEDURE RL) works without problem. However, the more natural Left-to-Right-Recursion (PROCEDURE LR) raises a RangeCheck error.
The problem occurs in standard implementation of recursive algorithms like QuickSort, MergeSort, etc.
The bug can be circumvented with an additional if:
IF Length(arr) > 0 THEN
BEGIN
consume(arr[0]);
IF Length(arr) > 1 THEN Recurse(arr[1..High(arr)];
END;
But this is definitely not elegant.
Additional information:
Suggested fix: Slices of length 0 (i.e., arr[i..j] where j+1-i = 0) should be considered as empty slice, no matter how big arr is.
Mantis conversion info:
- Mantis ID: 25776
- OS: Linux
- OS Build: Red Hat EL 6.5
- Build: 2013/02/16
- Platform: PC
- Version: 2.6.2
- Monitored by: » hirt (Martin Hirt)