```
enum x = [1,4];
@nogc void main()
{
// Easy case
// int[] x = [1,2,3,4];
auto b = x[0..1];
}
```
Comment #1 by maxhaton — 2022-12-12T18:25:22Z
Error: array literal in `@nogc` function `D main` may cause a GC allocation
is not precise enough.
Comment #2 by dkorpel — 2022-12-12T18:28:45Z
What is the expected message?
Comment #3 by grimmaple95 — 2022-12-12T18:32:46Z
(In reply to Dennis from comment #2)
> What is the expected message?
The error itself targets this line
```
auto b = x[0..1];
```
which isn't what causes the issue. It's this line
```
enum x = [1,4];
```
which prevents slicing from being @nogc compatible. If changed to static immutable x = [1,4]; code compiles fine
Comment #4 by dlang-bot — 2022-12-12T18:46:17Z
@maxhaton created dlang/dmd pull request #14690 "Fix Issue 23551 - Start making nogc error messages for array literals…" fixing this issue:
- Fix Issue 23551 - Start making nogc error messages for array literals more precise
This makes it more obvious what the array literal is, the greater issue is to know where it was lowered from. More on that soon.
https://github.com/dlang/dmd/pull/14690
Comment #5 by robert.schadek — 2024-12-13T19:26:22Z