Bug 23299 – Detect if lambda does not access its delegate context pointer

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-08-20T14:10:20Z
Last change time
2024-12-13T19:24:11Z
Assigned to
No Owner
Creator
Walter Bright
Moved to GitHub: dmd#18120 →

Comments

Comment #0 by bugzilla — 2022-08-20T14:10:20Z
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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18120 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB