Permalink
Browse files

Merge pull request #1355 from 9rnsr/fix8783

Issue 8783 - ref foreach update of const fixed size arrays in constructor
  • Loading branch information...
2 parents 4e98fb6 + a0da328 commit 3dff7653486bb68bbac21de2764cc76c93c259c4 @don-clugston-sociomantic don-clugston-sociomantic committed Dec 7, 2012
Showing with 28 additions and 3 deletions.
  1. +7 −2 src/declaration.c
  2. +3 −1 src/statement.c
  3. +18 −0 test/runnable/assignable.d
View
@@ -138,8 +138,13 @@ int Declaration::checkModify(Loc loc, Scope *sc, Type *t)
if ((sc->flags & SCOPEcontract) && isResult())
error(loc, "cannot modify result '%s' in contract", toChars());
- if (isCtorinit() && !t->isMutable() ||
- (storage_class & STCnodefaultctor))
+ if (isCtorinit() && !t->isMutable())
+ {
+ if ((storage_class & (STCforeach | STCref)) == (STCforeach | STCref))
+ return TRUE;
+ return modifyFieldVar(loc, sc, isVarDeclaration(), NULL);
+ }
+ else if (storage_class & STCnodefaultctor)
{ // It's only modifiable if inside the right constructor
return modifyFieldVar(loc, sc, isVarDeclaration(), NULL);
}
View
@@ -1812,7 +1812,9 @@ Statement *ForeachStatement::semantic(Scope *sc)
{
/* Reference to immutable data should be marked as const
*/
- if (!tn->isMutable())
+ if (aggr->checkCtorInit(sc))
+ var->storage_class |= STCctorinit;
+ else if (!tn->isMutable())
var->storage_class |= STCconst;
Type *t = tab->nextOf();
View
@@ -1121,6 +1121,24 @@ void test6336()
}
/***************************************************/
+// 8783
+
+struct Foo8783
+{
+ int[1] bar;
+}
+
+const Foo8783[1] foos8783;
+
+static this()
+{
+ foreach (i; 0 .. foos8783.length)
+ foos8783[i].bar[i] = 1; // OK
+ foreach (i, ref f; foos8783)
+ f.bar[i] = 1; // line 9, Error
+}
+
+/***************************************************/
// 9077
struct S9077a

0 comments on commit 3dff765

Please sign in to comment.