www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12666] New: nogc std.array.front, popFront, and more

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

          Issue ID: 12666
           Summary:  nogc std.array.front, popFront, and more
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P1
         Component: Phobos
          Assignee: nobody puremagic.com
          Reporter: bearophile_hugs eml.cc

import std.algorithm: map;
immutable data = [1];
void main()  nogc {
    foreach (w; data.map!((x)  nogc pure nothrow => x)) {}
}


test.d(4,5): Error:  nogc function 'D main' cannot call non- nogc function
'test.main.MapResult!(__lambda1, immutable(int)[]).MapResult.popFront'
test.d(4,5): Error:  nogc function 'D main' cannot call non- nogc function
'test.main.MapResult!(__lambda1, immutable(int)[]).MapResult.front'


Taking a better look the problem seems to be in std.array.front (and popFront):


 property ref T front(T)(T[] a)  safe pure nothrow
if (!isNarrowString!(T[]) && !is(T[] == void[]))
{
    assert(a.length, "Attempting to fetch the front of an empty array of " ~
T.stringof);
    return a[0];
}

void main()  nogc {
    auto data = [1];
    data.front;
}


A possible solution:

import std.traits: isNarrowString;

 property ref T front(T)(T[] a)  safe pure nothrow
if (!isNarrowString!(T[]) && !is(T[] == void[]))
{
    enum msg = "Attempting to fetch the front of an empty array of " ~
T.stringof;
    assert(a.length, msg);
    return a[0];
}

void main()  nogc {
    auto data = [1];
    data.front;
}


A small disadvantage of this solution is that the error message is now computed
statically, increasing binary size. But those array functions are too much
important, otherwise lot of other ranges can't work in  nogc code.

--
Apr 27 2014