www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19532] New: chunkBy assert error involving merge and

https://issues.dlang.org/show_bug.cgi?id=19532

          Issue ID: 19532
           Summary: chunkBy assert error involving merge and reference
                    ranges.
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: jrdemail2000-dlang yahoo.com

std.algorithm.chunkBy will trigger an assertion error in std.algorithm.merge
when merge is used with reference ranges. The failure is due to a call to front
on an empty range, the empty range being the implementation merge. Tested  with
DMD 2.083.

-------- Sample program: case1.d --------
import std.algorithm : merge;
import std.stdio;
import std.range;

class InputRangeClass(R)
{
    R data;
    this(R _data) pure  safe nothrow
    {
        data = _data;
    }
     property bool empty() pure  safe nothrow
    {
        return data.empty;
    }
     property auto front() pure  safe nothrow
    {
        return data.front;
    }
    void popFront() pure  safe nothrow
    {
        data.popFront();
    }
}

auto inputRangeClass(R)(R range)
{
    return new InputRangeClass!R(range);
}

struct InputRangeStruct(R)
{
    R data;
    this(R _data) pure  safe nothrow
    {
        data = _data;
    }
     property bool empty() pure  safe nothrow
    {
        return data.empty;
    }
     property auto front() pure  safe nothrow
    {
        return data.front;
    }
    void popFront() pure  safe nothrow
    {
        data.popFront();
    }
}

auto inputRangeStruct(R)(R range)
{
    return InputRangeStruct!R(range);
}

void main(string[] args)
{
    import std.algorithm : chunkBy, fold, map, merge;

    auto data1 = [2, 3, 5];
    auto data2 = [2, 4, 5];
    auto data3 = [1, 2, 4, 5];

    /* This succeeds. */
    auto x = merge(data1, data2, data3);
    writeln(x.chunkBy!((a, b) => (a == b)));

    auto y1 = data1.inputRangeStruct;
    auto y2 = data2.inputRangeStruct;
    auto y3 = data3.inputRangeStruct;

    /* A value range. This suceeds. */
    auto y = merge(y1, y2, y3);
    writeln(y.chunkBy!((a, b) => (a == b)));

    auto z1 = data1.inputRangeClass;
    auto z2 = data2.inputRangeClass;
    auto z3 = data3.inputRangeClass;

    /* A reference range. This fails. */
    auto z = merge(z1, z2, z3);
    writeln(z.chunkBy!((a, b) => (a == b)));
}
---------------------------------

The run:

$ dmd case1.d
$ ./case1
[[1], [2, 2, 2], [3], [4, 4], [5, 5, 5]]
[[1], [2, 2, 2], [3], [4, 4], [5, 5, 5]]
core.exception.AssertError /Library/D/dmd/src/phobos/std/algorithm/sorting.d(1123):
Assertion failure
----------------
??:? _d_assertp [0x220b121]
??:? pure nothrow  property  safe int std.algorithm.sorting.Merge!("a < b",
case1.InputRangeClass!(int[]).InputRangeClass,
case1.InputRangeClass!(int[]).InputRangeClass,
case1.InputRangeClass!(int[]).InputRangeClass).Merge.front() [0x21fd636]

... many more lines of stack trace ...

??:? _Dmain [0x21ec767]
[[1], [2, 2, 2], [4], [5]

--
Dec 31 2018