Bug 14807 – unnecessary closure allocation for function literal in compile time test
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-07-17T20:34:17Z
Last change time
2021-12-08T18:02:19Z
Assigned to
No Owner
Creator
Martin Nowak
Comments
Comment #0 by code — 2015-07-17T20:34:17Z
cat > bug.d << CODE
void foo(int a) @nogc
{
static if (__traits(compiles, { a = 2; }))
{}
}
CODE
bug.d(1): Error: function bug.foo @nogc function allocates a closure with the GC
----
Not sure if it's easy to detect the context of a function literal and see that it can never be called.
Comment #1 by ag0aep6g — 2015-07-17T20:41:44Z
Silly workaround: Pass the literal to another literal that takes a scope delegate.
----
void foo(int a) @nogc
{
static if (__traits(compiles,
(scope void delegate()){}(
{ a = 2; }
)
))
{}
}
----
Comment #2 by stanislav.blinov — 2021-12-08T18:02:19Z