Bug 15408 – S.init for struct containing member field with array literal initializer fails @nogc

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-12-05T16:44:36Z
Last change time
2024-12-13T18:45:59Z
Keywords
spec, wrong-code
Assigned to
No Owner
Creator
Jakob Ovrum
Moved to GitHub: dmd#19074 →

Comments

Comment #0 by jakobovrum — 2015-12-05T16:44:36Z
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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19074 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB