November 19, 2014
https://issues.dlang.org/show_bug.cgi?id=13750

          Issue ID: 13750
           Summary: @nogc decreasing array lenghts with decrementing
                    operator --
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: enhancement
          Priority: P1
         Component: druntime
          Assignee: nobody@puremagic.com
          Reporter: bearophile_hugs@eml.cc

I think (with the current dynamic array design) decreasing array lenghts should
be @nogc:

void main() @nogc {
    int[3] a = [1, 2, 3];
    auto b = a[];
    b = b[0 .. $ - 1]; // OK
    assert(b.length == 2);
    b.length--; // Error, rejects-valid
}


dmd 2.067alpha gives:

test2.d(6,13): Error: setting 'length' in @nogc function main may cause GC
allocation


In theory this too should be @nogc, but I think this can be done only with some weak form of dependent typing and only if the increase/decrease values are compile-time constants:

void main() @nogc {
    int[3] a = [1, 2, 3];
    auto b = a[];
    b.length -= 1;
    assert(b.length == 2);
    b.length += -1;
    assert(b.length == 1);
}

--