[Issue 13880] New: nothrow @nogc std.algorithm.reduce on fixed-size arrays

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Dec 20 05:12:06 PST 2014


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

          Issue ID: 13880
           Summary: nothrow @nogc std.algorithm.reduce on fixed-size
                    arrays
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: enhancement
          Priority: P1
         Component: Phobos
          Assignee: nobody at puremagic.com
          Reporter: bearophile_hugs at eml.cc

std.algorithm.reduce is nothrow @nogc if you give it a seed:

void main() pure nothrow @safe @nogc {
    import std.algorithm: reduce, min;
    int[5] arr;
    cast(void)reduce!min(0, arr);
}


Otherwise it can throw (and the creation of the exception is currently not
@nogc), this happens when the input range is empty:


void main() pure nothrow @safe @nogc {
    import std.algorithm: reduce, min;
    int[] arr;
    cast(void)reduce!min(arr);
}



But if you give it a fixed-size array and its length is known at compile-time
to be not empty, reduce can't throw, so this should compile:

void main() pure nothrow @safe @nogc {
    import std.algorithm: reduce, min;
    int[5] arr;
    reduce!min(arr);
}


In dmd 2.067alpha it gives:

test.d(4,8): Error: @nogc function 'D main' cannot call non- at nogc function
'std.algorithm.reduce!(min).reduce!(int[5]).reduce'
test.d(4,8): Error: 'std.algorithm.reduce!(min).reduce!(int[5]).reduce' is not
nothrow
test.d(1,6): Error: function 'D main' is nothrow yet may throw


This code can become a compile-time error, or it can always raise an exception
at run-time:

void main() pure @safe {
    import std.algorithm: reduce, min;
    int[0] arr;
    reduce!min(arr);
}

--


More information about the Digitalmars-d-bugs mailing list