Skip to content

Commit

Permalink
Merge pull request #3474 from AndrejMitrovic/Fix12480
Browse files Browse the repository at this point in the history
Issue 12480 - Implement better static assert diagnostics for static immutable arrays.
  • Loading branch information
9rnsr committed Apr 20, 2014
2 parents c643ec5 + 7ec47e1 commit 8903f3f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/optimize.c
Expand Up @@ -960,6 +960,18 @@ Expression *Expression_optimize(Expression *e, int result, bool keepLvalue)
ret = e->e1;
return;
}

// CTFE interpret static immutable arrays (to get better diagnostics)
if (e->e1->op == TOKvar)
{
VarDeclaration *v = ((VarExp *)e->e1)->var->isVarDeclaration();
if (v && (v->storage_class & STCstatic) && (v->storage_class & STCimmutable) && v->init)
{
if (Expression *ci = v->getConstInitializer())
e->e1 = ci;
}
}

if (e->e1->op == TOKstring || e->e1->op == TOKarrayliteral || e->e1->op == TOKassocarrayliteral ||
e->e1->type->toBasetype()->ty == Tsarray)
{
Expand Down
12 changes: 12 additions & 0 deletions test/fail_compilation/diag12480.d
@@ -0,0 +1,12 @@
// REQUIRED_ARGS: -m32
/*
TEST_OUTPUT:
---
fail_compilation/diag12480.d(12): Error: static assert (2u == 3u) is false
---
*/

module diag12480;

static immutable arr = ["a", "b"];
static assert(arr.length == 3);

0 comments on commit 8903f3f

Please sign in to comment.