A little known feature of `toDelegate` is that it can take a functor (a struct with opCall) and return a delegate to that.
However, I noticed that it takes its parameter as `auto ref`, meaning if it's a struct functor, the delegate it will return is a dangling pointer at the stack-stored struct.
example to cause the problem:
```d
import std.functional;
import std.stdio;
struct S
{
int x;
this(int x) { this.x = x; }
int opCall() { return x;}
}
void main()
{
auto dg = toDelegate(S(5));
writeln("garbage garbage"); // to smash the stack
writeln(dg());
}
```
On run.dlang.io, it produced the result:
garbage garbage
4517416
Clearly not the 5 that was expected.
`toDelegate` can't be marked @safe partly because of this, but also it probably should never return a delegate to a local stack variable that is about to go away, even in @system code.
Comment #1 by robert.schadek — 2024-12-01T16:40:19Z