Issue 17800 - [2.076] "static foreach" allocates closures in GC without reason
Summary: [2.076] "static foreach" allocates closures in GC without reason
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2017-09-02 06:49 UTC by Илья Ярошенко
Modified: 2017-10-01 20:40 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Илья Ярошенко 2017-09-02 06:49:27 UTC
struct S
{
    enum N = 1;
}

void foo(S v) @nogc
{
    static foreach(_; 0 .. v.N)
    {
    }
}

----
Error: function mir.ndslice.slice.foo is @nogc yet allocates closures with the GC
source\mir\ndslice\slice.d(290,23):        
foo.__lambda2.__lambda1 closes over variable v
Comment 1 Илья Ярошенко 2017-09-02 07:05:37 UTC
struct S
{
    enum N = 1;
}

S foo(S v) @nogc
{
    static foreach(_; 0 .. typeof(return).N)
    {
    }
    return S.init;
}
----
Error: cannot use typeof(return) inside function __lambda1 with inferred return type
Comment 2 timon.gehr 2017-09-04 08:56:35 UTC
The closure allocation error is not a bug in the `static foreach` implementation. Reduced test case:

---
struct S{
    enum N=1;
}

void foo(S v) @nogc{
    enum x=()=>v.N;
}
---

Workaround:

---
int foo(S v)@nogc{
    enum x=typeof(v).N;
}
---


The second case is due to an oversight on my side.
It can be worked around using an alias:

struct S{
    enum N = 1;
}

S foo(S v)@nogc{
    alias R=typeof(return);
    static foreach(_;0..R.N){ }
    return S.init;
}
Comment 3 timon.gehr 2017-09-04 09:56:48 UTC
https://github.com/dlang/dmd/pull/7118

The fix for the second case is at the same time a workaround for the first case. I'll file another bug report for the underlying cause.
Comment 4 timon.gehr 2017-09-04 10:03:41 UTC
(In reply to timon.gehr from comment #3)
> case. I'll file another bug report for the underlying cause.

https://issues.dlang.org/show_bug.cgi?id=17804
Comment 5 github-bugzilla 2017-09-09 03:44:45 UTC
Commits pushed to stable at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/17a1effdb48c0d31bb3ba3611d8a22ef2ce8abed
fix Issue 17800 - [2.076] "static foreach" allocates closures in GC without reason

https://github.com/dlang/dmd/commit/365e392f67aa66bab08a40fcb4336b6c600df511
Merge pull request #7118 from tgehr/fix17800

fix Issue 17800 - [2.076] "static foreach" allocates closures in GC without reason
merged-on-behalf-of: Walter Bright <WalterBright@users.noreply.github.com>
Comment 6 github-bugzilla 2017-10-01 20:40:58 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/17a1effdb48c0d31bb3ba3611d8a22ef2ce8abed
fix Issue 17800 - [2.076] "static foreach" allocates closures in GC without reason

https://github.com/dlang/dmd/commit/365e392f67aa66bab08a40fcb4336b6c600df511
Merge pull request #7118 from tgehr/fix17800