www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12642] New: Avoid some heap allocation cases for fixed-size

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

          Issue ID: 12642
           Summary: Avoid some heap allocation cases for fixed-size arrays
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: bearophile_hugs eml.cc

The  nogc annotation of dmd 2.066alpha refuses the following programs:


__gshared int[1] data1;
int[1] bar()  nogc {
    int x;
    return [x];
}
void main()  nogc {
    int x;
    data1 = [x];
    int[1] data2;
    data2 = [x];
}


test.d(4,12): Error: array literals in  nogc function bar may cause GC
allocation
test.d(8,13): Error: array literals in  nogc function main may cause GC
allocation
test.d(10,13): Error: array literals in  nogc function main may cause GC
allocation


But is it possible to avoid heap allocations in those cases, to allow those
programs to compile?


This similar code compiles:

__gshared int[1] data1;
int[1] bar()  nogc {
    int x;
    typeof(return) tmp;
    tmp[0] = x;
    return tmp;
}
void main()  nogc {
    int x;
    data1[0] = x;
    int[1] data2;
    data2[0] = x;
}

--
Apr 25 2014