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:

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


While it refuses this code:

// Program#2
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
support the  nogc attribute for the Program#2.


Eventually even this program could be supported:

// Program#3
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:


// Program#4
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):


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


See also Issue 10242

--
May 26 2014