Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #702 from 9rnsr/fix7444

Issue 7444 - Require [] for array copies too
  • Loading branch information...
commit ba1009c5561b51b8f18d9c869fde9bd45cb7ebc7 2 parents 3644943 + 8519a3a
@WalterBright WalterBright authored
Showing with 102 additions and 0 deletions.
  1. +45 −0 src/expression.c
  2. +57 −0 test/fail_compilation/warn7444.d
View
45 src/expression.c
@@ -10714,6 +10714,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);
@@ -10809,6 +10823,24 @@ 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
@@ -10816,6 +10848,19 @@ Expression *AssignExp::semantic(Scope *sc)
}
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)
View
57 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
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.