arrayssortingdphobos

std.algorithm.sorting fails with obscure errors when using Array!T and opSlice()


I'm sure I'm missing something obvious here - the rest of D (even the compiler errors) have been very sensible and easy to understand. I have a std.containers.Array of comparable structs and I'd like to sort it. the std.containers documentation notes that in order to use the stuff in std.algorithm you need to slice it, either with array[] or array.opSlice(). Okay, no problem.

However, if I make an Array of two pretty trivial types, it won't sort - instead, it tells me that a routine deep within Phobos is not nothrow (?)

B:\lib\D\dmd2\windows\bin\..\..\src\phobos\std\range\package.d(7189): Error: 'std.range.SortedRange!(RangeT!(Array!(MyInt)), "a < b").SortedRange.dbgVerifySorted' is not nothrow
B:\lib\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\sorting.d(982): Error: template instance std.range.assumeSorted!("a < b", RangeT!(Array!(MyInt))) error instantiating
main.d(21):        instantiated from here: sort!("a < b", cast(SwapStrategy)0, RangeT!(Array!(MyInt))) 

Minimum example below. The first sort (an auto-generated standard array of two values) sorts fine. The other sort calls fail with the above compiler errors. Building with DMD2 from VS Community 2015, I can't find a compiler version identifier but this was downloaded only yesterday.

import std.array;
import std.container.array;
import std.algorithm.sorting;

struct MyInt
{
    int data;

    int opCmp(MyInt o)
    {
        return data - o.data;
    }
}

int main(string[] argv)
{
    MyInt ami, bmi;
    Array!MyInt arr = [ ami, bmi ];

    sort([ami, bmi]);
    sort(arr[0..2]);
    sort(arr[]);
    sort(arr.opSlice());

    return 0;
}

Solution

  • It's the bug in Phobos: Issue #14981

    It was fixed like a month ago, but changes haven't made it to a release yet. Probably will be available in 2.069.

    As a workaround for now you can build your project in release mode.