Comment #0 by dlang-bugzilla — 2021-04-28T04:34:02Z
//////////// test.d ///////////
struct S
{
int[] arr;
}
void fun(S s)() @nogc
{
if (s.arr[0]) {}
}
static immutable S s1 = S([1]);
alias fun1 = fun!s1;
///////////////////////////////
Compiler output:
test.d(11,27): Error: array literal in `@nogc` function `test.fun!(S([1])).fun` may cause a GC allocation
test.d(13,14): Error: template instance `test.fun!(S([1]))` error instantiating
There are potentially three issues here:
1. The `s` parameter already has storage allocated for it, so, accessing an element of it should not require an allocation. It looks like currently when passing a static immutable argument to a value template parameter, the value is used like an `enum`.
2. Accessing an element of an `enum` array really shouldn't require allocating the array first.
3. The error message occurs far from the point where the allocation is attempted. Even though the error message mentions `fun`, none of the locations are within `fun`.
Comment #1 by robert.schadek — 2024-12-13T19:16:00Z