[Issue 12733] New: parallelism.amap incorrect assignment without initialization

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun May 11 10:33:59 PDT 2014


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

          Issue ID: 12733
           Summary: parallelism.amap incorrect assignment without
                    initialization
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: Phobos
          Assignee: nobody at puremagic.com
          Reporter: monarchdodra at gmail.com

This code from Rosettacode fails in 2.065:

//----
ulong[] decompose(ulong n) pure nothrow {
    typeof(return) result;
    for (ulong i = 2; n >= i * i; i++)
        for (; n % i == 0; n /= i)
            result ~= i;
    if (n != 1)
        result ~= n;
    return result;
}

void main() {
    import std.stdio, std.algorithm, std.parallelism, std.typecons;

    immutable ulong[] data = [
        2UL^^59-1, 2UL^^59-1, 2UL^^59-1, 112_272_537_195_293UL,
        115_284_584_522_153, 115_280_098_190_773,
        115_797_840_077_099, 112_582_718_962_171,
        112_272_537_095_293, 1_099_726_829_285_419];

    //auto factors = taskPool.amap!(n => tuple(decompose(n), n))(data);
    //static enum genPair = (ulong n) pure => tuple(decompose(n), n);
    static genPair(ulong n) pure { return tuple(decompose(n), n); }
    auto factors = taskPool.amap!genPair(data);

    auto pairs = factors.map!(p => tuple(p[0].reduce!min, p[1]));
    writeln("N. with largest min factor: ", pairs.reduce!max[1]);
}
//----

It errors in swap because of internal pointers, when calling Tuple.opAssign.

You'll notice it now works in Head, but this is only because we lifted the
check in:
https://github.com/D-Programming-Language/phobos/pull/1390

However, even if the code "runs", it is still wrong, and not yet fixed, as
evidenced by this test:

//----
struct S
{
    invariant()
    {    
        assert(checksum == 1234567890);
    }    
    this(ulong u){n = u;}
    void opAssign(S s){this.n = s.n;}
    ulong n;
    ulong checksum = 1234567890;
}

void main()
{
    import std.stdio, std.algorithm, std.parallelism, std.typecons;

    immutable ulong[] data = [ 2UL^^59-1, 2UL^^59-1, 2UL^^59-1,
112_272_537_195_293UL ];

    static auto genPair(ulong n) { return S(n); }
    taskPool.amap!genPair(data);
}
//----

--


More information about the Digitalmars-d-bugs mailing list