D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5786 - std.algorithm.sort does not work with std.container.Array: Range violation
Summary: std.algorithm.sort does not work with std.container.Array: Range violation
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: Andrei Alexandrescu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-27 01:16 UTC by Jonathan M Davis
Modified: 2012-04-23 18:23 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jonathan M Davis 2011-03-27 01:16:18 UTC
This program

import std.algorithm;
import std.container;

void main()
{
    Array!int arr;
    arr.insert([1, 7, 2, 8, 3]);
    sort(arr[]);
}

throws this AssertError when it runs:

core.exception.RangeError@std.container(1709): Range violation
----------------
./test(onRangeError+0x28) [0x80b3ec8]
./test(_d_array_bounds+0x16) [0x80acc96]
./test(_D3std9container7__arrayZ+0x12) [0x80aec26]
./test(_D3std9container12__T5ArrayTiZ5Array5Range6moveAtMFkZi+0x95) [0x80a33a1]
./test(_D3std5range53__T6moveAtTS3std9container12__T5ArrayTiZ5Array5RangeZ6moveAtFS3std9container12__T5ArrayTiZ5Array5RangekZi+0x12) [0x80a8f06]
./test(_D3std9algorithm53__T6swapAtTS3std9container12__T5ArrayTiZ5Array5RangeZ6swapAtFS3std9container12__T5ArrayTiZ5Array5RangekkZv+0x3e) [0x80a8e9e]
./test(_D3std9algorithm168__T8sortImplS793std10functional54__T13binaryFunImplVAyaa5_61203c2062VAyaa1_61VAyaa1_62Z6resultVE3std9algorithm12SwapStrategy0TS3std9container12__T5ArrayTiZ5Array5RangeZ8sortImplFS3std9container12__T5ArrayTiZ5Array5RangeZv+0x61) [0x80a8ca9]
./test(_D3std9algorithm99__T4sortVAyaa5_61203c2062VE3std9algorithm12SwapStrategy0TS3std9container12__T5ArrayTiZ5Array5RangeZ4sortFS3std9container12__T5ArrayTiZ5Array5RangeZS3std5range76__T11SortedRangeTS3std9container12__T5ArrayTiZ5Array5RangeVAyaa5_61203c2062Z11SortedRange+0x28) [0x80a8718]
./test(_Dmain+0x56) [0x80a2aaa]
./test(_D2rt6dmain24mainUiPPaZi7runMainMFZv+0x1a) [0x80acdf6]
./test(_D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv+0x20) [0x80acd88]
./test(_D2rt6dmain24mainUiPPaZi6runAllMFZv+0x32) [0x80ace3a]
./test(_D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv+0x20) [0x80acd88]
./test(main+0x94) [0x80acd34]
/usr/lib32/libc.so.6(__libc_start_main+0xe6) [0xf758bdb6]
./test() [0x80a29a1]
Comment 1 kennytm 2011-03-27 05:42:36 UTC
The function which emits the error is


        T moveAt(size_t i)
        {
            i += _a;
            enforce(i < _b && !empty);
            return move(_outer._data._payload[_a + i]);    // <---
        }


That line should really read


            return move(_outer._data._payload[i]);
Comment 2 Andrei Alexandrescu 2011-03-28 12:11:43 UTC
Thanks Jonathan for reporting and Kenny for finding this bug.
Comment 3 SomeDude 2012-04-23 03:23:26 UTC
Compiles and runs fine with 2.059 Win32.