Skip to content

Commit

Permalink
Merge pull request #6477 from LemonBoy/b17111
Browse files Browse the repository at this point in the history
Fix issue 17111 - More robust detection of variables used as case exp.
  • Loading branch information
MartinNowak committed Feb 11, 2017
2 parents a1c9439 + 418f08b commit e15fb01
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/statementsem.d
Expand Up @@ -2168,11 +2168,17 @@ else
cs.exp = cs.exp.implicitCastTo(sc, sw.condition.type);
cs.exp = cs.exp.optimize(WANTvalue | WANTexpand);

Expression e = cs.exp;
// Remove all the casts the user and/or implicitCastTo may introduce
// otherwise we'd sometimes fail the check below.
while (e.op == TOKcast)
e = (cast(CastExp)e).e1;

/* This is where variables are allowed as case expressions.
*/
if (cs.exp.op == TOKvar)
if (e.op == TOKvar)
{
VarExp ve = cast(VarExp)cs.exp;
VarExp ve = cast(VarExp)e;
VarDeclaration v = ve.var.isVarDeclaration();
Type t = cs.exp.type.toBasetype();
if (v && (t.isintegral() || t.ty == Tclass))
Expand Down
13 changes: 13 additions & 0 deletions test/compilable/b17111.d
@@ -0,0 +1,13 @@
alias TestType = ubyte;

void test()
{
TestType a,b,c;

switch(c)
{
case a: break;
case (cast(ushort)b): break;
default: assert(false);
}
}

0 comments on commit e15fb01

Please sign in to comment.