Bug 21800 – Unused lambda causes unnecessary closure allocation

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2021-04-05T16:20:29Z
Last change time
2024-12-13T19:15:44Z
Assigned to
No Owner
Creator
Eyal
Moved to GitHub: dmd#18031 →

Comments

Comment #0 by eyal — 2021-04-05T16:20:29Z
This program fails to compile in the "ignore!..." line. @nogc nothrow void ignore(alias F)() {} @nogc nothrow void call(alias F)() { F(); } @nogc void main() { int x; ignore!(()=>x)(); // delegate causes "x" to come from GC call!(()=>x)(); // is fine } It compiles fine if the "ignore!" line is commented out. If a delegate is unused, surely its scope is at least as narrow as a delegate that is used. Why is @nogc inference not done on the template instantiation result?
Comment #1 by snarwin+bugzilla — 2021-08-10T14:02:45Z
Simplified example: --- void main() @nogc { int x; void ignore() { alias F = () => x; } } --- Interestingly, replacing the lambda with a named function gets rid of the closure allocation: --- void main() @nogc { int x; void ignore() { auto F() { return x; } } } --- The above workaround also works for the original example.
Comment #2 by robert.schadek — 2024-12-13T19:15:44Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18031 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB