D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4041 - Error with ref or auto ref return from opOpAssign
Summary: Error with ref or auto ref return from opOpAssign
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
: 4934 (view as issue list)
Depends on: 3175
Blocks:
  Show dependency treegraph
 
Reported: 2010-04-01 18:26 UTC by Mike Parker
Modified: 2012-02-01 02:10 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Mike Parker 2010-04-01 18:26:25 UTC
##############################
struct Foo
{
	int x;
	
	ref Foo opOpAssign(string op)(ref Foo f) if(op == "+=")
	{
		x += f.x;
		return this;
	}
}

void main()
{
	Foo f1 = Foo(1);
	Foo f2 = Foo(2);
	f2 += f1;
}
#################################

Results in: 

refret.d(5): Error: variable refret.Foo.op only parameters or foreach declaratio
ns can be ref

Change the return type from 'ref Foo' to 'auto ref Foo' and the same error occurs. Remove the ref from the return and it compiles. Comment out the 'f2 += f1' and it compiles.

This happens on both 2.041 and 2.042. I haven't tried earlier versions.

BTW, 2.042 is missing from the version list here on Bugzilla.
Comment 1 Shin Fujishiro 2010-09-25 18:19:48 UTC
*** Issue 4934 has been marked as a duplicate of this issue. ***
Comment 2 Kenji Hara 2011-06-20 02:17:53 UTC
Trivial fix of sample code:
----
struct Foo
{
    int x;

    ref Foo opOpAssign(string op)(ref Foo f) if(op == "+")  // <- "+="
    {
        x += f.x;
        return this;
    }
}
void main()
{
    Foo f1 = Foo(1);
    Foo f2 = Foo(2);
    f2 += f1;
}
----

And, on dmd 2.054 (d36b3b12fc4814e59f3a0d680b8700e787b1ceff) this code has passed to compile.
Comment 3 yebblies 2012-02-01 02:10:52 UTC
I can't reproduce this.