www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21871] New: Accessing elements of "static immutable" arrays

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

          Issue ID: 21871
           Summary: Accessing elements of "static immutable" arrays passed
                    as template parameters requires allocation
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: diagnostic, rejects-valid
          Severity: normal
          Priority: P3
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: dlang-bugzilla thecybershadow.net

//////////// 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`.

--
Apr 27