Merge pull request #6398 from wilzbach/fix-13880

Fix Issue 13880 - nothrow @nogc std.algorithm.reduce on fixed-size arrays
This commit is contained in:
Sebastian Wilzbach 2018-04-05 07:15:21 +02:00 committed by GitHub
commit 25edf91761
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2793,7 +2793,13 @@ if (fun.length >= 1)
static if (isInputRange!R)
{
enforce(!r.empty, "Cannot reduce an empty input range w/o an explicit seed value.");
// no need to throw if range is statically known to be non-empty
static if (!__traits(compiles,
{
static assert(r.length > 0);
}))
enforce(!r.empty, "Cannot reduce an empty input range w/o an explicit seed value.");
Args result = r.front;
r.popFront();
return reduceImpl!false(r, result);
@ -2882,8 +2888,15 @@ if (fun.length >= 1)
args[i] = f(args[i], e);
}
static if (mustInitialize)
if (!initialized)
throw new Exception("Cannot reduce an empty iterable w/o an explicit seed value.");
// no need to throw if range is statically known to be non-empty
static if (!__traits(compiles,
{
static assert(r.length > 0);
}))
{
if (!initialized)
throw new Exception("Cannot reduce an empty iterable w/o an explicit seed value.");
}
static if (Args.length == 1)
return args[0];
@ -3165,6 +3178,19 @@ The number of seeds must be correspondingly increased.
assert(data.length == 0);
}
// https://issues.dlang.org/show_bug.cgi?id=13880
// reduce shouldn't throw if the length is statically known
pure nothrow @safe @nogc unittest
{
import std.algorithm.comparison : min;
int[5] arr;
arr[2] = -1;
assert(arr.reduce!min == -1);
int[0] arr0;
assert(reduce!min(42, arr0) == 42);
}
//Helper for Reduce
private template ReduceSeedType(E)
{