4 changes: 4 additions & 0 deletions src/cast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,10 @@ MATCH implicitConvTo(Expression *e, Type *t)
result = MATCHconst;
}
}

// Enhancement 10724
if (tb->ty == Tpointer && e->e1->op == TOKstring)
e->e1->accept(this);
}
};

Expand Down
48 changes: 38 additions & 10 deletions src/optimize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1060,19 +1060,33 @@ Expression *Expression_optimize(Expression *e, int result, bool keepLvalue)
{
// Convert slice of string literal into dynamic array
Type *t = e->e1->type->toBasetype();
if (t->nextOf())
ret = e->e1->castTo(NULL, t->nextOf()->arrayOf());
if (Type *tn = t->nextOf())
ret = e->e1->castTo(NULL, tn->arrayOf());
}
return;
}
e->e1 = fromConstInitializer(result, e->e1);
// We might know $ now
setLengthVarIfKnown(e->lengthVar, e->e1);
e->lwr = e->lwr->optimize(WANTvalue);
e->upr = e->upr->optimize(WANTvalue);
ret = Slice(e->type, e->e1, e->lwr, e->upr).copy();
if (CTFEExp::isCantExp(ret))
else
{
e->e1 = fromConstInitializer(result, e->e1);
// We might know $ now
setLengthVarIfKnown(e->lengthVar, e->e1);
e->lwr = e->lwr->optimize(WANTvalue);
e->upr = e->upr->optimize(WANTvalue);
ret = Slice(e->type, e->e1, e->lwr, e->upr).copy();
if (CTFEExp::isCantExp(ret))
ret = e;
}

// Bugzilla 14649: We need to leave the slice form so it might be
// a part of array operation.
// Assume that the backend codegen will handle the form `e[]`
// as an equal to `e` itself.
if (ret->op == TOKstring)
{
e->e1 = ret;
e->lwr = NULL;
e->upr = NULL;
ret = e;
}
//printf("-SliceExp::optimize() %s\n", ret->toChars());
}

Expand Down Expand Up @@ -1197,6 +1211,20 @@ Expression *Expression_optimize(Expression *e, int result, bool keepLvalue)
}
}

// optimize "str"[] -> "str"
if (e->e1->op == TOKslice)
{
SliceExp *se1 = (SliceExp *)e->e1;
if (se1->e1->op == TOKstring && !se1->lwr)
e->e1 = se1->e1;
}
if (e->e2->op == TOKslice)
{
SliceExp *se2 = (SliceExp *)e->e2;
if (se2->e1->op == TOKstring && !se2->lwr)
e->e2 = se2->e1;
}

ret = Cat(e->type, e->e1, e->e2).copy();
if (CTFEExp::isCantExp(ret))
ret = e;
Expand Down
11 changes: 0 additions & 11 deletions test/fail_compilation/fail266.d

This file was deleted.

18 changes: 0 additions & 18 deletions test/fail_compilation/fail323.d

This file was deleted.

35 changes: 0 additions & 35 deletions test/fail_compilation/fail3903.d

This file was deleted.

54 changes: 0 additions & 54 deletions test/fail_compilation/fail9459.d

This file was deleted.

150 changes: 150 additions & 0 deletions test/fail_compilation/fail_arrayop1.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// REQUIRED_ARGS: -o-

/*
TEST_OUTPUT:
---
fail_compilation/fail_arrayop1.d(11): Error: invalid array operation a + a (possible missing [])
---
*/
void test2199(int[] a) // Issue 2199 - Segfault using array operation in function call (from fail266.d)
{
test2199(a + a);
}

/*
TEST_OUTPUT:
---
fail_compilation/fail_arrayop1.d(29): Error: invalid array operation -a (possible missing [])
---
*/
void fail323() // from fail323.d, maybe was a part of issue 3471 fix?
{
void foo(double[]) {}

auto a = new double[10],
b = a.dup,
c = a.dup,
d = a.dup;

foo(-a);
// a[] = -(b[] * (c[] + 4)) + 5 * d[]; // / 3;
}

/*
TEST_OUTPUT:
---
fail_compilation/fail_arrayop1.d(54): Error: invalid array operation -a (possible missing [])
fail_compilation/fail_arrayop1.d(55): Error: invalid array operation ~a (possible missing [])
fail_compilation/fail_arrayop1.d(56): Error: invalid array operation a + a (possible missing [])
fail_compilation/fail_arrayop1.d(57): Error: invalid array operation a - a (possible missing [])
fail_compilation/fail_arrayop1.d(58): Error: invalid array operation a * a (possible missing [])
fail_compilation/fail_arrayop1.d(59): Error: invalid array operation a / a (possible missing [])
fail_compilation/fail_arrayop1.d(60): Error: invalid array operation a % a (possible missing [])
fail_compilation/fail_arrayop1.d(61): Error: invalid array operation a ^^ a (possible missing [])
fail_compilation/fail_arrayop1.d(62): Error: invalid array operation a & a (possible missing [])
fail_compilation/fail_arrayop1.d(63): Error: invalid array operation a | a (possible missing [])
fail_compilation/fail_arrayop1.d(64): Error: invalid array operation a ^ a (possible missing [])
---
*/
void test3903()
{
int[] a = [1, 2];
int[] r;

r = -a;
r = ~a;
r = a + a;
r = a - a;
r = a * a;
r = a / a;
r = a % a;
r = a ^^ a;
r = a & a;
r = a | a;
r = a ^ a;
}

/*
TEST_OUTPUT:
---
fail_compilation/fail_arrayop1.d(85): Error: invalid array operation a += a[] (possible missing [])
fail_compilation/fail_arrayop1.d(86): Error: invalid array operation a -= a[] (possible missing [])
fail_compilation/fail_arrayop1.d(87): Error: invalid array operation a *= a[] (possible missing [])
fail_compilation/fail_arrayop1.d(88): Error: invalid array operation a /= a[] (possible missing [])
fail_compilation/fail_arrayop1.d(89): Error: invalid array operation a %= a[] (possible missing [])
fail_compilation/fail_arrayop1.d(90): Error: invalid array operation a ^= a[] (possible missing [])
fail_compilation/fail_arrayop1.d(91): Error: invalid array operation a &= a[] (possible missing [])
fail_compilation/fail_arrayop1.d(92): Error: invalid array operation a |= a[] (possible missing [])
fail_compilation/fail_arrayop1.d(93): Error: invalid array operation a ^^= a[] (possible missing [])
---
*/
void test9459()
{
int[] a = [1, 2, 3];

a += a[];
a -= a[];
a *= a[];
a /= a[];
a %= a[];
a ^= a[];
a &= a[];
a |= a[];
a ^^= a[];
}

/*
TEST_OUTPUT:
---
fail_compilation/fail_arrayop1.d(111): Error: invalid array operation 'x1[] = x2[] * x3[]' because X doesn't support necessary arithmetic operations
fail_compilation/fail_arrayop1.d(115): Error: invalid array operation 's2[] += s1[]' because string is not a scalar type
fail_compilation/fail_arrayop1.d(119): Error: invalid array operation 'pa1[] *= pa2[]' for element type int*
---
*/
void test11376()
{
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[];
}

/*
TEST_OUTPUT:
---
fail_compilation/fail_arrayop1.d(131): Error: invalid array operation a[] <<= 1 (possible missing [])
---
*/
void test11566()
{
int[] a;
a[] <<= 1;
}

/*
TEST_OUTPUT:
---
fail_compilation/fail_arrayop1.d(147): Error: invalid array operation a + b (possible missing [])
fail_compilation/fail_arrayop1.d(148): Error: invalid array operation x + y (possible missing [])
fail_compilation/fail_arrayop1.d(149): Error: invalid array operation "hel" + "lo." (possible missing [])
---
*/
void test14649()
{
char[] a, b, r;
string x, y;

r[] = a + b;
r[] = x + y;
r[] = "hel" + "lo.";
}
Loading