On Thursday, 17 April 2014 at 09:46:23 UTC, bearophile wrote:
Walter Bright:
http://wiki.dlang.org/DIP60
Start on implementation:
https://github.com/D-Programming-Language/dmd/pull/3455
If I have this program:
__gshared int x = 5;
int main() {
int[] a = [x, x + 10, x * x];
return a[0] + a[1] + a[2];
}
If I compile with all optimizations DMD produces this X86 asm,
that contains the call to __d_arrayliteralTX, so that main
can't be @nogc:
But if I compile the code with ldc2 with full optimizations the
compiler is able to perform a bit of escape analysis, and to
see the array doesn't need to be allocated, and produces the
asm:
Now there are no memory allocations.
So what's the right behavour of @nogc? Is it possible to
compile this main with a future version of ldc2 if I compile
the code with full optimizations?
Bye,
bearophile
That code is not @nogc safe, as you're creating a dynamic array
within it. The fact that LDC2 at full optimizations doesn't
actually allocate is simply an optimization and does not affect
the design of the code.
If you wanted it to be @nogc, you could use:
int main() @nogc {
int[3] a = [x, x + 10, x * x];
return a[0] + a[1] + a[2];
}