Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 7444 - Require [] for array copies too #702

Merged
merged 2 commits into from Mar 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/expression.c
Expand Up @@ -10786,6 +10786,20 @@ Expression *AssignExp::semantic(Scope *sc)
e2 = new SliceExp(e2->loc, e2, NULL, NULL);
e2 = e2->semantic(sc);
}
else if (global.params.warnings && !global.gag && op == TOKassign &&
e2->op != TOKarrayliteral && e2->op != TOKstring)
{ // Disallow sa = da (Converted to sa[] = da[])
// Disallow sa = e (Converted to sa[] = e)
const char* e1str = e1->toChars();
const char* e2str = e2->toChars();
if (e2->op == TOKslice || t2->implicitConvTo(t1->nextOf()))
warning("explicit element-wise assignment (%s)[] = %s is better than %s = %s",
e1str, e2str, e1str, e2str);
else
warning("explicit element-wise assignment (%s)[] = (%s)[] is better than %s = %s",
e1str, e2str, e1str, e2str);
return new ErrorExp();
}

// Convert e1 to e1[]
Expression *e = new SliceExp(e1->loc, e1, NULL, NULL);
Expand Down Expand Up @@ -10870,13 +10884,44 @@ Expression *AssignExp::semantic(Scope *sc)
{
checkPostblit(e2->loc, t2->nextOf());
}
if (global.params.warnings && !global.gag && op == TOKassign &&
e2->op != TOKslice && e2->op != TOKassign &&
e2->op != TOKarrayliteral && e2->op != TOKstring &&
!(e2->op == TOKadd || e2->op == TOKmin ||
e2->op == TOKmul || e2->op == TOKdiv ||
e2->op == TOKmod || e2->op == TOKxor ||
e2->op == TOKand || e2->op == TOKor ||
#if DMDV2
e2->op == TOKpow ||
#endif
e2->op == TOKtilde || e2->op == TOKneg))
{
const char* e1str = e1->toChars();
const char* e2str = e2->toChars();
warning("explicit element-wise assignment %s = (%s)[] is better than %s = %s",
e1str, e2str, e1str, e2str);
return new ErrorExp();
}
if (op == TOKconstruct)
e2 = e2->castTo(sc, e1->type->constOf());
else
e2 = e2->implicitCastTo(sc, e1->type->constOf());
}
else
{
if (global.params.warnings && !global.gag && op == TOKassign &&
t1->ty == Tarray && t2->ty == Tsarray &&
e2->op != TOKslice && //e2->op != TOKarrayliteral &&
t2->implicitConvTo(t1))
{ // Disallow ar[] = sa (Converted to ar[] = sa[])
// Disallow da = sa (Converted to da = sa[])
const char* e1str = e1->toChars();
const char* e2str = e2->toChars();
warning("explicit %s assignment %s = (%s)[] is better than %s = %s",
e1->op == TOKslice ? "element-wise" : "slice",
e1str, e2str, e1str, e2str);
return new ErrorExp();
}
e2 = e2->implicitCastTo(sc, e1->type);
}
if (e2->op == TOKerror)
Expand Down
57 changes: 57 additions & 0 deletions test/fail_compilation/warn7444.d
@@ -0,0 +1,57 @@
// REQUIRED_ARGS: -w
// PERMUTE_ARGS:

/*
TEST_OUTPUT:
---
fail_compilation/warn7444.d(30): Warning: explicit element-wise assignment (sa)[] = e is better than sa = e
fail_compilation/warn7444.d(32): Error: cannot implicitly convert expression (e) of type int to int[]
fail_compilation/warn7444.d(37): Warning: explicit element-wise assignment (sa)[] = sa[] is better than sa = sa[]
fail_compilation/warn7444.d(38): Warning: explicit element-wise assignment sa[] = (sa)[] is better than sa[] = sa
fail_compilation/warn7444.d(41): Warning: explicit element-wise assignment (sa)[] = (da)[] is better than sa = da
fail_compilation/warn7444.d(42): Warning: explicit element-wise assignment (sa)[] = da[] is better than sa = da[]
fail_compilation/warn7444.d(43): Warning: explicit element-wise assignment sa[] = (da)[] is better than sa[] = da
fail_compilation/warn7444.d(47): Warning: explicit slice assignment da = (sa)[] is better than da = sa
fail_compilation/warn7444.d(49): Warning: explicit element-wise assignment da[] = (sa)[] is better than da[] = sa
fail_compilation/warn7444.d(54): Warning: explicit element-wise assignment da[] = (da)[] is better than da[] = da
---
*/

void test7444()
{
int[2] sa;
int[] da;
int e;

{
// X: Changed accepts-invalid to rejects-invalid by this issue
// a: slice assginment
// b: element-wise assignment
sa = e; // X
sa[] = e; // b
da = e;
da[] = e; // b

// lhs is static array
sa = sa; // b == identity assign
sa = sa[]; // X
sa[] = sa; // X
sa[] = sa[]; // b

sa = da; // X
sa = da[]; // X
sa[] = da; // X
sa[] = da[]; // b

// lhs is dynamic array
da = sa; // X
da = sa[]; // a
da[] = sa; // X
da[] = sa[]; // b

da = da; // a == identity assign
da = da[]; // a
da[] = da; // X
da[] = da[]; // b
}
}