www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12808] New: Small amount of escape analysis to allow more

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

          Issue ID: 12808
           Summary: Small amount of escape analysis to allow more  nogc
                    functions
           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

DMD 2.066alpha accepts this code:


void main()  nogc {
    int[2] tmp = [1, 2];
    foreach (x; tmp) {}
}


While it refuses this code:


void main()  nogc {
    foreach (x; [1, 2]) {}
}


With:

test.d(3,17): Error: array literal in  nogc function main may cause GC
allocation

I suggest to start introducing a small amount of Escape Analysis in D, to



Eventually even this program could be supported:


void main()  nogc {
    import std.algorithm: filter;
    foreach (x; [1, 2].filter!(x => true)) {}
}



Note that with the []s suffix syntax for fixed-size arrays there is no
ambiguity:



void main()  nogc {
    foreach (x; [1, 2]s) {}
}



And this can generate a clean error message (escape of pointer to stack frame
fixed-size array):



int[] foo()  nogc {
    return [1, 2]s;
}
void main() {}


See also Issue 10242

--
May 26 2014