Skip to content

Commit

Permalink
Fixes Issue 8777
Browse files Browse the repository at this point in the history
Add test-cases for const/immutable diagnostics.
Use MODtoBuffer for output.
  • Loading branch information
AndrejMitrovic committed Oct 24, 2012
1 parent 68ab986 commit 7aaa509
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 15 deletions.
20 changes: 6 additions & 14 deletions src/expression.c
Expand Up @@ -1842,12 +1842,9 @@ void Expression::checkModifiable(Scope *sc)
}
else
{
const char *p =
type->isImmutable() ? "immutable" :
type->isConst() ? "const" :
type->isWild() ? "wild" : NULL;
assert(p);
error("cannot modify %s expression %s", p, toChars());
OutBuffer buf;
MODtoBuffer(&buf, type->mod);
error("cannot modify %s expression %s", buf.toChars(), toChars());
}
}
}
Expand Down Expand Up @@ -7580,14 +7577,9 @@ Expression *CallExp::resolveUFCS(Scope *sc)
return new ErrorExp();
}
if (!e->type->isMutable())
{ const char *p = NULL;
if (e->type->isConst())
p = "const";
else if (e->type->isImmutable())
p = "immutable";
else
p = "inout";
error("cannot remove key from %s associative array %s", p, e->toChars());
{ OutBuffer buf;
MODtoBuffer(&buf, e->type->mod);
error("cannot remove key from %s associative array %s", buf.toChars(), e->toChars());
return new ErrorExp();
}
Expression *key = (*arguments)[0];
Expand Down
6 changes: 5 additions & 1 deletion src/func.c
Expand Up @@ -1234,7 +1234,11 @@ void FuncDeclaration::semantic3(Scope *sc)
* as delegating calls to other constructors
*/
if (v->isCtorinit() && !v->type->isMutable() && cd)
error("missing initializer for final field %s", v->toChars());
{
OutBuffer buf;
MODtoBuffer(&buf, v->type->mod);
error("missing initializer for %s field %s", buf.toChars(), v->toChars());
}
else if (v->storage_class & STCnodefaultctor)
error("field %s must be initialized in constructor", v->toChars());
}
Expand Down
12 changes: 12 additions & 0 deletions test/fail_compilation/diag8777a.d
@@ -0,0 +1,12 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag8777a.d(3): Error: constructor diag8777a.Foo.this missing initializer for immutable field bar
---
*/

#line 1
class Foo {
immutable int[5] bar;
this() {}
}
12 changes: 12 additions & 0 deletions test/fail_compilation/diag8777b.d
@@ -0,0 +1,12 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag8777b.d(3): Error: constructor diag8777b.Foo.this missing initializer for const field bar
---
*/

#line 1
class Foo {
const int[5] bar;
this() {}
}
13 changes: 13 additions & 0 deletions test/fail_compilation/diag8777c.d
@@ -0,0 +1,13 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag8777c.d(4): Error: cannot modify const expression x
---
*/

#line 1
void test()
{
const int x;
x = 1;
}
13 changes: 13 additions & 0 deletions test/fail_compilation/diag8777d.d
@@ -0,0 +1,13 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag8777d.d(4): Error: cannot modify immutable expression x
---
*/

#line 1
void test()
{
immutable int x;
x = 1;
}
14 changes: 14 additions & 0 deletions test/fail_compilation/diag8777e.d
@@ -0,0 +1,14 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag8777e.d(5): Error: cannot remove key from immutable associative array hash
---
*/

#line 1
immutable(int[int]) hash;

void main()
{
hash.remove(1);
}
14 changes: 14 additions & 0 deletions test/fail_compilation/diag8777f.d
@@ -0,0 +1,14 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag8777f.d(5): Error: cannot remove key from const associative array hash
---
*/

#line 1
const(int[int]) hash;

void main()
{
hash.remove(1);
}

0 comments on commit 7aaa509

Please sign in to comment.