Bug 21871 – Accessing elements of "static immutable" arrays passed as template parameters requires allocation

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-04-28T04:34:02Z
Last change time
2024-12-13T19:16:00Z
Keywords
diagnostic, rejects-valid
Assigned to
No Owner
Creator
Vladimir Panteleev
Moved to GitHub: dmd#19913 →

Comments

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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19913 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB