Skip to content

Commit

Permalink
Merge pull request #1907 from donc/voidenum9921
Browse files Browse the repository at this point in the history
9921: Enum variables of type void should be illegal
  • Loading branch information
WalterBright committed Apr 18, 2013
2 parents 202a28b + 35a3765 commit 2fb8a99
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/enum.c
Expand Up @@ -126,7 +126,7 @@ void EnumDeclaration::semantic(Scope *sc)

//printf("EnumDeclaration::semantic(sd = %p, '%s') %s\n", sc->scopesym, sc->scopesym->toChars(), toChars());
//printf("EnumDeclaration::semantic() %s\n", toChars());
if (!members) // enum ident;
if (!members && !memtype) // enum ident;
return;

if (!memtype && !isAnonymous())
Expand Down Expand Up @@ -182,6 +182,11 @@ void EnumDeclaration::semantic(Scope *sc)
return;
}
}
if (memtype->ty == Tvoid)
{
error("base type must not be void");
memtype = Type::terror;
}
#if 0 // Decided to abandon this restriction for D 2.0
if (!memtype->isintegral())
{ error("base type must be of integral type, not %s", memtype->toChars());
Expand All @@ -191,6 +196,10 @@ void EnumDeclaration::semantic(Scope *sc)
}

isdone = 1;

if (!members) // enum ident : memtype;
return;

Module::dprogress++;

type = type->semantic(loc, sc);
Expand Down Expand Up @@ -277,6 +286,13 @@ void EnumDeclaration::semantic(Scope *sc)
if (!isAnonymous())
e = e->castTo(sce, type);
}
else if (memtype && memtype == Type::terror)
{
e = new ErrorExp();
minval = e;
maxval = e;
defaultval = e;
}
else
{
// Lazily evaluate enum.max
Expand Down Expand Up @@ -327,7 +343,7 @@ void EnumDeclaration::semantic(Scope *sc)
* If enum doesn't have a name, we can never identify the enum type,
* so there is no purpose for a .min, .max or .default
*/
if (!isAnonymous())
if (!isAnonymous() && memtype != Type::terror)
{
if (first)
{ defaultval = e;
Expand Down
12 changes: 12 additions & 0 deletions test/fail_compilation/enum9921.d
@@ -0,0 +1,12 @@
/*
TEST_OUTPUT:
---
fail_compilation/enum9921.d(1): Error: enum enum9921.X base type must not be void
fail_compilation/enum9921.d(3): Error: enum enum9921.Z base type must not be void
---
*/

#line 1
enum X : void;

enum Z : void { Y };

0 comments on commit 2fb8a99

Please sign in to comment.