Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1431 from 9rnsr/fix9263

Issue 9263 - statement is not reachable when statement is reachable
  • Loading branch information...
commit 539b54ac7c423f53966384bd572ec4db10c6da0e 2 parents b07e161 + 9e58954
Walter Bright WalterBright authored
Showing with 208 additions and 188 deletions.
  1. +3 −1 src/statement.c
  2. +205 −187 test/runnable/testswitch.d
4 src/statement.c
View
@@ -3321,8 +3321,10 @@ Statement *SwitchStatement::semantic(Scope *sc)
a->reserve(2);
sc->sw->sdefault = new DefaultStatement(loc, s);
- a->push(sc->sw->sdefault);
a->push(body);
+ if (body->blockExit(FALSE) & BEfallthru)
+ a->push(new BreakStatement(0, NULL));
+ a->push(sc->sw->sdefault);
cs = new CompoundStatement(loc, a);
body = cs;
}
392 test/runnable/testswitch.d
View
@@ -1,4 +1,4 @@
-// PERMUTE_ARGS:
+// PERMUTE_ARGS: -w
extern(C) int printf(const char*, ...);
@@ -8,28 +8,29 @@ int testswitch(string h)
switch (h)
{
- case "abc":
- printf("abc\n");
- x = 4;
- break;
- case "foo":
- printf("foo\n");
- x = 1;
- break;
- case "bar":
- printf("bar\n");
- x = 2;
- break;
- default:
- printf("default\n");
- x = 3;
- break;
+ case "abc":
+ printf("abc\n");
+ x = 4;
+ break;
+ case "foo":
+ printf("foo\n");
+ x = 1;
+ break;
+ case "bar":
+ printf("bar\n");
+ x = 2;
+ break;
+ default:
+ printf("default\n");
+ x = 3;
+ break;
}
return x;
}
void test1()
-{ int i;
+{
+ int i;
i = testswitch("foo");
printf("i = %d\n", i);
@@ -43,18 +44,18 @@ void test1()
/*****************************************/
void test2()
-{ int i;
+{
+ int i;
switch (5)
{
- case 3,4,5,6:
- i = 20;
- break;
-
- case 7:
- default:
- assert(0);
- break;
+ case 3,4,5,6:
+ i = 20;
+ break;
+
+ case 7:
+ default:
+ assert(0);
}
assert(i == 20);
}
@@ -63,20 +64,21 @@ void test2()
/*****************************************/
void test3()
-{ int i;
+{
+ int i;
switch (5)
{
- case 7:
- i = 6;
- goto default;
- default:
- i = 8;
- break;
-
- case 3,4,5,6:
- i = 20;
- goto default;
+ case 7:
+ i = 6;
+ goto default;
+ default:
+ i = 8;
+ break;
+
+ case 3,4,5,6:
+ i = 20;
+ goto default;
}
assert(i == 8);
}
@@ -89,17 +91,17 @@ void test4()
switch (5)
{
- case 3,4,5,6:
- i = 20;
- goto default;
+ case 3,4,5,6:
+ i = 20;
+ goto default;
- case 7:
- i = 6;
- goto default;
+ case 7:
+ i = 6;
+ goto default;
- default:
- i = 8;
- break;
+ default:
+ i = 8;
+ break;
}
assert(i == 8);
}
@@ -112,16 +114,16 @@ void test5()
switch (5)
{
- case 7:
- i = 6;
- goto case;
- default:
- i = 8;
- break;
-
- case 3,4,5,6:
- i = 20;
- break;
+ case 7:
+ i = 6;
+ goto case;
+ default:
+ i = 8;
+ break;
+
+ case 3,4,5,6:
+ i = 20;
+ break;
}
assert(i == 20);
}
@@ -130,20 +132,21 @@ void test5()
/*****************************************/
void test6()
-{ int i;
+{
+ int i;
switch (5)
{
- case 7:
- i = 6;
- goto case 4;
- default:
- i = 8;
- break;
-
- case 3,4,5,6:
- i = 20;
- break;
+ case 7:
+ i = 6;
+ goto case 4;
+ default:
+ i = 8;
+ break;
+
+ case 3,4,5,6:
+ i = 20;
+ break;
}
assert(i == 20);
}
@@ -152,20 +155,21 @@ void test6()
/*****************************************/
void test7()
-{ int i;
+{
+ int i;
switch (5)
{
- case 3,4,5,6:
- i = 20;
- break;
-
- case 7:
- i = 6;
- goto case 4;
- default:
- i = 8;
- break;
+ case 3,4,5,6:
+ i = 20;
+ break;
+
+ case 7:
+ i = 6;
+ goto case 4;
+ default:
+ i = 8;
+ break;
}
assert(i == 20);
}
@@ -178,15 +182,15 @@ void test8()
dstring str = "xyz";
switch (str)
{
- case "xyz":
- printf("correct\n");
- return;
+ case "xyz":
+ printf("correct\n");
+ return;
- case "abc":
- break;
+ case "abc":
+ break;
- default:
- assert(0);
+ default:
+ assert(0);
}
assert(0);
}
@@ -199,18 +203,18 @@ void test9()
switch(i)
{
- case 2:
- return;
- case 1:
- switch(i)
- {
- case 1:
- goto case 2;
- default:
- assert(0);
- }
- default:
- assert(0);
+ case 2:
+ return;
+ case 1:
+ switch(i)
+ {
+ case 1:
+ goto case 2;
+ default:
+ assert(0);
+ }
+ default:
+ assert(0);
}
assert(0);
}
@@ -231,8 +235,8 @@ void test10()
case 3: ++id; goto case;
case 2: ++id; goto case;
case 1: ++id; goto default;
- default:
- break;
+ default:
+ break;
}
assert(id == 8);
}
@@ -244,11 +248,11 @@ void test11()
long foo = 4;
switch (foo)
{
- case 2: assert (false); break;
- case 3: break;
- case 4: break;
- case 5: break;
- default: assert(0);
+ case 2: assert (false);
+ case 3: break;
+ case 4: break;
+ case 5: break;
+ default: assert(0);
}
}
@@ -256,68 +260,66 @@ void test11()
void test12()
{
- switch("#!")
- {
- case "#!": printf("----Found #!\n"); break;
- case "\xFF\xFE"c: break;
- default:
- assert(0);
- printf("----Found ASCII\n"); break;
- }
+ switch("#!")
+ {
+ case "#!": printf("----Found #!\n"); break;
+ case "\xFF\xFE"c: break;
+ default:
+ assert(0);
+ }
}
/*****************************************/
void test13()
{
- switch("#!")
- {
- case "#!": printf("----Found #!\n"); break;
- case "#\xFE"c: break;
- default:
- assert(0);
- printf("----Found ASCII\n"); break;
- }
+ switch("#!")
+ {
+ case "#!": printf("----Found #!\n"); break;
+ case "#\xFE"c: break;
+ default:
+ assert(0);
+ }
}
/*****************************************/
void foo14(A...)(int i)
{
- switch (i)
- {
- foreach(a; A)
- {
- goto case;
- case a:
- printf("%d\n", a);
- }
- break;
- default:
- assert(0);
- }
+ switch (i)
+ {
+ foreach(a; A)
+ {
+ goto case;
+ case a:
+ printf("%d\n", a);
+ }
+ break;
+ default:
+ assert(0);
+ }
}
void bar14(A...)(int i)
{
- switch (i)
+ switch (i)
+ {
+ foreach(j, a; A)
{
- foreach(j, a; A)
- {
- goto case;
- case A[j]:
- printf("a = %d, A[%d] = %d\n", a, j, A[j]);
- }
- break;
- default:
- assert(0);
+ goto case;
+ case A[j]:
+ printf("a = %d, A[%d] = %d\n", a, j, A[j]);
}
+ break;
+ default:
+ assert(0);
+ }
}
void test14()
{
- foo14!(1,2,3,4,5)(1);
- bar14!(1,2,3,4,5)(1);
+ foo14!(1,2,3,4,5)(1);
+ bar14!(1,2,3,4,5)(1);
}
/*****************************************/
@@ -331,21 +333,21 @@ int foo15(int i)
auto y = 1;
switch (i)
{
- case X15:
- y += 1;
- goto case;
- case 3:
- y += 2;
- break;
- case Y15:
- y += 20;
- goto case;
- case Z15:
- y += 10;
- break;
- default:
- y += 4;
- break;
+ case X15:
+ y += 1;
+ goto case;
+ case 3:
+ y += 2;
+ break;
+ case Y15:
+ y += 20;
+ goto case;
+ case Z15:
+ y += 10;
+ break;
+ default:
+ y += 4;
+ break;
}
printf("y = %d\n", y);
return y;
@@ -382,10 +384,10 @@ void test16()
E16 e = E16.A;
final switch (e)
{
- case E16.A:
- case E16.B:
- case E16.C:
- ;
+ case E16.A:
+ case E16.B:
+ case E16.C:
+ {}
}
}
@@ -396,37 +398,37 @@ void test17()
int i = 2;
switch (i)
{
- case 1: .. case 3:
- i = 5;
- break;
- default:
- assert(0);
+ case 1: .. case 3:
+ i = 5;
+ break;
+ default:
+ assert(0);
}
if (i != 5)
- assert(0);
+ assert(0);
switch (i)
{
- case 1: .. case 3:
- i = 4;
- break;
- case 5:
- i = 6;
- break;
- default:
- assert(0);
+ case 1: .. case 3:
+ i = 4;
+ break;
+ case 5:
+ i = 6;
+ break;
+ default:
+ assert(0);
}
if (i != 6)
- assert(0);
+ assert(0);
}
/*****************************************/
int test19()
{
- enum foo{ bar };
- foo x;
- final switch(x){ case foo.bar: return 0; }
+ enum foo{ bar }
+ foo x;
+ final switch(x){ case foo.bar: return 0; }
}
/*****************************************/
@@ -435,10 +437,10 @@ void test20()
{
switch(1)
{
- mixin("case 0:{}");
- case 1:
- case 2:
- default:
+ mixin("case 0:{}");
+ case 1:
+ case 2:
+ default:
}
}
@@ -458,7 +460,7 @@ int wrongcode3139(int x)
case -9: .. case 2: return 3;
default:
return 4;
- }
+ }
}
static assert(wrongcode3139(-5)==3);
@@ -497,6 +499,21 @@ void test7358()
}
/*****************************************/
+// 9263
+
+void test9263()
+{
+ enum Foo { A }
+
+ Foo f;
+ final switch (f) with(Foo)
+ {
+ case A:
+ return;
+ }
+}
+
+/*****************************************/
int main()
{
@@ -520,6 +537,7 @@ int main()
test19();
test20();
test7358();
+ test9263();
printf("Success\n");
return 0;
Please sign in to comment.
Something went wrong with that request. Please try again.