Bug 17800 – [2.076] "static foreach" allocates closures in GC without reason

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-09-02T06:49:27Z
Last change time
2017-10-01T20:40:58Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
Илья Ярошенко

Comments

Comment #0 by ilyayaroshenko — 2017-09-02T06:49:27Z
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 by ilyayaroshenko — 2017-09-02T07:05:37Z
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 by timon.gehr — 2017-09-04T08:56:35Z
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 by timon.gehr — 2017-09-04T09:56:48Z
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 by timon.gehr — 2017-09-04T10:03:41Z
(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 by github-bugzilla — 2017-09-09T03:44:45Z
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 <[email protected]>
Comment #6 by github-bugzilla — 2017-10-01T20:40:58Z
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