Bug 15718 – use ref or out parameters in the anonymous method may cause error

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2016-02-24T09:25:09Z
Last change time
2022-11-03T09:56:12Z
Assigned to
No Owner
Creator
mzfhhhh

Comments

Comment #0 by mzfhhhh — 2016-02-24T09:25:09Z
use ref or out parameters in the anonymous method may cause error, for example: void delegate() test1(ref int a,ref int b,ref int c) { return { a=1; b=2; c=3; }; } void delegate() test2() { int i; int a; int b; int c; auto p = test1(a,b,c); return p; } void test3(void delegate() p) { int[3] arr; p(); writeln(arr);//arr output: 1,2,3 } void main(string[] args) { auto p = test2(); test3(p); }
Comment #1 by razvan.nitu1305 — 2022-11-03T09:56:12Z
What happens here is that you are creating a delegate that receives some stack pointers. When the delegate is actually called it ends up modifying an expired stack frame (which coincidentally points to the current frame). Although this is problematic, I would argue that it is a code problem, not a compiler one. What should the compiler be doing here? I guess that in @safe code it should not allow the references to a, b, and c escape the scope of test1. However, this bug report already exists: https://issues.dlang.org/show_bug.cgi?id=23438 . I will close this as WONTFIX because this system code is ok from a compiler perspective.