Consider:
final class RedBlackTree(alias less)
{
this()
{
}
}
auto redBlackTree(alias less)()
{
return new RedBlackTree!(less)();
}
@save void test()
{
//static bool compare(double x, double y) { return x < y; }
//auto rbt2 = redBlackTree!(compare, double)(5.1, 2.3);
auto rbt2 = redBlackTree!((a, b) => a < b)();
//auto rbt2 = redBlackTree!((a, b){return a < b;})();
}
This fails to compile with -preview=dip1000 with:
test.d(18): Error: `@safe` function `test.test` cannot call `@system` function `test.test.redBlackTree!((a, b) => a < b).redBlackTree`
test.d(11): which was inferred `@system` because of:
test.d(11): escaping reference to stack allocated value returned by `new RedBlackTree!(__lambda1)`
test.d(9): `test.test.redBlackTree!((a, b) => a < b).redBlackTree` is declared here
The lambda (a,b)=>a<b is resolved as a delegate, but since it never accesses its delegate pointer, it is not really an escaping reference to something the delegate may refer to.
The compiler should detect this and not issue the error.
Comment #1 by robert.schadek — 2024-12-13T19:24:11Z