Skip to content

Commit

Permalink
Merge pull request #662 from yebblies/issue4517
Browse files Browse the repository at this point in the history
Issue 4517 - final switch over with base type allows missing values
  • Loading branch information
Don Clugston committed Jul 17, 2012
2 parents 716aa6e + ef6d2e1 commit 00778d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/statement.c
Expand Up @@ -3146,6 +3146,10 @@ Statement *SwitchStatement::semantic(Scope *sc)
return this; // already run
condition = condition->semantic(sc);
condition = resolveProperties(sc, condition);
TypeEnum *te = NULL;
// preserve enum type for final switches
if (condition->type->ty == Tenum)
te = (TypeEnum *)condition->type;
if (condition->type->isString())
{
// If it's not an array, cast it to one
Expand Down Expand Up @@ -3210,8 +3214,8 @@ Statement *SwitchStatement::semantic(Scope *sc)
{ // Don't use toBasetype() because that will skip past enums
t = ((TypeTypedef *)t)->sym->basetype;
}
if (condition->type->ty == Tenum)
{ TypeEnum *te = (TypeEnum *)condition->type;
if (te)
{
EnumDeclaration *ed = te->toDsymbol(sc)->isEnumDeclaration();
assert(ed);
size_t dim = ed->members->dim;
Expand All @@ -3222,7 +3226,7 @@ Statement *SwitchStatement::semantic(Scope *sc)
{
for (size_t j = 0; j < cases->dim; j++)
{ CaseStatement *cs = (*cases)[j];
if (cs->exp->equals(em->value))
if (cs->exp->equals(em->value) || cs->exp->toInteger() == em->value->toInteger())
goto L1;
}
error("enum member %s not represented in final switch", em->toChars());
Expand Down
15 changes: 15 additions & 0 deletions test/fail_compilation/fail4517.d
@@ -0,0 +1,15 @@

enum E : ushort
{
A, B
}

void main()
{
E e;
final switch(e)
{
case E.A:
break;
}
}

0 comments on commit 00778d3

Please sign in to comment.