Skip to content

Commit

Permalink
Merge pull request #2891 from 9rnsr/fix11525
Browse files Browse the repository at this point in the history
[REG2.065a] Issue 11525 - Error: 'a[] *= a[]' each element is not a scalar, it is a Complex!double
  • Loading branch information
MartinNowak committed Nov 30, 2013
2 parents e0b6a37 + 46bee52 commit 9a7ebe8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/arrayop.c
Expand Up @@ -279,7 +279,14 @@ ArrayOp *buildArrayOp(Identifier *ident, BinExp *exp, Scope *sc, Loc loc)
sc->linkage = LINKc;
fd->semantic(sc);
fd->semantic2(sc);
unsigned errors = global.startGagging();
fd->semantic3(sc);
if (global.endGagging(errors))
{
fd->type = Type::terror;
fd->errors = true;
fd->fbody = NULL;
}
sc->pop();

if (op->cFunc)
Expand Down Expand Up @@ -368,12 +375,6 @@ Expression *BinExp::arrayOp(Scope *sc)
error("Cannot perform array operations on void[] arrays");
return new ErrorExp();
}
if (!tbn->isscalar())
{
error("'%s' each element is not a scalar, it is a %s", toChars(), tbn->toChars());
return new ErrorExp();
}

if (!isArrayOpValid(this))
{
error("invalid array operation %s (did you forget a [] ?)", toChars());
Expand Down Expand Up @@ -405,6 +406,20 @@ Expression *BinExp::arrayOp(Scope *sc)
if (!op)
op = buildArrayOp(ident, this, sc, loc);

if (op->dFunc && op->dFunc->errors)
{
const char *fmt;
if (tbn->ty == Tstruct || tbn->ty == Tclass)
fmt = "invalid array operation '%s' because %s doesn't support necessary arithmetic operations";
else if (!tbn->isscalar())
fmt = "invalid array operation '%s' because %s is not a scalar type";
else
fmt = "invalid array operation '%s' for element type %s";

error(fmt, toChars(), tbn->toChars());
return new ErrorExp();
}

*pOp = op;

FuncDeclaration *fd = op->cFunc ? op->cFunc : op->dFunc;
Expand Down
26 changes: 26 additions & 0 deletions test/fail_compilation/ice11376.d
@@ -0,0 +1,26 @@
/*
TEST_OUTPUT:
---
fail_compilation/ice11376.d(17): Error: invalid array operation 'x1[] = x2[] * x3[]' because X doesn't support necessary arithmetic operations
fail_compilation/ice11376.d(21): Error: invalid array operation 's2[] += s1[]' because string is not a scalar type
fail_compilation/ice11376.d(25): Error: invalid array operation 'pa1[] *= pa2[]' for element type int*
---
*/

void main()
{
struct X { }

auto x1 = [X()];
auto x2 = [X()];
auto x3 = [X()];
x1[] = x2[] * x3[];

string[] s1;
string[] s2;
s2[] += s1[];

int*[] pa1;
int*[] pa2;
pa1[] *= pa2[];
}
25 changes: 25 additions & 0 deletions test/runnable/arrayop.d
Expand Up @@ -712,6 +712,30 @@ auto sumArrs11376(T0, T1)(T0[] a, T1[] b)

static assert(!__traits(compiles, sumArrs11376(TL11376!(string[], string).init)));

/************************************************************************/
// 11525

void test11525()
{
static struct Complex(T)
{
T re, im;

ref opOpAssign(string op : "*")(Complex z)
{
auto temp = re*z.re - im*z.im;
im = im*z.re + re*z.im;
re = temp;
return this;
}
}

auto a = [Complex!double(2, 2)];
assert(a.length == 1 && a[0].re == 2 && a[0].im == 2);
a[] *= a[];
assert(a.length == 1 && a[0].re == 0 && a[0].im == 8);
}

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

int main()
Expand All @@ -728,6 +752,7 @@ int main()
test10433();
test10684a();
test10684b();
test11525();

printf("Success\n");
return 0;
Expand Down

0 comments on commit 9a7ebe8

Please sign in to comment.