On Friday, 5 July 2019 at 23:08:04 UTC, Patrick Schluter wrote:
On Thursday, 4 July 2019 at 10:56:50 UTC, Nick Treleaven wrote:
immutable(int[]) f() @nogc {
    return [1,2];
}

onlineapp.d(2): Error: array literal in `@nogc` function `onlineapp.f` may cause a GC allocation

This makes dynamic array literals unusable with @nogc, and adds to GC pressure for no reason. What code would break if dmd used only static data for [1,2]?

int[] in D is not an array but a fat pointer. When one realizes that then it become quite obvious why [1,2] was allocated. There is somewhere in the binary a static array [1,2] but as it is assigned to a pointer to mutable data, the compiler has no choice as to allocate a mutable copy of that immutable array.

and it cannot optimize it away because it doesn't know what the caller want to do with it. It might in another module invoke it and modify it, the compiler cannot tell. auto a=f(); a[0]++;

Reply via email to