Problem with passing ref parameters to properties

SebastianA sebastian.ahlman at remedygames.com
Thu May 10 07:18:59 PDT 2012


We are seeing a problem with passing ref parameters to 
properties. This bug only occurs in certain situations. Consider 
the following code:

====
void runTest()
{
	Thing t;
	t.vPosition = (Clock.currStdTime % 2 == 0) ? Vec(2, 2) : Vec(3, 
3);
	Vec v = t.vPosition;

	outputDebug("%d %d\n", v.x, v.y);
}

struct Vec
{
	int x;
	int y;
}

struct Thing
{
	@property Vec vPosition() { return mPosition; }
	@property Vec vPosition( const ref Vec value ) { return 
mPosition = value; }

private:
	Vec mPosition;
}
===

Now, this code should output either "2 2" or "3 3" depending on 
the time. However, on release builds this code will output "0 0" 
every time it is run. This seems to happen regardless of the 
optimization level (we have tried with -O1 and -O3). Some things 
to note is that the bug will NOT occur if any of the following is 
true:

- We compile the code in debug mode. - We specify a constant 
known at compile time (eg. "true") instead of the 
non-deterministic time value. - We save the vector into a 
temporary local variable before passing it to the property. - We 
remove the "const ref" from the property setter and pass the 
vector by value.

Debugging the code reveals that the value passed to the getter is 
indeed 0,0, which rules out the getter as the error source.

We are building this on x64 Windows using gdc version 4.6.1 
20110627 (gdc hg r826:396ce79e6402(default), using dmd ) 
(tdm64-1).

Any idea what's going on?


More information about the D.gnu mailing list