Permalink
Browse files

Appendix for Issue 6652

If *implicit ref* foreach key doesn't have a mutable type, warning is not necessary.
  • Loading branch information...
1 parent 093cf54 commit 780ce35e8338435e5d063da2cc93360d7f0aecbd @9rnsr 9rnsr committed Nov 10, 2012
Showing with 28 additions and 2 deletions.
  1. +2 −2 src/expression.c
  2. +13 −0 test/fail_compilation/fail6652a.d
  3. +13 −0 test/fail_compilation/fail6652b.d
View
@@ -5142,11 +5142,11 @@ Expression *VarExp::modifiableLvalue(Scope *sc, Expression *e)
#if (BUG6652 == 1)
VarDeclaration *v = var->isVarDeclaration();
- if (v && (v->storage_class & STCbug6652))
+ if (v && (v->storage_class & STCbug6652) && v->type->isMutable())
warning("variable modified in foreach body requires ref storage class");
#elif (BUG6652 == 2)
VarDeclaration *v = var->isVarDeclaration();
- if (v && (v->storage_class & STCbug6652))
+ if (v && (v->storage_class & STCbug6652) && v->type->isMutable())
deprecation("variable modified in foreach body requires ref storage class");
#endif
@@ -3,11 +3,24 @@
/******************************************/
// 6652
+/*
+TEST_OUTPUT:
+---
+fail_compilation/fail6652a.d(19): Warning: variable modified in foreach body requires ref storage class
+fail_compilation/fail6652a.d(24): Error: cannot modify const expression i
+---
+*/
+
void main()
{
size_t[] res;
foreach (i; 0..2)
{
res ~= ++i;
}
+
+ foreach (const i; 0..2)
+ {
+ ++i;
+ }
}
@@ -3,11 +3,24 @@
/******************************************/
// 6652
+/*
+TEST_OUTPUT:
+---
+fail_compilation/fail6652b.d(19): Warning: variable modified in foreach body requires ref storage class
+fail_compilation/fail6652b.d(24): Error: cannot modify const expression i
+---
+*/
+
void main()
{
size_t[] res;
foreach (i, e; [1,2,3,4,5])
{
res ~= ++i;
}
+
+ foreach (const i, e; [1,2,3,4,5])
+ {
+ ++i;
+ }
}

0 comments on commit 780ce35

Please sign in to comment.