Skip to content

Commit

Permalink
fix Issue 8783 - ref foreach update of const fixed size arrays in con…
Browse files Browse the repository at this point in the history
…structor
  • Loading branch information
9rnsr committed Dec 7, 2012
1 parent ee695eb commit a0da328
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/declaration.c
Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/statement.c
Expand Up @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions test/runnable/assignable.d
Expand Up @@ -1120,6 +1120,24 @@ void test6336()
// Allow return by ref both S1 and S2
}

/***************************************************/
// 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

Expand Down

0 comments on commit a0da328

Please sign in to comment.