Skip to content

Commit 84fcc60

Browse files
committed
Fix issue 17111 - More robust detection of variables used as case exp.
1 parent 2853931 commit 84fcc60

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/statementsem.d

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,11 +2168,17 @@ else
21682168
cs.exp = cs.exp.implicitCastTo(sc, sw.condition.type);
21692169
cs.exp = cs.exp.optimize(WANTvalue | WANTexpand);
21702170

2171+
Expression e = cs.exp;
2172+
// Remove all the casts the user and/or implicitCastTo may introduce
2173+
// otherwise we'd sometimes fail the check below.
2174+
while (e.op == TOKcast)
2175+
e = (cast(CastExp)e).e1;
2176+
21712177
/* This is where variables are allowed as case expressions.
21722178
*/
2173-
if (cs.exp.op == TOKvar)
2179+
if (e.op == TOKvar)
21742180
{
2175-
VarExp ve = cast(VarExp)cs.exp;
2181+
VarExp ve = cast(VarExp)e;
21762182
VarDeclaration v = ve.var.isVarDeclaration();
21772183
Type t = cs.exp.type.toBasetype();
21782184
if (v && (t.isintegral() || t.ty == Tclass))

test/compilable/b17111.d

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import std.stdio : readf, writeln;
2+
alias TestType = ubyte;
3+
4+
void main()
5+
{
6+
TestType a,b,c;
7+
readf("%s %s %s ", &a, &b, &c);
8+
9+
switch(c)
10+
{
11+
case a : writeln("a") ;break;
12+
case (cast(ushort)b): writeln("b") ;break;
13+
default : assert(false);
14+
}
15+
}

0 commit comments

Comments
 (0)