Thread overview
Regression in 2.060 - corruption when lambda closes over foreach variable
Sep 16, 2012
Ben Davis
Sep 16, 2012
Ben Davis
Sep 17, 2012
Nick Sabalausky
Sep 18, 2012
Ben Davis
September 16, 2012
Hi,

I have some code which has started breaking as of 2.060. I've simplified it down to the following:

------------

import std.stdio;

void main() {
	broken();
	reffix();
	noclosefix();
}

void broken() {
	foreach (z; 0..1) {
		writeln(z);	//Corrupt
		() {writeln(z);} ();	//Corrupt (same)
	}
}

//Adding 'ref' fixes it:
void reffix() {
	foreach (ref z; 0..1) {
		writeln(z);	//0
		() {writeln(z);} ();	//0
	}
}

//It only breaks if 'z' is closed into the lambda:
void noclosefix() {
	foreach (z; 0..1) {
		writeln(z);	//0
		int z2=z;
		() {writeln(z2);} ();	//0
	}
}

------------

The sort of corrupt values I see for z are for example
28835840 = 0x01B80000
29949952 = 0x01C90000
38535168 = 0x024C0000
36110336 = 0x02270000
But it's always the same between one writeln and the other.

Also breaks with foreach_reverse.

I'm compiling with no switches at all, just "dmd filename.d".

I'm compiling with debug, without optimisations.

Anyone fancy taking a look?

Thanks,

Ben :)
September 16, 2012
Sorry, the second "I'm compiling with" is a lie. (Edited it out below.)

On 17/09/2012 00:16, Ben Davis wrote:
> Hi,
>
> I have some code which has started breaking as of 2.060. I've simplified
> it down to the following:
>
> ------------
>
> import std.stdio;
>
> void main() {
>      broken();
>      reffix();
>      noclosefix();
> }
>
> void broken() {
>      foreach (z; 0..1) {
>          writeln(z);    //Corrupt
>          () {writeln(z);} ();    //Corrupt (same)
>      }
> }
>
> //Adding 'ref' fixes it:
> void reffix() {
>      foreach (ref z; 0..1) {
>          writeln(z);    //0
>          () {writeln(z);} ();    //0
>      }
> }
>
> //It only breaks if 'z' is closed into the lambda:
> void noclosefix() {
>      foreach (z; 0..1) {
>          writeln(z);    //0
>          int z2=z;
>          () {writeln(z2);} ();    //0
>      }
> }
>
> ------------
>
> The sort of corrupt values I see for z are for example
> 28835840 = 0x01B80000
> 29949952 = 0x01C90000
> 38535168 = 0x024C0000
> 36110336 = 0x02270000
> But it's always the same between one writeln and the other.
>
> Also breaks with foreach_reverse.
>
> I'm compiling with no switches at all, just "dmd filename.d".
>
> Anyone fancy taking a look?
>
> Thanks,
>
> Ben :)

September 17, 2012
You should probably file that here:

http://d.puremagic.com/issues/

September 18, 2012
On 17/09/2012 07:02, Nick Sabalausky wrote:
> You should probably file that here:
>
> http://d.puremagic.com/issues/

Done :)

These two already existed:

http://d.puremagic.com/issues/show_bug.cgi?id=8621 <-- posted here
http://d.puremagic.com/issues/show_bug.cgi?id=8526

I guess they're probably duplicates.