Code:
---
struct S
{
uint[] arr = [42];
}
pure nothrow @safe @nogc unittest
{
auto s = S.init;
}
---
Error:
---
test.d(3): Error: array literal in @nogc function __unittestL6_1 may cause GC allocation
---
Loosely related to https://issues.dlang.org/show_bug.cgi?id=2947
Comment #1 by schveiguy — 2022-08-03T01:29:32Z
This reduces in AST to:
```d
S s = S([42]);
```
Which is why it always allocates.
This also defies the spec, which states:
> .init produces a constant expression that is the default initializer.
This is not a constant expression.
This also leads to such puzzles as `assert(S.init !is S.init)`
Also note that:
```d
S s;
```
Does *not* produce an allocation.
S.init should not allocate. Neither should S()
Comment #2 by robert.schadek — 2024-12-13T18:45:59Z