Skip to content

Commit

Permalink
Merge pull request #1364 from 9rnsr/fix9140
Browse files Browse the repository at this point in the history
Issue 9140 - ref foreach of immutables in postcondition
  • Loading branch information
WalterBright committed Dec 13, 2012
2 parents c4dc640 + 596fae9 commit 2284bb9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
9 changes: 2 additions & 7 deletions src/declaration.c
Expand Up @@ -138,13 +138,8 @@ 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())
{
if ((storage_class & (STCforeach | STCref)) == (STCforeach | STCref))
return TRUE;
return modifyFieldVar(loc, sc, isVarDeclaration(), NULL);
}
else if (storage_class & STCnodefaultctor)
if (isCtorinit() && !t->isMutable() ||
(storage_class & STCnodefaultctor))
{ // It's only modifiable if inside the right constructor
return modifyFieldVar(loc, sc, isVarDeclaration(), NULL);
}
Expand Down
4 changes: 1 addition & 3 deletions src/statement.c
Expand Up @@ -1812,9 +1812,7 @@ Statement *ForeachStatement::semantic(Scope *sc)
{
/* Reference to immutable data should be marked as const
*/
if (aggr->checkCtorInit(sc))
var->storage_class |= STCctorinit;
else if (!tn->isMutable())
if (!tn->isMutable())
var->storage_class |= STCconst;

Type *t = tab->nextOf();
Expand Down
13 changes: 13 additions & 0 deletions test/runnable/assignable.d
Expand Up @@ -1123,6 +1123,8 @@ void test6336()
/***************************************************/
// 8783

version(none)
{
struct Foo8783
{
int[1] bar;
Expand All @@ -1137,6 +1139,7 @@ static this()
foreach (i, ref f; foos8783)
f.bar[i] = 1; // line 9, Error
}
}

/***************************************************/
// 9077
Expand All @@ -1154,6 +1157,16 @@ struct S9077b
this(this) {}
}

/***************************************************/
// 9140

immutable(int)[] bar9140()
out(result) {
foreach (ref r; result) {}
} body {
return null;
}

/***************************************************/

int main()
Expand Down

0 comments on commit 2284bb9

Please sign in to comment.