Thread overview
[Issue 12936] Some more @nogc cases for immediately iterated array literal
May 19, 2016
Kenji Hara
Aug 25, 2022
RazvanN
March 11, 2015
https://issues.dlang.org/show_bug.cgi?id=12936

--- Comment #1 from bearophile_hugs@eml.cc ---
A different case where the compiler should avoid heap allocations and allow @nogc:


void main() @nogc {
    int[3] a;
    a[] = [1, 2, 3];
    a[0 .. 2] = [1, 2];
}


temp.d(3,11): Error: array literal in @nogc function main may cause GC
allocation
temp.d(4,17): Error: array literal in @nogc function main may cause GC
allocation

--
May 19, 2016
https://issues.dlang.org/show_bug.cgi?id=12936

Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
           Hardware|x86                         |All
                 OS|Windows                     |All

--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> ---
(In reply to bearophile_hugs from comment #0)
> 
> struct F { int x; }
> void main() @nogc {
>     foreach (       a; [F(1)]) {}           // Case#1 OK
>     foreach (int[1] a; [[1]]) {}            // Case#2 Error
>     foreach (const ref int[1] a; [[1]]) {}  // Case#3 Error
>     foreach (       a; [[1]]) {}            // Case#4 Error
> }

For case#2, it's possible enhancement because int[1] a is a copy of iterated array literal element.

For case#3, it's not safe because an address of stack allocated array literal can escape out of the lifetime.

    const(int)* p;
    foreach (const ref int[1] a; [[1]]) { p = &a[0]; }
    // After the foreach, p wold point invalid stack address.

Note that similar situation is properly rejected.

    void foo(ref int[1] sa) {}
    void main() {
        foo([1]);   // array literal never become stack allocated because
                    // its address can escape out via the ref parameter.
    }

For case#4 supporting it would cause inconsistent type inference result.

    void foo() {
        foreach (a; [[1]]) { pragma(msg, typeof(a)); }  // prints int[]
    }
    void bar() @nogc {
        foreach (a; [[1]]) { pragma(msg, typeof(a)); }  // prints int[] or
int[1]?
    }

I opened a compiler improvement for case#2: https://github.com/dlang/dmd/pull/5795

--
August 10, 2016
https://issues.dlang.org/show_bug.cgi?id=12936

--- Comment #3 from github-bugzilla@puremagic.com ---
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/6a48d5bb77be35a3778c7ad673333de232b2e9c8 Issue 12936 - Some more @nogc cases for immediately iterated array literal

Support case#2 in bugzilla.

https://github.com/dlang/dmd/commit/46c35125b335354077c11973389c7e82686036ff Merge pull request #5795 from 9rnsr/fix12936

Issue 12936 - Some more @nogc cases for immediately iterated array literal

--
October 01, 2016
https://issues.dlang.org/show_bug.cgi?id=12936

--- Comment #4 from github-bugzilla@puremagic.com ---
Commits pushed to stable at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/6a48d5bb77be35a3778c7ad673333de232b2e9c8 Issue 12936 - Some more @nogc cases for immediately iterated array literal

https://github.com/dlang/dmd/commit/46c35125b335354077c11973389c7e82686036ff Merge pull request #5795 from 9rnsr/fix12936

--
August 25, 2022
https://issues.dlang.org/show_bug.cgi?id=12936

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |razvan.nitu1305@gmail.com
         Resolution|---                         |FIXED

--