Skip to content

Commit

Permalink
fix Issue 12778 - Aliasing opBinaryRight to opBinary works only in ce…
Browse files Browse the repository at this point in the history
…rtain cases
  • Loading branch information
9rnsr committed May 31, 2014
1 parent d1beeaa commit 80e3e8a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/opover.c
Expand Up @@ -587,6 +587,11 @@ Expression *op_overload(Expression *e, Scope *sc)
if (ad2 && id_r)
{
s_r = search_function(ad2, id_r);

// Bugzilla 12778: If both x.opBinary(y) and y.opBinaryRight(x) found,
// and they are exactly same symbol, x.opBinary(y) should be preferred.
if (s_r && s_r == s)
s_r = NULL;
}
#endif

Expand Down Expand Up @@ -621,6 +626,8 @@ Expression *op_overload(Expression *e, Scope *sc)
result = new ErrorExp();
return;
}
if (s_r && s_r == s) // Bugzilla 12778
s_r = NULL;
}

// Set tiargs, the template argument list, which will be the operator string
Expand Down Expand Up @@ -723,6 +730,8 @@ Expression *op_overload(Expression *e, Scope *sc)
if (ad2 && id)
{
s = search_function(ad2, id);
if (s && s == s_r) // Bugzilla 12778
s = NULL;
}

if (s || s_r)
Expand Down
48 changes: 48 additions & 0 deletions test/runnable/opover.d
Expand Up @@ -1020,6 +1020,54 @@ void test8522()
assert(cp == cp); // doesn't work
}

/**************************************/
// 12778

struct Vec12778X
{
Vec12778X opBinary(string op)(Vec12778X b) const
if (op == "+")
{
mixin("return Vec12778X(this.x " ~ op ~ " b.x, this.y " ~ op ~ " b.y);");
}
alias opBinaryRight = opBinary;

float x = 0, y = 0;
}

struct Vec12778Y
{
Vec12778Y opAdd()(Vec12778Y b) const
{
enum op = "+";
mixin("return Vec12778Y(this.x " ~ op ~ " b.x, this.y " ~ op ~ " b.y);");
}
alias opAdd_r = opAdd;

float x = 0, y = 0;
}

void test12778()
{
struct S
{
void test1()
{
Vec12778X vx = vx1 + vx2; // ok
Vec12778Y vy = vy1 + vy2; // ok
}

void test2() const
{
Vec12778X vx = vx1 + vx2; // ok <- error
Vec12778Y vy = vy1 + vy2; // ok <- error
}

Vec12778X vx1, vx2;
Vec12778Y vy1, vy2;
}
}

/**************************************/

int main()
Expand Down

0 comments on commit 80e3e8a

Please sign in to comment.