D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15718 - use ref or out parameters in the anonymous method may cause error
Summary: use ref or out parameters in the anonymous method may cause error
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-02-24 09:25 UTC by mzfhhhh
Modified: 2022-11-03 09:56 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description mzfhhhh 2016-02-24 09:25:09 UTC
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 RazvanN 2022-11-03 09:56:12 UTC
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.