Bug 13880 – nothrow @nogc std.algorithm.reduce on fixed-size arrays

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2014-12-20T13:12:06Z
Last change time
2018-04-05T05:15:27Z
Keywords
pull, rejects-valid
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2014-12-20T13:12:06Z
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-@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); }
Comment #1 by bearophile_hugs — 2014-12-20T13:16:16Z
This is a less easy case to support, because the slice length is known at compile-time to be > 0, but a slicing generates a dynamic array: void main() pure nothrow @safe @nogc { import std.algorithm: reduce, min; int[10] arr; reduce!min(arr[0 .. 5]); }
Comment #2 by greensunny12 — 2018-03-31T16:10:27Z
Comment #3 by github-bugzilla — 2018-04-05T05:15:26Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/ab70319d692639d207665ec50be61110c7d850a1 Fix Issue 13880 - nothrow @nogc std.algorithm.reduce on fixed-size arrays https://github.com/dlang/phobos/commit/25edf91761ec7a1dbe87933ea8d7f3621860163c Merge pull request #6398 from wilzbach/fix-13880 Fix Issue 13880 - nothrow @nogc std.algorithm.reduce on fixed-size arrays