Thread overview
[Issue 21756] Immutable array literals cause runtime GC allocation instead of static readonly section allocation
Mar 24, 2021
Eyal
Mar 31, 2021
Basile-z
Mar 31, 2021
Dlang Bot
Apr 01, 2021
Mathias LANG
Apr 01, 2021
Eyal
Dec 17, 2022
Iain Buclaw
March 24, 2021
https://issues.dlang.org/show_bug.cgi?id=21756

--- Comment #1 from Eyal <eyal@weka.io> ---
I am lacking a way to say the array literal is immutable, so it gets allocated the same way a string literal does.

This is especially problematic in a function like:

  @nogc string f() {
      enum compileTimeStr = "fmt %x".format(someNum());
      return compileTimeStr; // GC allocation here
  }

--
March 31, 2021
https://issues.dlang.org/show_bug.cgi?id=21756

Basile-z <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |b2.temp@gmx.com

--- Comment #2 from Basile-z <b2.temp@gmx.com> ---
cast(immutable) could do that. Under the hood a hidden static immutable would
be created.

--
March 31, 2021
https://issues.dlang.org/show_bug.cgi?id=21756

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #3 from Dlang Bot <dlang-bot@dlang.rocks> ---
@TungstenHeart created dlang/dmd pull request #12329 "fix 21756 - make `cast(immutable)arrayLiteral` @nogc" fixing this issue:

- fix 21756 - make `cast(immutable)arrayLiteral` @nogc

  adjust array literal semantics so that

  ```d
  cast(immutable) <arrayLiteral>
  ```

  gets rewritten to a DeclExp comma VarExp

  ```d
  (static immutable __array = <arrayLiteral>, __array)
  ```

  allowing immutable array literals to be `@nogc`

https://github.com/dlang/dmd/pull/12329

--
April 01, 2021
https://issues.dlang.org/show_bug.cgi?id=21756

Mathias LANG <pro.mathias.lang@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pro.mathias.lang@gmail.com

--- Comment #4 from Mathias LANG <pro.mathias.lang@gmail.com> ---
No, `cast` should not do this magic, it should be requested by the user
directly.
So:
```
auto g() { static immutable ret = ['a', 'b', 'c']; return ret; }
```

Leaving open for the benefit of the discussion (I found this issue because there's a PR), but IMO this is RESOLVED INVALID territory.

--
April 01, 2021
https://issues.dlang.org/show_bug.cgi?id=21756

--- Comment #5 from Eyal <eyal@weka.io> ---
Neither the PR nor this solution seem to resolve the issue. Consider these pieces of code.

Will NOT compile:

  @nogc string f() {
      static immutable s = "%5d".format(5);
      enum u = s;
      return u; // does GC :-(
  }

Will compile fine:

  @nogc string f() {
      static immutable s = "%5d".format(5);
      enum u = s ~ "";
      return u; // all is well
  }

Will also compile fine:

  @nogc string f() {
      static immutable s = "%5d".format(5) ~ "";
      enum u = s;
      return u; // all is well
  }

Surely this cannot be considered a reasonable state of things?

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=21756

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

--